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: basic' '
25
+ write_script .git/hooks/test-hook <<-EOF &&
26
+ echo Test hook
27
+ EOF
28
+
29
+ cat >expect <<-\EOF &&
30
+ Test hook
31
+ EOF
32
+ git hook run test-hook 2>actual &&
33
+ test_cmp expect actual
34
+'
35
+
36
+test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
37
+ write_script .git/hooks/test-hook <<-EOF &&
38
+ echo >&1 Will end up on stderr
39
+ echo >&2 Will end up on stderr
40
+ EOF
41
+
42
+ cat >stderr.expect <<-\EOF &&
43
+ Will end up on stderr
44
+ Will end up on stderr
45
+ EOF
46
+ git hook run test-hook >stdout.actual 2>stderr.actual &&
47
+ test_cmp stderr.expect stderr.actual &&
48
+ test_must_be_empty stdout.actual
49
+'
50
+
51
+test_expect_success 'git hook run: exit codes are passed along' '
52
+ write_script .git/hooks/test-hook <<-EOF &&
53
+ exit 1
54
+ EOF
55
+
56
+ test_expect_code 1 git hook run test-hook &&
57
+
58
+ write_script .git/hooks/test-hook <<-EOF &&
59
+ exit 2
60
+ EOF
61
+
62
+ test_expect_code 2 git hook run test-hook &&
63
+
64
+ write_script .git/hooks/test-hook <<-EOF &&
65
+ exit 128
66
+ EOF
67
+
68
+ test_expect_code 128 git hook run test-hook &&
69
+
70
+ write_script .git/hooks/test-hook <<-EOF &&
71
+ exit 129
72
+ EOF
73
+
74
+ test_expect_code 129 git hook run test-hook
75
+'
76
+
77
+test_expect_success 'git hook run arg u ments without -- is not allowed' '
78
+ test_expect_code 129 git hook run test-hook arg u ments
79
+'
80
+
81
+test_expect_success 'git hook run -- pass arguments' '
82
+ write_script .git/hooks/test-hook <<-\EOF &&
83
+ echo $1
84
+ echo $2
85
+ EOF
86
+
87
+ cat >expect <<-EOF &&
88
+ arg
89
+ u ments
90
+ EOF
91
+
92
+ git hook run test-hook -- arg "u ments" 2>actual &&
93
+ test_cmp expect actual
94
+'
95
+
96
+test_expect_success 'git hook run -- out-of-repo runs excluded' '
97
+ write_script .git/hooks/test-hook <<-EOF &&
98
+ echo Test hook
99
+ EOF
100
+
101
+ nongit test_must_fail git hook run test-hook
102
+'
103
+
104
+test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
105
+ mkdir my-hooks &&
106
+ write_script my-hooks/test-hook <<-\EOF &&
107
+ echo Hook ran $1 >>actual
108
+ EOF
109
+
110
+ cat >expect <<-\EOF &&
111
+ Test hook
112
+ Hook ran one
113
+ Hook ran two
114
+ Hook ran three
115
+ Hook ran four
116
+ EOF
117
+
118
+ # Test various ways of specifying the path. See also
119
+ # t1350-config-hooks-path.sh
120
+ >actual &&
121
+ git hook run test-hook -- ignored 2>>actual &&
122
+ git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
123
+ git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
124
+ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
125
+ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
126
+ test_cmp expect actual
127
+'
128
+
129
+test_done