test_main_validation.py
1 """ 2 Unit tests for main.py's validate_rules_at_startup() function. 3 """ 4 5 from unittest.mock import patch 6 7 import pytest 8 9 from main import validate_rules_at_startup 10 11 12 @pytest.mark.unit 13 def test_validate_rules_at_startup_success(tmp_path): 14 """Test validate_rules_at_startup with valid rules.""" 15 # Create temporary rule file 16 rule_file = tmp_path / "D-TEST-01.py" 17 rule_file.write_text(''' 18 """Test rule""" 19 def main(entry): 20 return {"passed": True} 21 ''') 22 23 enabled_rules = {"D-TEST-01": "abc123"} 24 25 with patch('os.path.exists', return_value=True): 26 with patch('builtins.open', return_value=open(rule_file)): 27 # Should not raise SystemExit 28 validate_rules_at_startup(enabled_rules) 29 30 31 @pytest.mark.unit 32 def test_validate_rules_at_startup_missing_file(): 33 """Test validate_rules_at_startup with missing rule file.""" 34 enabled_rules = {"D-MISSING-01": "abc123"} 35 36 with patch('os.path.exists', return_value=False): 37 with pytest.raises(SystemExit) as exc_info: 38 validate_rules_at_startup(enabled_rules) 39 assert exc_info.value.code == 1 40 41 42 @pytest.mark.unit 43 def test_validate_rules_at_startup_syntax_error(tmp_path): 44 """Test validate_rules_at_startup with syntax error in rule.""" 45 # Create rule file with syntax error 46 rule_file = tmp_path / "D-SYNTAX-01.py" 47 rule_file.write_text(''' 48 def main(entry) 49 return {"passed": True} # Missing colon 50 ''') 51 52 enabled_rules = {"D-SYNTAX-01": "abc123"} 53 54 with patch('os.path.exists', return_value=True): 55 with patch('builtins.open', return_value=open(rule_file)): 56 with pytest.raises(SystemExit) as exc_info: 57 validate_rules_at_startup(enabled_rules) 58 assert exc_info.value.code == 1 59 60 61 @pytest.mark.unit 62 def test_validate_rules_at_startup_missing_main(tmp_path): 63 """Test validate_rules_at_startup with missing main() function.""" 64 # Create rule file without main() function 65 rule_file = tmp_path / "D-NOMAIN-01.py" 66 rule_file.write_text(''' 67 """Test rule without main""" 68 def process(entry): 69 return {"passed": True} 70 ''') 71 72 enabled_rules = {"D-NOMAIN-01": "abc123"} 73 74 with patch('os.path.exists', return_value=True): 75 with patch('builtins.open', return_value=open(rule_file)): 76 with pytest.raises(SystemExit) as exc_info: 77 validate_rules_at_startup(enabled_rules) 78 assert exc_info.value.code == 1 79 80 81 @pytest.mark.unit 82 def test_validate_rules_at_startup_multiple_rules(tmp_path): 83 """Test validate_rules_at_startup with multiple valid rules.""" 84 rule1 = tmp_path / "D-TEST-01.py" 85 rule1.write_text('def main(entry):\n return {"passed": True}') 86 87 rule2 = tmp_path / "D-TEST-02.py" 88 rule2.write_text('def main(entry):\n return {"passed": False}') 89 90 enabled_rules = { 91 "D-TEST-01": "abc123", 92 "D-TEST-02": "def456", 93 } 94 95 def mock_exists(path): 96 return "D-TEST-01" in path or "D-TEST-02" in path 97 98 def mock_open(path, *args, **kwargs): 99 if "D-TEST-01" in path: 100 return open(rule1, *args, **kwargs) 101 elif "D-TEST-02" in path: 102 return open(rule2, *args, **kwargs) 103 raise FileNotFoundError(path) 104 105 with patch('os.path.exists', side_effect=mock_exists): 106 with patch('builtins.open', side_effect=mock_open): 107 # Should not raise SystemExit 108 validate_rules_at_startup(enabled_rules) 109 110 111 @pytest.mark.unit 112 def test_validate_rules_at_startup_empty_rules(): 113 """Test validate_rules_at_startup with no rules.""" 114 enabled_rules = {} 115 # Should not raise SystemExit for empty rules dict 116 validate_rules_at_startup(enabled_rules) 117 118 119 @pytest.mark.unit 120 def test_validate_rules_at_startup_partial_failure(tmp_path): 121 """Test validate_rules_at_startup fails if any rule is invalid.""" 122 good_rule = tmp_path / "D-GOOD-01.py" 123 good_rule.write_text('def main(entry):\n return {"passed": True}') 124 125 enabled_rules = { 126 "D-GOOD-01": "abc123", 127 "D-BAD-01": "def456", # This one will be missing 128 } 129 130 def mock_exists(path): 131 return "D-GOOD-01" in path 132 133 with patch('os.path.exists', side_effect=mock_exists): 134 with patch('builtins.open', return_value=open(good_rule)): 135 with pytest.raises(SystemExit) as exc_info: 136 validate_rules_at_startup(enabled_rules) 137 assert exc_info.value.code == 1