|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import subprocess |
| 4 | +import shutil |
| 5 | +import time |
| 6 | +from datetime import date |
| 7 | + |
| 8 | +class TestPreCommitHook(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.test_dir = "temp_test_repo" |
| 11 | + if os.path.exists(self.test_dir): |
| 12 | + shutil.rmtree(self.test_dir) |
| 13 | + os.makedirs(self.test_dir) |
| 14 | + os.chdir(self.test_dir) |
| 15 | + subprocess.run(["git", "init"], capture_output=True) |
| 16 | + # Configure user for testing |
| 17 | + subprocess.run(["git", "config", "user.email", "test@example.com"], capture_output=True) |
| 18 | + subprocess.run(["git", "config", "user.name", "Test User"], capture_output=True) |
| 19 | + |
| 20 | + os.makedirs(".gemini/hooks") |
| 21 | + os.makedirs("journal") |
| 22 | + |
| 23 | + # Initial commit |
| 24 | + with open(".gitignore", "w") as f: |
| 25 | + f.write("temp_test_repo/\n") |
| 26 | + subprocess.run(["git", "add", ".gitignore"], capture_output=True) |
| 27 | + subprocess.run(["git", "commit", "-m", "Initial commit"], capture_output=True) |
| 28 | + |
| 29 | + def tearDown(self): |
| 30 | + os.chdir("..") |
| 31 | + if os.path.exists(self.test_dir): |
| 32 | + shutil.rmtree(self.test_dir) |
| 33 | + |
| 34 | + def test_journal_mtime_validation(self): |
| 35 | + # Create a journal entry |
| 36 | + today = date.today().strftime("%Y-%m-%d") |
| 37 | + journal_path = f"journal/{today}.md" |
| 38 | + with open(journal_path, "w") as f: |
| 39 | + f.write("Journal entry") |
| 40 | + |
| 41 | + # Ensure mtime is set |
| 42 | + time.sleep(0.1) |
| 43 | + |
| 44 | + # Create a modified file |
| 45 | + with open("code.py", "w") as f: |
| 46 | + f.write("Code change") |
| 47 | + |
| 48 | + # Hook should fail because code.py is newer than journal |
| 49 | + # We'll run the pre-commit hook script directly |
| 50 | + result = subprocess.run(["python3", "../.gemini/hooks/pre-commit.py"], capture_output=True, text=True) |
| 51 | + self.assertNotEqual(result.returncode, 0) |
| 52 | + self.assertIn("Updated journal required", result.stdout + result.stderr) |
| 53 | + |
| 54 | + def test_journal_mtime_validation_success(self): |
| 55 | + # Add a dummy makefile to the test repo to avoid failure when running make test |
| 56 | + with open("makefile", "w") as f: |
| 57 | + f.write("test:\n\t@echo 'Tests passed'") |
| 58 | + |
| 59 | + # Create a modified file |
| 60 | + with open("code.py", "w") as f: |
| 61 | + f.write("Code change") |
| 62 | + |
| 63 | + # Create a journal entry AFTER the file modification |
| 64 | + time.sleep(0.1) |
| 65 | + today = date.today().strftime("%Y-%m-%d") |
| 66 | + journal_path = f"journal/{today}.md" |
| 67 | + with open(journal_path, "w") as f: |
| 68 | + f.write("Journal entry updated") |
| 69 | + |
| 70 | + # Hook should succeed |
| 71 | + result = subprocess.run(["python3", "../.gemini/hooks/pre-commit.py"], capture_output=True, text=True) |
| 72 | + if result.returncode != 0: |
| 73 | + print(f"\nHOOK FAILED (rc={result.returncode})") |
| 74 | + print(f"STDOUT: {result.stdout}") |
| 75 | + print(f"STDERR: {result.stderr}") |
| 76 | + self.assertEqual(result.returncode, 0) |
| 77 | + self.assertIn("Validation passed", result.stdout) |
| 78 | + |
| 79 | + def test_make_failure(self): |
| 80 | + # Add a failing makefile |
| 81 | + with open("makefile", "w") as f: |
| 82 | + f.write("test:\n\t@exit 1") |
| 83 | + |
| 84 | + # Create a journal entry |
| 85 | + today = date.today().strftime("%Y-%m-%d") |
| 86 | + journal_path = f"journal/{today}.md" |
| 87 | + with open(journal_path, "w") as f: |
| 88 | + f.write("Journal entry") |
| 89 | + |
| 90 | + # Ensure journal is newest |
| 91 | + time.sleep(0.1) |
| 92 | + os.utime(journal_path, None) |
| 93 | + |
| 94 | + # Hook should fail because make test fails |
| 95 | + result = subprocess.run(["python3", "../.gemini/hooks/pre-commit.py"], capture_output=True, text=True) |
| 96 | + self.assertNotEqual(result.returncode, 0) |
| 97 | + self.assertIn("Validation failed", result.stdout) |
| 98 | + |
| 99 | + def test_ignore_gemini_files(self): |
| 100 | + # Create a journal entry |
| 101 | + today = date.today().strftime("%Y-%m-%d") |
| 102 | + journal_path = f"journal/{today}.md" |
| 103 | + with open(journal_path, "w") as f: |
| 104 | + f.write("Journal entry") |
| 105 | + |
| 106 | + time.sleep(0.1) |
| 107 | + |
| 108 | + # Modify a .gemini file |
| 109 | + with open(".gemini/settings.json", "w") as f: |
| 110 | + f.write("{}") |
| 111 | + |
| 112 | + # Hook should succeed because .gemini file is ignored in mtime check |
| 113 | + result = subprocess.run(["python3", "../.gemini/hooks/pre-commit.py"], capture_output=True, text=True) |
| 114 | + # It might fail because make test fails (no makefile) |
| 115 | + # But it shouldn't fail with "Updated journal required" |
| 116 | + self.assertNotIn("Updated journal required", result.stdout + result.stderr) |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + unittest.main() |
0 commit comments