Files
shellgenius/.tests/test_refactoring.py
7000pctAUTO 90ac86cc68
Some checks failed
CI / test (push) Failing after 12s
fix: remove unused imports from test files
2026-02-04 11:22:18 +00:00

144 lines
4.2 KiB
Python

"""Tests for ShellGenius refactoring module."""
from shellgenius.refactoring import (
RefactoringAnalyzer,
RefactoringResult,
SecurityRulesDB,
refactor_script,
)
class TestSecurityRulesDB:
"""Test security rules database."""
def test_get_rules(self):
"""Test getting all rules."""
rules = SecurityRulesDB.get_rules()
assert isinstance(rules, list)
assert len(rules) > 0
def test_check_rule_match(self):
"""Test rule matching."""
rule = SecurityRulesDB.check_rule("rm $USER_VAR")
assert rule is not None
assert rule["severity"] == "high"
def test_check_rule_no_match(self):
"""Test no false positives."""
rule = SecurityRulesDB.check_rule("ls -la")
assert rule is None
def test_chmod_777_detection(self):
"""Test chmod 777 detection."""
rule = SecurityRulesDB.check_rule("chmod 777 /path")
assert rule is not None
assert "777" in rule["pattern"]
def test_eval_detection(self):
"""Test eval with variable detection."""
rule = SecurityRulesDB.check_rule("eval $USER_INPUT")
assert rule is not None
class TestRefactoringAnalyzer:
"""Test refactoring analyzer."""
def test_analyzer_initialization(self):
"""Test analyzer creates properly."""
analyzer = RefactoringAnalyzer()
assert analyzer.parser is not None
assert analyzer.rules_db is not None
def test_analyze_safe_script(self):
"""Test analyzing safe script."""
analyzer = RefactoringAnalyzer()
script = "#!/bin/bash
echo \"Hello\"
ls -la
"
result = analyzer.analyze(script, include_suggestions=False)
assert isinstance(result, RefactoringResult)
assert result.shell_type == "bash"
assert result.score > 0
def test_calculate_score(self):
"""Test score calculation."""
analyzer = RefactoringAnalyzer()
from shellgenius.refactoring import RefactoringIssue
issues = [
RefactoringIssue(
line_number=1,
original="rm -rf /",
issue_type="CWE-78",
severity="high",
description="Test",
risk_assessment="Test",
suggestion="Test",
safer_alternative="",
)
]
score = analyzer._calculate_score(issues, "rm -rf /")
assert score < 100
def test_generate_suggestions(self):
"""Test suggestion generation."""
analyzer = RefactoringAnalyzer()
from shellgenius.refactoring import RefactoringIssue
issues = [
RefactoringIssue(
line_number=1,
original="rm -rf /",
issue_type="Test",
severity="high",
description="Test",
risk_assessment="Test",
suggestion="Test",
safer_alternative="",
)
]
suggestions = analyzer._generate_suggestions(issues, "script")
assert isinstance(suggestions, list)
assert len(suggestions) > 0
def test_check_improvements(self):
"""Test code quality improvement detection."""
analyzer = RefactoringAnalyzer()
backtick_result = analyzer._check_improvements("echo `date`")
assert backtick_result is not None
assert "backtick" in backtick_result["description"].lower()
def test_refactor_script_function(self):
"""Test convenience function."""
result = refactor_script("echo test", include_suggestions=False)
assert result is not None
assert hasattr(result, "shell_type")
assert hasattr(result, "issues")
assert hasattr(result, "score")
assert hasattr(result, "suggestions")
class TestRefactoringResult:
"""Test refactoring result dataclass."""
def test_result_creation(self):
"""Test creating result."""
result = RefactoringResult(
shell_type="bash",
issues=[],
score=100,
suggestions=["Use set -e"],
safer_script="#!/bin/bash\necho test",
)
assert result.shell_type == "bash"
assert result.score == 100
assert len(result.suggestions) == 1