-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_diff.py
More file actions
168 lines (135 loc) · 6.12 KB
/
Copy pathtest_user_diff.py
File metadata and controls
168 lines (135 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Test with the user's actual diff to verify correct placement
"""
import sys
import os
# Add the current directory to Python path so we can import from app.py
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from app import apply_unified_diff_to_content
def test_user_diff():
"""Test with the user's actual diff"""
# This should be the actual file structure in the repository
original_content = '''from flask import Flask, request, jsonify
import os
app = Flask(__name__)
@app.route('/trigger-error', methods=['GET'])
def trigger_error():
"""Endpoint that generates an error when enabled."""
global error_enabled
if error_enabled:
# INTENTIONAL ERROR: This will cause a NameError because 'undefined_variable' is not defined
# This is the error that can be fixed in a PR
result = undefined_variable + "This should cause an error"
return jsonify({'result': result})
else:
return jsonify({
'message': 'Error not triggered. Add ?error=true to trigger the error.',
'status': 'ok'
})
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({'status': 'healthy'})
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Not found'}), 404
@app.errorhandler(500)
def internal_error(error):
return jsonify({'error': 'Internal server error'}), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True) '''
# The user's actual diff
diff_content = '''diff --git a/app.py b/app.py
index c8cba05..c5e9216 100644
--- a/app.py
+++ b/app.py
@@ -69,7 +69,9 @@ def trigger_error():
if error_enabled:
# INTENTIONAL ERROR: This will cause a NameError because 'undefined_variable' is not defined
# This is the error that can be fixed in a PR
+ #result = undefined_variable + "This should cause an error"
result = undefined_variable + "This should cause an error"
+ result1 = undefined_variable + "This should cause an error"
return jsonify({'result': result})
else:
return jsonify({
'''
print("Testing user's actual diff...")
print("=" * 50)
try:
# Apply the diff
result = apply_unified_diff_to_content(original_content, diff_content)
print("✅ Diff applied successfully!")
print(f"Original content length: {len(original_content)} characters")
print(f"Modified content length: {len(result)} characters")
# Show the differences
original_lines = original_content.split('\n')
result_lines = result.split('\n')
print(f"\nOriginal file has {len(original_lines)} lines")
print(f"Modified file has {len(result_lines)} lines")
# Show the specific changes in the trigger_error function
print("\nChanges in trigger_error function:")
print("-" * 40)
# Find the trigger_error function
trigger_start = -1
for i, line in enumerate(original_lines):
if 'def trigger_error():' in line:
trigger_start = i
break
if trigger_start != -1:
print(f"trigger_error function starts at line {trigger_start + 1}")
# Show the function content
for i in range(trigger_start, min(trigger_start + 20, len(original_lines))):
if i < len(original_lines):
print(f"Original {i+1:3d}: {original_lines[i]}")
print("\nModified version:")
for i in range(trigger_start, min(trigger_start + 22, len(result_lines))):
if i < len(result_lines):
print(f"Modified {i+1:3d}: {result_lines[i]}")
# Verify the expected changes
print("\n" + "=" * 50)
print("Verifying expected changes:")
# Check if the commented line was added in the correct place
has_commented_line = False
has_result1_line = False
correct_placement = False
# Find the if block and check if our lines are inside it
if_block_start = -1
if_block_end = -1
for i, line in enumerate(result_lines):
if 'if error_enabled:' in line:
if_block_start = i
# Find the end of the if block (next line with same or less indentation)
if_indent = len(line) - len(line.lstrip())
for j in range(i + 1, len(result_lines)):
if result_lines[j].strip() and len(result_lines[j]) - len(result_lines[j].lstrip()) <= if_indent:
if_block_end = j
break
else:
if_block_end = len(result_lines)
break
for i, line in enumerate(result_lines):
if '#result = undefined_variable + "This should cause an error"' in line:
has_commented_line = True
# Check if it's inside the if block
if if_block_start != -1 and if_block_end != -1 and if_block_start < i < if_block_end:
correct_placement = True
if 'result1 = undefined_variable + "This should cause an error"' in line:
has_result1_line = True
print(f"✅ Commented line added: {has_commented_line}")
print(f"✅ result1 line added: {has_result1_line}")
print(f"✅ Correct placement (inside if block): {correct_placement}")
if has_commented_line and has_result1_line and correct_placement:
print("\n🎉 All changes applied correctly in the right place!")
else:
print("\n❌ Changes not applied correctly!")
# Show what went wrong
if not correct_placement:
print(" - The lines were not placed inside the if error_enabled: block")
except Exception as e:
print(f"❌ Error applying diff: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
test_user_diff()