-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
273 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import re | ||
|
||
|
||
def apply_changes(original_content, diff_content): | ||
# Split the content into lines | ||
original_lines = original_content.splitlines() | ||
diff_lines = diff_content.splitlines() | ||
|
||
result_lines = original_lines.copy() | ||
line_offset = 0 | ||
|
||
for diff_line in diff_lines: | ||
# Parse the diff line | ||
match = re.match(r'([+\-r]) (\d+):(.*)', diff_line.strip()) | ||
if match: | ||
operation, line_number, content = match.groups() | ||
line_number = int(line_number) - 1 # Convert to 0-based index | ||
|
||
# Adjust line number for previous additions/deletions | ||
adjusted_line_number = line_number + line_offset | ||
|
||
if operation == '+': | ||
# Add a new line | ||
result_lines.insert(adjusted_line_number, content.strip()) | ||
line_offset += 1 | ||
elif operation == '-': | ||
# Remove a line | ||
if 0 <= adjusted_line_number < len(result_lines): | ||
del result_lines[adjusted_line_number] | ||
line_offset -= 1 | ||
elif operation == 'r': | ||
# Replace a line | ||
if 0 <= adjusted_line_number < len(result_lines): | ||
result_lines[adjusted_line_number] = content.strip() | ||
|
||
return '\n'.join(result_lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import difflib | ||
from colorama import Fore, Style, init | ||
|
||
# Initialize colorama | ||
init(autoreset=True) | ||
|
||
|
||
def generate_colored_diff(original_content, new_content, context_lines=3): | ||
original_lines = original_content.splitlines() | ||
new_lines = new_content.splitlines() | ||
|
||
differ = difflib.unified_diff( | ||
original_lines, new_lines, lineterm='', n=context_lines) | ||
|
||
colored_diff = [] | ||
for line in differ: | ||
if line.startswith('+'): | ||
colored_diff.append(f"{Fore.GREEN}{line}{Style.RESET_ALL}") | ||
elif line.startswith('-'): | ||
colored_diff.append(f"{Fore.RED}{line}{Style.RESET_ALL}") | ||
elif line.startswith('^'): | ||
colored_diff.append(f"{Fore.BLUE}{line}{Style.RESET_ALL}") | ||
else: | ||
colored_diff.append(line) | ||
|
||
return '\n'.join(colored_diff) | ||
|
||
|
||
def preview_file_changes(operation, filename, new_content=None, original_content=None): | ||
preview = [f"{Fore.CYAN}{Style.BRIGHT}File: {filename}{Style.RESET_ALL}"] | ||
|
||
if operation == 'CREATE': | ||
preview.append( | ||
f"{Fore.GREEN}{Style.BRIGHT}Operation: CREATE{Style.RESET_ALL}") | ||
preview.append(f"{Fore.GREEN}New content:{Style.RESET_ALL}") | ||
preview.append(f"{Fore.GREEN}{new_content}{Style.RESET_ALL}") | ||
elif operation == 'UPDATE': | ||
preview.append( | ||
f"{Fore.YELLOW}{Style.BRIGHT}Operation: UPDATE{Style.RESET_ALL}") | ||
if original_content: | ||
preview.append(generate_colored_diff( | ||
original_content, new_content)) | ||
else: | ||
preview.append( | ||
f"{Fore.RED}Error: Missing original content or changes for UPDATE operation{Style.RESET_ALL}") | ||
elif operation == 'DELETE': | ||
preview.append( | ||
f"{Fore.RED}{Style.BRIGHT}Operation: DELETE{Style.RESET_ALL}") | ||
preview.append( | ||
f"{Fore.RED}The file '{filename}' will be deleted.{Style.RESET_ALL}") | ||
else: | ||
preview.append( | ||
f"{Fore.RED}Unknown operation: {operation}{Style.RESET_ALL}") | ||
|
||
return '\n'.join(preview) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
from drd.utils.step_executor import apply_changes # Update this import as needed | ||
|
||
|
||
class TestApplyChangesSpecific(unittest.TestCase): | ||
def test_add_email_and_move_paragraph(self): | ||
original_content = """<body> | ||
<h1>Welcome to Our Company</h1> | ||
<p>We are a leading provider of services.</p> | ||
</body> | ||
<p>Contact us for more information.</p> | ||
""" | ||
changes = """ | ||
+ 4: <p>Email: [email protected]</p> | ||
- 5: | ||
+ 5: <p>Contact us for more information.</p> | ||
""" | ||
expected_content = """<body> | ||
<h1>Welcome to Our Company</h1> | ||
<p>We are a leading provider of services.</p> | ||
<p>Email: [email protected]</p> | ||
<p>Contact us for more information.</p> | ||
</body>""" | ||
result = apply_changes(original_content, changes) | ||
self.assertEqual(result.strip(), expected_content.strip()) | ||
|
||
def test_html_structure_changes(self): | ||
original_content = """<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Welcome to Our Company</h1> | ||
<h3>hello</h3> | ||
<h2>About Us</h2> | ||
<p>Address: 123 Main Street, Anytown, ST 12345</p> | ||
</body> | ||
<p>Phone: (555) 123-4567</p> | ||
</html>""" | ||
changes = """ | ||
r 5: <title>About Us - TestApp</title> | ||
r 11: <h2>About Us</h2> | ||
r 12: <p>Address: 123 Main Street, Anytown, ST 12345</p> | ||
r 13: <p>Phone: (555) 123-4567</p> | ||
+ 14: <p>Email: [email protected]</p> | ||
- 15: | ||
- 16: | ||
""" | ||
expected_content = """<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>About Us - TestApp</title> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Welcome to Our Company</h1> | ||
<h3>hello</h3> | ||
<h2>About Us</h2> | ||
<p>Address: 123 Main Street, Anytown, ST 12345</p> | ||
<p>Phone: (555) 123-4567</p> | ||
<p>Email: [email protected]</p> | ||
</body> | ||
</html>""" | ||
result = apply_changes(original_content, changes) | ||
self.assertEqual(result.strip(), expected_content.strip()) | ||
|
||
def test_multiple_changes_same_line(self): | ||
original_content = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n" | ||
changes = """ | ||
r 2: Modified Line 2 | ||
+ 2: New Line between 1 and 2 | ||
- 4: | ||
r 5: Modified Line 5 | ||
""" | ||
expected_content = "Line 1\nNew Line between 1 and 2\nModified Line 2\nLine 3\nModified Line 5\n" | ||
result = apply_changes(original_content, changes) | ||
self.assertEqual(result.strip(), expected_content.strip()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import unittest | ||
from drd.utils.diff import generate_colored_diff, preview_file_changes | ||
from colorama import Fore, Style, init | ||
|
||
# Initialize colorama | ||
init(autoreset=True) | ||
|
||
|
||
class TestDiffUtils(unittest.TestCase): | ||
|
||
def test_generate_colored_diff(self): | ||
original_content = "Line 1\nLine 2\nLine 3\n" | ||
new_content = "Line 1\nModified Line 2\nLine 3\nNew Line 4\n" | ||
|
||
expected_diff = ( | ||
f"{Fore.RED}--- {Style.RESET_ALL}\n" | ||
f"{Fore.GREEN}+++ {Style.RESET_ALL}\n" | ||
f"@@ -1,3 +1,4 @@\n" | ||
f" Line 1\n" | ||
f"{Fore.RED}-Line 2{Style.RESET_ALL}\n" | ||
f"{Fore.GREEN}+Modified Line 2{Style.RESET_ALL}\n" | ||
f" Line 3\n" | ||
f"{Fore.GREEN}+New Line 4{Style.RESET_ALL}" | ||
) | ||
|
||
result = generate_colored_diff(original_content, new_content) | ||
self.assertEqual(result, expected_diff) | ||
|
||
def test_preview_file_changes_create(self): | ||
result = preview_file_changes( | ||
'CREATE', 'new_file.txt', new_content='New file content') | ||
expected_output = ( | ||
f"{Fore.CYAN}{Style.BRIGHT}File: new_file.txt{Style.RESET_ALL}\n" | ||
f"{Fore.GREEN}{Style.BRIGHT}Operation: CREATE{Style.RESET_ALL}\n" | ||
f"{Fore.GREEN}New content:{Style.RESET_ALL}\n" | ||
f"{Fore.GREEN}New file content{Style.RESET_ALL}" | ||
) | ||
self.assertEqual(result, expected_output) | ||
|
||
def test_preview_file_changes_update(self): | ||
original_content = "Line 1\nLine 2\nLine 3\n" | ||
new_content = "Line 1\nModified Line 2\nLine 3\nNew Line 4\n" | ||
result = preview_file_changes( | ||
'UPDATE', 'existing_file.txt', new_content=new_content, original_content=original_content) | ||
self.assertIn( | ||
f"{Fore.YELLOW}{Style.BRIGHT}Operation: UPDATE{Style.RESET_ALL}", result) | ||
self.assertIn(f"{Fore.RED}-Line 2{Style.RESET_ALL}", result) | ||
self.assertIn(f"{Fore.GREEN}+Modified Line 2{Style.RESET_ALL}", result) | ||
self.assertIn(f"{Fore.GREEN}+New Line 4{Style.RESET_ALL}", result) | ||
|
||
def test_preview_file_changes_delete(self): | ||
result = preview_file_changes('DELETE', 'file_to_delete.txt') | ||
expected_output = ( | ||
f"{Fore.CYAN}{Style.BRIGHT}File: file_to_delete.txt{Style.RESET_ALL}\n" | ||
f"{Fore.RED}{Style.BRIGHT}Operation: DELETE{Style.RESET_ALL}\n" | ||
f"{Fore.RED}The file 'file_to_delete.txt' will be deleted.{Style.RESET_ALL}" | ||
) | ||
self.assertEqual(result, expected_output) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.