]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1800-hook.sh
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / t / t1800-hook.sh
1 #!/bin/sh
2
3 test_description='git-hook command'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'git hook usage' '
9 test_expect_code 129 git hook &&
10 test_expect_code 129 git hook run &&
11 test_expect_code 129 git hook run -h &&
12 test_expect_code 129 git hook run --unknown 2>err &&
13 grep "unknown option" err
14 '
15
16 test_expect_success 'git hook run: nonexistent hook' '
17 cat >stderr.expect <<-\EOF &&
18 error: cannot find a hook named test-hook
19 EOF
20 test_expect_code 1 git hook run test-hook 2>stderr.actual &&
21 test_cmp stderr.expect stderr.actual
22 '
23
24 test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
25 git hook run --ignore-missing does-not-exist 2>stderr.actual &&
26 test_must_be_empty stderr.actual
27 '
28
29 test_expect_success 'git hook run: basic' '
30 write_script .git/hooks/test-hook <<-EOF &&
31 echo Test hook
32 EOF
33
34 cat >expect <<-\EOF &&
35 Test hook
36 EOF
37 git hook run test-hook 2>actual &&
38 test_cmp expect actual
39 '
40
41 test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
42 write_script .git/hooks/test-hook <<-EOF &&
43 echo >&1 Will end up on stderr
44 echo >&2 Will end up on stderr
45 EOF
46
47 cat >stderr.expect <<-\EOF &&
48 Will end up on stderr
49 Will end up on stderr
50 EOF
51 git hook run test-hook >stdout.actual 2>stderr.actual &&
52 test_cmp stderr.expect stderr.actual &&
53 test_must_be_empty stdout.actual
54 '
55
56 test_expect_success 'git hook run: exit codes are passed along' '
57 write_script .git/hooks/test-hook <<-EOF &&
58 exit 1
59 EOF
60
61 test_expect_code 1 git hook run test-hook &&
62
63 write_script .git/hooks/test-hook <<-EOF &&
64 exit 2
65 EOF
66
67 test_expect_code 2 git hook run test-hook &&
68
69 write_script .git/hooks/test-hook <<-EOF &&
70 exit 128
71 EOF
72
73 test_expect_code 128 git hook run test-hook &&
74
75 write_script .git/hooks/test-hook <<-EOF &&
76 exit 129
77 EOF
78
79 test_expect_code 129 git hook run test-hook
80 '
81
82 test_expect_success 'git hook run arg u ments without -- is not allowed' '
83 test_expect_code 129 git hook run test-hook arg u ments
84 '
85
86 test_expect_success 'git hook run -- pass arguments' '
87 write_script .git/hooks/test-hook <<-\EOF &&
88 echo $1
89 echo $2
90 EOF
91
92 cat >expect <<-EOF &&
93 arg
94 u ments
95 EOF
96
97 git hook run test-hook -- arg "u ments" 2>actual &&
98 test_cmp expect actual
99 '
100
101 test_expect_success 'git hook run -- out-of-repo runs excluded' '
102 write_script .git/hooks/test-hook <<-EOF &&
103 echo Test hook
104 EOF
105
106 nongit test_must_fail git hook run test-hook
107 '
108
109 test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
110 mkdir my-hooks &&
111 write_script my-hooks/test-hook <<-\EOF &&
112 echo Hook ran $1 >>actual
113 EOF
114
115 cat >expect <<-\EOF &&
116 Test hook
117 Hook ran one
118 Hook ran two
119 Hook ran three
120 Hook ran four
121 EOF
122
123 # Test various ways of specifying the path. See also
124 # t1350-config-hooks-path.sh
125 >actual &&
126 git hook run test-hook -- ignored 2>>actual &&
127 git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
128 git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
129 git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
130 git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
131 test_cmp expect actual
132 '
133
134 test_done