hook_test.odin (2358B)
1 package hook 2 3 import "core:os" 4 import "core:path/filepath" 5 import "core:strings" 6 import "core:testing" 7 import "jm:sh" 8 9 @(test) 10 install_writes_the_hook_once :: proc(t: ^testing.T) { 11 context.allocator = context.temp_allocator 12 temp := os.temp_directory(context.temp_allocator) or_else "" 13 root, err := os.make_directory_temp(temp, "review-hook-*", context.temp_allocator) 14 testing.expect(t, err == nil) 15 defer os.remove_all(root) 16 testing.expect(t, sh.exec({"git", "init", "-q"}, {dir = root}, context.temp_allocator).ok) 17 // The tool's own git reads the real configuration, and a machine 18 // with core.hooksPath set would send the install elsewhere. 19 os.set_env("GIT_CONFIG_GLOBAL", "/dev/null") 20 os.set_env("GIT_CONFIG_NOSYSTEM", "1") 21 defer os.unset_env("GIT_CONFIG_GLOBAL") 22 defer os.unset_env("GIT_CONFIG_NOSYSTEM") 23 before, _ := os.get_working_directory(context.temp_allocator) 24 testing.expect(t, os.set_working_directory(root) == nil) 25 defer os.set_working_directory(before) 26 27 out, install_err := run({"install"}) 28 testing.expect_value(t, install_err, "") 29 testing.expect(t, strings.contains(out, "PreToolUse"), "the harness stanza is printed") 30 path := filepath.join({root, ".git", "hooks", "commit-msg"}, context.temp_allocator) or_else "" 31 data, read_err := os.read_entire_file_from_path(path, context.temp_allocator) 32 testing.expect(t, read_err == nil) 33 testing.expect(t, strings.has_prefix(string(data), "#!/bin/sh")) 34 testing.expect(t, strings.contains(string(data), `--message-file "$1" --exit-code`)) 35 // A hook already there is someone's, and is not replaced unasked. 36 _, again := run({"install"}) 37 testing.expect(t, strings.contains(again, "-force")) 38 _, forced := run({"-force", "install"}) 39 testing.expect_value(t, forced, "") 40 _, unknown := run({"dance"}) 41 testing.expect(t, strings.contains(unknown, "hook takes install or print")) 42 } 43 44 @(test) 45 print_and_agent_text_explain_the_gate :: proc(t: ^testing.T) { 46 out, err := run({}) 47 defer delete(out) 48 testing.expect_value(t, err, "") 49 for want in ([]string{"commit-msg", "--exit-code", "PreToolUse", "review agent"}) { 50 testing.expectf(t, strings.contains(out, want), "%q missing from hook print", want) 51 } 52 for want in ([]string{"review --json", "must-fix", "review:ignore", "--baseline"}) { 53 testing.expectf(t, strings.contains(agent_text, want), "%q missing from agent text", want) 54 } 55 }