Merge branch 'jk/test-lib-drop-pid-from-results'
[git.git] / t / t4051-diff-function-context.sh
blobb79b87790b29f6f1193578fd725d22da7058c819
1 #!/bin/sh
3 test_description='diff function context'
5 . ./test-lib.sh
7 dir="$TEST_DIRECTORY/t4051"
9 commit_and_tag () {
10 tag=$1 &&
11 shift &&
12 git add "$@" &&
13 test_tick &&
14 git commit -m "$tag" &&
15 git tag "$tag"
18 first_context_line () {
19 awk '
20 found {print; exit}
21 /^@@/ {found = 1}
25 last_context_line () {
26 sed -ne \$p
29 check_diff () {
30 name=$1
31 desc=$2
32 options="-W $3"
34 test_expect_success "$desc" '
35 git diff $options "$name^" "$name" >"$name.diff"
38 test_expect_success ' diff applies' '
39 test_when_finished "git reset --hard" &&
40 git checkout --detach "$name^" &&
41 git apply --index "$name.diff" &&
42 git diff --exit-code "$name"
46 test_expect_success 'setup' '
47 cat "$dir/includes.c" "$dir/dummy.c" "$dir/dummy.c" "$dir/hello.c" \
48 "$dir/dummy.c" "$dir/dummy.c" >file.c &&
49 commit_and_tag initial file.c &&
51 grep -v "delete me from hello" <file.c >file.c.new &&
52 mv file.c.new file.c &&
53 commit_and_tag changed_hello file.c &&
55 grep -v "delete me from includes" <file.c >file.c.new &&
56 mv file.c.new file.c &&
57 commit_and_tag changed_includes file.c &&
59 cat "$dir/appended1.c" >>file.c &&
60 commit_and_tag appended file.c &&
62 cat "$dir/appended2.c" >>file.c &&
63 commit_and_tag extended file.c &&
65 grep -v "Begin of second part" <file.c >file.c.new &&
66 mv file.c.new file.c &&
67 commit_and_tag long_common_tail file.c &&
69 git checkout initial &&
70 grep -v "delete me from hello" <file.c >file.c.new &&
71 mv file.c.new file.c &&
72 cat "$dir/appended1.c" >>file.c &&
73 commit_and_tag changed_hello_appended file.c
76 check_diff changed_hello 'changed function'
78 test_expect_success ' context includes begin' '
79 grep "^ .*Begin of hello" changed_hello.diff
82 test_expect_success ' context includes end' '
83 grep "^ .*End of hello" changed_hello.diff
86 test_expect_success ' context does not include other functions' '
87 test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1
90 test_expect_success ' context does not include preceding empty lines' '
91 test "$(first_context_line <changed_hello.diff)" != " "
94 test_expect_success ' context does not include trailing empty lines' '
95 test "$(last_context_line <changed_hello.diff)" != " "
98 check_diff changed_includes 'changed includes'
100 test_expect_success ' context includes begin' '
101 grep "^ .*Begin.h" changed_includes.diff
104 test_expect_success ' context includes end' '
105 grep "^ .*End.h" changed_includes.diff
108 test_expect_success ' context does not include other functions' '
109 test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1
112 test_expect_success ' context does not include trailing empty lines' '
113 test "$(last_context_line <changed_includes.diff)" != " "
116 check_diff appended 'appended function'
118 test_expect_success ' context includes begin' '
119 grep "^[+].*Begin of first part" appended.diff
122 test_expect_success ' context includes end' '
123 grep "^[+].*End of first part" appended.diff
126 test_expect_success ' context does not include other functions' '
127 test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
130 check_diff extended 'appended function part'
132 test_expect_success ' context includes begin' '
133 grep "^ .*Begin of first part" extended.diff
136 test_expect_success ' context includes end' '
137 grep "^[+].*End of second part" extended.diff
140 test_expect_success ' context does not include other functions' '
141 test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
144 test_expect_success ' context does not include preceding empty lines' '
145 test "$(first_context_line <extended.diff)" != " "
148 check_diff long_common_tail 'change with long common tail and no context' -U0
150 test_expect_success ' context includes begin' '
151 grep "^ .*Begin of first part" long_common_tail.diff
154 test_expect_success ' context includes end' '
155 grep "^ .*End of second part" long_common_tail.diff
158 test_expect_success ' context does not include other functions' '
159 test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2
162 test_expect_success ' context does not include preceding empty lines' '
163 test "$(first_context_line <long_common_tail.diff.diff)" != " "
166 check_diff changed_hello_appended 'changed function plus appended function'
168 test_expect_success ' context includes begin' '
169 grep "^ .*Begin of hello" changed_hello_appended.diff &&
170 grep "^[+].*Begin of first part" changed_hello_appended.diff
173 test_expect_success ' context includes end' '
174 grep "^ .*End of hello" changed_hello_appended.diff &&
175 grep "^[+].*End of first part" changed_hello_appended.diff
178 test_expect_success ' context does not include other functions' '
179 test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
182 test_done