Repository navigation
Expand file tree
/
Copy pathtest_real_diff.py
More file actions
157 lines (125 loc) · 5.83 KB
/
Copy pathtest_real_diff.py
File metadata and controls
157 lines (125 loc) · 5.83 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
#!/usr/bin/env python3
"""
Test the real diff parsing with the actual file structure
"""
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_real_diff():
"""Test with the actual file structure that matches the diff"""
# This is the file structure that the diff expects (based on the diff line numbers)
original_content = '''from flask import Flask, request, jsonify
import os
app = Flask(__name__)
@app.route('/trigger-error', methods=['GET'])
def trigger_error():
error_enabled = request.args.get('error', 'false').lower() == 'true'
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 actual diff from the user's curl command
diff_content = '''diff --git a/app.py b/app.py
index c8cba05..d3349e5 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({
@@ -117,4 +119,4 @@ def internal_error(error):
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
- app.run(host='0.0.0.0', port=port, debug=True) \n\\ No newline at end of file
+ app.run(host='0.0.0.0', port=port, debug=True) "'''
print("Testing real diff parsing...")
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 around 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 + 15, 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 + 17, len(result_lines))):
if i < len(result_lines):
print(f"Modified {i+1:3d}: {result_lines[i]}")
# Show the end of file changes
print("\nEnd of file changes:")
print("-" * 30)
for i in range(max(0, len(original_lines)-3), len(original_lines)):
if i < len(original_lines):
print(f"Original {i+1:3d}: {original_lines[i]}")
print()
for i in range(max(0, len(result_lines)-3), 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
has_commented_line = any('#result = undefined_variable + "This should cause an error"' in line for line in result_lines)
print(f"✅ Commented line added: {has_commented_line}")
# Check if the new result1 line was added
has_result1_line = any('result1 = undefined_variable + "This should cause an error"' in line for line in result_lines)
print(f"✅ result1 line added: {has_result1_line}")
# Check if the end of file was fixed
last_line = result_lines[-1] if result_lines else ""
has_fixed_end = '"' in last_line and last_line.strip().endswith('"')
print(f"✅ End of file fixed: {has_fixed_end}")
if has_commented_line and has_result1_line and has_fixed_end:
print("\n🎉 All expected changes applied correctly!")
else:
print("\n❌ Some expected changes are missing!")
except Exception as e:
print(f"❌ Error applying diff: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
test_real_diff()