import importlib.util import sys import tempfile import unittest from pathlib import Path def load_pre_commit_checks(): module_path = Path(__file__).resolve().parent.parent / ".codex" / "hooks" / "pre_commit_checks.py" spec = importlib.util.spec_from_file_location("pre_commit_checks", module_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module class PreCommitChecksTests(unittest.TestCase): def test_git_commit_runs_python_self_tests_and_workspace_validation(self): pre_commit_checks = load_pre_commit_checks() with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) commands = pre_commit_checks._build_pre_commit_commands(root) self.assertEqual( commands, [ [sys.executable, "-m", "unittest", "discover", "-s", "scripts", "-p", "test_*.py"], [sys.executable, "scripts/validate_workspace.py"], ], ) self.assertFalse(any("npm" in part.lower() for command in commands for part in command)) def test_only_git_commit_commands_trigger_checks(self): pre_commit_checks = load_pre_commit_checks() self.assertTrue(pre_commit_checks._is_git_commit('git commit -m "change"')) self.assertTrue(pre_commit_checks._is_git_commit('git -c core.editor=true commit -m "change"')) self.assertFalse(pre_commit_checks._is_git_commit("git status --short")) self.assertFalse(pre_commit_checks._is_git_commit("echo git commit")) if __name__ == "__main__": unittest.main()