Add tests for pre-commit and commit-msg hooks
As desired, these pass for git-commit.sh, fail for builtin-commit (prior to the fixes), and succeeded for builtin-commit (after the fixes). Signed-off-by: Wincent Colaiuta <win@wincent.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Wincent Colaiuta committed
Dec 8, 2007 at 13:29 UTC
264474f29a3f41124f98e955b41ebe4e36d14b53
3 files changed
+153
-1
t/t7501-commit.sh
+1
-1
@@ -4,7 +4,7 @@
4
#
5
6
# FIXME: Test the various index usages, -i and -o, test reflog,
7
-# signoff, hooks
7
+# signoff
8
9
test_description='git-commit'
10
. ./test-lib.sh
t/t7503-pre-commit-hook.sh
new
+64
@@ -0,0 +1,64 @@
1
+#!/bin/sh
2
+
3
+test_description='pre-commit hook'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success "with no hook" \
8
+ "echo 'foo' > file &&
9
+ git add file &&
10
+ git commit -m 'first'"
11
+
12
+test_expect_success "--no-verify with no hook" \
13
+ "echo 'bar' > file &&
14
+ git add file &&
15
+ git commit --no-verify -m 'bar'"
16
+
17
+# now install hook that always succeeds
18
+HOOKDIR="$(git rev-parse --git-dir)/hooks"
19
+HOOK="$HOOKDIR/pre-commit"
20
+mkdir -p "$HOOKDIR"
21
+cat > "$HOOK" <<EOF
22
+#!/bin/sh
23
+exit 0
24
+EOF
25
+chmod +x "$HOOK"
26
+
27
+test_expect_success "with succeeding hook" \
28
+ "echo 'more' >> file &&
29
+ git add file &&
30
+ git commit -m 'more'"
31
+
32
+test_expect_success "--no-verify with succeeding hook" \
33
+ "echo 'even more' >> file &&
34
+ git add file &&
35
+ git commit --no-verify -m 'even more'"
36
+
37
+# now a hook that fails
38
+cat > "$HOOK" <<EOF
39
+#!/bin/sh
40
+exit 1
41
+EOF
42
+
43
+test_expect_failure "with failing hook" \
44
+ "echo 'another' >> file &&
45
+ git add file &&
46
+ git commit -m 'another'"
47
+
48
+test_expect_success "--no-verify with failing hook" \
49
+ "echo 'stuff' >> file &&
50
+ git add file &&
51
+ git commit --no-verify -m 'stuff'"
52
+
53
+chmod -x "$HOOK"
54
+test_expect_success "with non-executable hook" \
55
+ "echo 'content' >> file &&
56
+ git add file &&
57
+ git commit -m 'content'"
58
+
59
+test_expect_success "--no-verify with non-executable hook" \
60
+ "echo 'more content' >> file &&
61
+ git add file &&
62
+ git commit --no-verify -m 'more content'"
63
+
64
+test_done
t/t7504-commit-msg-hook.sh
new
+88
@@ -0,0 +1,88 @@
1
+#!/bin/sh
2
+
3
+test_description='commit-msg hook'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success "with no hook" \
8
+ "echo 'foo' > file &&
9
+ git add file &&
10
+ git commit -m 'first'"
11
+
12
+test_expect_success "--no-verify with no hook" \
13
+ "echo 'bar' > file &&
14
+ git add file &&
15
+ git commit --no-verify -m 'bar'"
16
+
17
+# now install hook that always succeeds
18
+HOOKDIR="$(git rev-parse --git-dir)/hooks"
19
+HOOK="$HOOKDIR/commit-msg"
20
+mkdir -p "$HOOKDIR"
21
+cat > "$HOOK" <<EOF
22
+#!/bin/sh
23
+exit 0
24
+EOF
25
+chmod +x "$HOOK"
26
+
27
+test_expect_success "with succeeding hook" \
28
+ "echo 'more' >> file &&
29
+ git add file &&
30
+ git commit -m 'more'"
31
+
32
+test_expect_success "--no-verify with succeeding hook" \
33
+ "echo 'even more' >> file &&
34
+ git add file &&
35
+ git commit --no-verify -m 'even more'"
36
+
37
+# now a hook that fails
38
+cat > "$HOOK" <<EOF
39
+#!/bin/sh
40
+exit 1
41
+EOF
42
+
43
+test_expect_failure "with failing hook" \
44
+ "echo 'another' >> file &&
45
+ git add file &&
46
+ git commit -m 'another'"
47
+
48
+test_expect_success "--no-verify with failing hook" \
49
+ "echo 'stuff' >> file &&
50
+ git add file &&
51
+ git commit --no-verify -m 'stuff'"
52
+
53
+chmod -x "$HOOK"
54
+test_expect_success "with non-executable hook" \
55
+ "echo 'content' >> file &&
56
+ git add file &&
57
+ git commit -m 'content'"
58
+
59
+test_expect_success "--no-verify with non-executable hook" \
60
+ "echo 'more content' >> file &&
61
+ git add file &&
62
+ git commit --no-verify -m 'more content'"
63
+
64
+# now a hook that edits the commit message
65
+cat > "$HOOK" <<'EOF'
66
+#!/bin/sh
67
+echo "new message" > "$1"
68
+exit 0
69
+EOF
70
+chmod +x "$HOOK"
71
+
72
+commit_msg_is () {
73
+ test "`git log --pretty=format:%s%b -1`" = "$1"
74
+}
75
+
76
+test_expect_success "hook edits commit message" \
77
+ "echo 'additional' >> file &&
78
+ git add file &&
79
+ git commit -m 'additional' &&
80
+ commit_msg_is 'new message'"
81
+
82
+test_expect_success "hook doesn't edit commit message" \
83
+ "echo 'plus' >> file &&
84
+ git add file &&
85
+ git commit --no-verify -m 'plus' &&
86
+ commit_msg_is 'plus'"
87
+
88
+test_done