fix: resolve CI test failure in output.py
- Fixed undefined 'tool' variable in display_history function - Changed '[tool]' markup tag usage to proper Rich syntax - All tests now pass (38/38 unit tests) - Type checking passes with mypy --strict
This commit is contained in:
159
tests/test_autofix.py
Normal file
159
tests/test_autofix.py
Normal file
@@ -0,0 +1,159 @@
|
||||
"""Tests for the auto-fix module."""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from cli_diff_auditor.autofix import AutoFixer, SafeWriter
|
||||
|
||||
|
||||
class TestSafeWriter:
|
||||
"""Test cases for SafeWriter class."""
|
||||
|
||||
def test_write_with_backup(self):
|
||||
"""Test writing content with backup creation."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
test_file = os.path.join(tmpdir, "test.txt")
|
||||
with open(test_file, 'w') as f:
|
||||
f.write("original content")
|
||||
|
||||
writer = SafeWriter()
|
||||
result = writer.write_with_backup(test_file, "new content")
|
||||
|
||||
assert result.success is True
|
||||
assert result.fixes_applied == 1
|
||||
with open(test_file, 'r') as f:
|
||||
assert f.read() == "new content"
|
||||
assert result.original_path != test_file
|
||||
|
||||
def test_write_without_backup(self):
|
||||
"""Test writing content without backup."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
test_file = os.path.join(tmpdir, "test.txt")
|
||||
with open(test_file, 'w') as f:
|
||||
f.write("original content")
|
||||
|
||||
writer = SafeWriter()
|
||||
result = writer.write_with_backup(test_file, "new content", create_backup=False)
|
||||
|
||||
assert result.success is True
|
||||
assert result.original_path == test_file
|
||||
|
||||
def test_remove_trailing_whitespace(self):
|
||||
"""Test removing trailing whitespace."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("line1 \n")
|
||||
f.write("line2\t\n")
|
||||
f.write("line3\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.remove_trailing_whitespace(temp_path)
|
||||
|
||||
assert result.success is True
|
||||
with open(temp_path, 'r') as f:
|
||||
content = f.read()
|
||||
assert content == "line1\nline2\nline3\n"
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_remove_trailing_whitespace_no_changes(self):
|
||||
"""Test removing trailing whitespace when none exists."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("line1\nline2\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.remove_trailing_whitespace(temp_path)
|
||||
|
||||
assert result.success is True
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_fix_notimplemented_error(self):
|
||||
"""Test fixing NotImplemented to NotImplementedError."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("def foo():\n raise NotImplemented\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.fix_notimplemented_error(temp_path)
|
||||
|
||||
assert result.success is True
|
||||
with open(temp_path, 'r') as f:
|
||||
content = f.read()
|
||||
assert "raise NotImplementedError" in content
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_apply_regex_fixes(self):
|
||||
"""Test applying regex-based fixes."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("old_value = 1\nold_value = 2\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.apply_regex_fixes(
|
||||
temp_path,
|
||||
r"old_value",
|
||||
"new_value"
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
with open(temp_path, 'r') as f:
|
||||
content = f.read()
|
||||
assert "new_value = 1" in content
|
||||
assert "new_value = 2" in content
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_apply_regex_fixes_no_matches(self):
|
||||
"""Test regex fix with no matches."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("other_value = 1\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.apply_regex_fixes(
|
||||
temp_path,
|
||||
r"nonexistent",
|
||||
"replacement"
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
assert result.fixes_applied == 0
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_remove_debug_imports(self):
|
||||
"""Test removing debug imports."""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
||||
f.write("import ipdb\nimport pdb\ncode here\n")
|
||||
temp_path = f.name
|
||||
|
||||
try:
|
||||
fixer = AutoFixer()
|
||||
result = fixer.remove_debug_imports(temp_path)
|
||||
|
||||
assert result.success is True
|
||||
with open(temp_path, 'r') as f:
|
||||
content = f.read()
|
||||
assert "# import ipdb" in content
|
||||
assert "# import pdb" in content
|
||||
assert "code here" in content
|
||||
finally:
|
||||
os.unlink(temp_path)
|
||||
|
||||
def test_file_not_found(self):
|
||||
"""Test fixing a non-existent file."""
|
||||
fixer = AutoFixer()
|
||||
result = fixer.remove_trailing_whitespace("/nonexistent/file.txt")
|
||||
|
||||
assert result.success is False
|
||||
assert "no such file" in result.error_message.lower() or "not found" in result.error_message.lower()
|
||||
Reference in New Issue
Block a user