138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
from pathlib import Path
|
|
import tempfile
|
|
|
|
from config_auditor.fixes import (
|
|
Fixer,
|
|
DeprecatedPackageFixer,
|
|
AddTestScriptFixer,
|
|
EnableStrictModeFixer,
|
|
)
|
|
|
|
|
|
class TestDeprecatedPackageFixer:
|
|
def test_can_fix_deprecated(self):
|
|
fixer = DeprecatedPackageFixer()
|
|
data = {"dependencies": {"request": "^2.0.0"}}
|
|
|
|
assert fixer.can_fix(data, Path("package.json")) is True
|
|
|
|
def test_cannot_fix_when_no_deprecated(self):
|
|
fixer = DeprecatedPackageFixer()
|
|
data = {"dependencies": {"express": "^4.0.0"}}
|
|
|
|
assert fixer.can_fix(data, Path("package.json")) is False
|
|
|
|
def test_apply_fix(self):
|
|
fixer = DeprecatedPackageFixer()
|
|
data = {"dependencies": {"request": "^2.0.0"}}
|
|
result = fixer.apply_fix(data, Path("package.json"))
|
|
|
|
assert "requests" in result["dependencies"]
|
|
assert "request" not in result["dependencies"]
|
|
|
|
|
|
class TestAddTestScriptFixer:
|
|
def test_can_fix_missing_test(self):
|
|
fixer = AddTestScriptFixer()
|
|
data = {"scripts": {}}
|
|
|
|
assert fixer.can_fix(data, Path("package.json")) is True
|
|
|
|
def test_cannot_fix_when_test_exists(self):
|
|
fixer = AddTestScriptFixer()
|
|
data = {"scripts": {"test": "jest"}}
|
|
|
|
assert fixer.can_fix(data, Path("package.json")) is False
|
|
|
|
def test_apply_fix(self):
|
|
fixer = AddTestScriptFixer()
|
|
data = {"scripts": {}}
|
|
result = fixer.apply_fix(data, Path("package.json"))
|
|
|
|
assert "test" in result["scripts"]
|
|
assert "build" in result["scripts"]
|
|
|
|
|
|
class TestEnableStrictModeFixer:
|
|
def test_can_fix_missing_strict(self):
|
|
fixer = EnableStrictModeFixer()
|
|
data = {"compilerOptions": {}}
|
|
|
|
assert fixer.can_fix(data, Path("tsconfig.json")) is True
|
|
|
|
def test_cannot_fix_when_strict_enabled(self):
|
|
fixer = EnableStrictModeFixer()
|
|
data = {"compilerOptions": {"strict": True}}
|
|
|
|
assert fixer.can_fix(data, Path("tsconfig.json")) is False
|
|
|
|
def test_apply_fix(self):
|
|
fixer = EnableStrictModeFixer()
|
|
data = {"compilerOptions": {}}
|
|
result = fixer.apply_fix(data, Path("tsconfig.json"))
|
|
|
|
assert result["compilerOptions"]["strict"] is True
|
|
|
|
|
|
class TestFixer:
|
|
def test_dry_run_does_not_modify_file(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "package.json"
|
|
content = '{"name": "test", "scripts": {}}'
|
|
config_path.write_text(content)
|
|
|
|
fixer = Fixer(dry_run=True)
|
|
fixer.fix_config(config_path, "json", content)
|
|
|
|
actual_content = config_path.read_text()
|
|
assert actual_content == content
|
|
|
|
def test_force_applies_fixes_without_confirm(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "package.json"
|
|
content = '{"name": "test", "scripts": {}}'
|
|
config_path.write_text(content)
|
|
|
|
fixer = Fixer(force=True)
|
|
fix_count = fixer.fix_config(config_path, "json", content)
|
|
|
|
assert fix_count > 0
|
|
actual_content = config_path.read_text()
|
|
assert '"test"' in actual_content
|
|
|
|
def test_returns_fix_count(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "package.json"
|
|
content = '{"name": "test", "scripts": {}}'
|
|
config_path.write_text(content)
|
|
|
|
fixer = Fixer(force=True)
|
|
fix_count = fixer.fix_config(config_path, "json", content)
|
|
|
|
assert fix_count >= 1
|
|
|
|
def test_invalid_content_returns_zero(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "package.json"
|
|
config_path.write_text("not valid json")
|
|
|
|
fixer = Fixer(force=True)
|
|
fix_count = fixer.fix_config(config_path, "json", "not valid json")
|
|
|
|
assert fix_count == 0
|
|
|
|
def test_backup_created_on_fix(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "package.json"
|
|
content = '{"name": "test", "scripts": {}}'
|
|
config_path.write_text(content)
|
|
|
|
fixer = Fixer(force=True)
|
|
fixer.fix_config(config_path, "json", content)
|
|
|
|
backup_path = Path(".config_auditor_backup") / "package.json.bak"
|
|
if backup_path.exists():
|
|
backup_content = backup_path.read_text()
|
|
assert "name" in backup_content
|
|
assert "test" in backup_content
|