import os from pathlib import Path import shutil import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] SCRIPTS = ROOT / 'shared/hooks/scripts' SKILL = '---\nname: caveman\ndescription: Terse style.\n---\n\n# caveman\n\nRespond terse like smart caveman.\n' def wrappers(): found = [] for name in ('pwsh', 'powershell'): executable = shutil.which(name) if executable: found.append((name, [executable, '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', str(SCRIPTS / 'Show-CavemanMode.ps1')])) bash = shutil.which('bash') if os.name == 'nt': # System32\bash.exe is WSL; use Git Bash like the other Windows tests. candidate = Path(os.environ.get('ProgramFiles', 'C:/Program Files')) / 'Git/bin/bash.exe' bash = str(candidate) if candidate.exists() else None if bash: found.append(('bash', [bash, str(SCRIPTS / 'show-caveman-mode.sh')])) return found class CavemanHookTests(unittest.TestCase): def run_hook(self, command, home, mode=None): environment = dict(os.environ, HOME=str(home), USERPROFILE=str(home)) environment.pop('CAVEMAN_DEFAULT_MODE', None) if mode: environment['CAVEMAN_DEFAULT_MODE'] = mode return subprocess.run(command, capture_output=True, encoding='utf-8', timeout=60, env=environment) def test_prints_installed_skill_without_frontmatter_and_respects_off(self): available = wrappers() if not available: self.skipTest('No supported shell is available.') with tempfile.TemporaryDirectory() as directory: home = Path(directory) for name, command in available: with self.subTest(shell=name, case='missing skill'): result = self.run_hook(command, home) self.assertEqual((0, ''), (result.returncode, result.stdout)) skill = home / '.agents/skills/caveman/SKILL.md' skill.parent.mkdir(parents=True) skill.write_text(SKILL, encoding='utf-8') for name, command in available: with self.subTest(shell=name, case='installed'): result = self.run_hook(command, home) self.assertEqual(0, result.returncode, result.stderr) self.assertIn('CAVEMAN MODE ACTIVE (mode: caveman)', result.stdout) self.assertIn('Respond terse like smart caveman.', result.stdout) self.assertNotIn('description: Terse style.', result.stdout) with self.subTest(shell=name, case='off'): result = self.run_hook(command, home, 'off') self.assertEqual((0, ''), (result.returncode, result.stdout)) if __name__ == '__main__': unittest.main()