|
3 | 3 | test_description='diff function context'
|
4 | 4 |
|
5 | 5 | . ./test-lib.sh
|
6 |
| -. "$TEST_DIRECTORY"/diff-lib.sh |
7 | 6 |
|
| 7 | +dir="$TEST_DIRECTORY/t4051" |
8 | 8 |
|
9 |
| -cat <<\EOF >hello.c |
10 |
| -#include <stdio.h> |
11 |
| -
|
12 |
| -static int a(void) |
13 |
| -{ |
14 |
| - /* |
15 |
| - * Dummy. |
16 |
| - */ |
| 9 | +commit_and_tag () { |
| 10 | + tag=$1 && |
| 11 | + shift && |
| 12 | + git add "$@" && |
| 13 | + test_tick && |
| 14 | + git commit -m "$tag" && |
| 15 | + git tag "$tag" |
17 | 16 | }
|
18 | 17 |
|
19 |
| -static int hello_world(void) |
20 |
| -{ |
21 |
| - /* Classic. */ |
22 |
| - printf("Hello world.\n"); |
23 |
| -
|
24 |
| - /* Success! */ |
25 |
| - return 0; |
| 18 | +first_context_line () { |
| 19 | + awk ' |
| 20 | + found {print; exit} |
| 21 | + /^@@/ {found = 1} |
| 22 | + ' |
26 | 23 | }
|
27 |
| -static int b(void) |
28 |
| -{ |
29 |
| - /* |
30 |
| - * Dummy, too. |
31 |
| - */ |
| 24 | + |
| 25 | +last_context_line () { |
| 26 | + sed -ne \$p |
32 | 27 | }
|
33 | 28 |
|
34 |
| -int main(int argc, char **argv) |
35 |
| -{ |
36 |
| - a(); |
37 |
| - b(); |
38 |
| - return hello_world(); |
| 29 | +check_diff () { |
| 30 | + name=$1 |
| 31 | + desc=$2 |
| 32 | + options="-W $3" |
| 33 | + |
| 34 | + test_expect_success "$desc" ' |
| 35 | + git diff $options "$name^" "$name" >"$name.diff" |
| 36 | + ' |
| 37 | + |
| 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" |
| 43 | + ' |
39 | 44 | }
|
40 |
| -EOF |
41 | 45 |
|
42 | 46 | test_expect_success 'setup' '
|
43 |
| - git add hello.c && |
44 |
| - test_tick && |
45 |
| - git commit -m initial && |
46 |
| -
|
47 |
| - grep -v Classic <hello.c >hello.c.new && |
48 |
| - mv hello.c.new hello.c |
49 |
| -' |
50 |
| - |
51 |
| -cat <<\EOF >expected |
52 |
| -diff --git a/hello.c b/hello.c |
53 |
| ---- a/hello.c |
54 |
| -+++ b/hello.c |
55 |
| -@@ -10,8 +10,7 @@ static int a(void) |
56 |
| - static int hello_world(void) |
57 |
| - { |
58 |
| -- /* Classic. */ |
59 |
| - printf("Hello world.\n"); |
60 |
| - |
61 |
| - /* Success! */ |
62 |
| - return 0; |
63 |
| - } |
64 |
| -EOF |
65 |
| - |
66 |
| -test_expect_success 'diff -U0 -W' ' |
67 |
| - git diff -U0 -W >actual && |
68 |
| - compare_diff_patch actual expected |
69 |
| -' |
70 |
| - |
71 |
| -cat <<\EOF >expected |
72 |
| -diff --git a/hello.c b/hello.c |
73 |
| ---- a/hello.c |
74 |
| -+++ b/hello.c |
75 |
| -@@ -9,9 +9,8 @@ static int a(void) |
76 |
| - |
77 |
| - static int hello_world(void) |
78 |
| - { |
79 |
| -- /* Classic. */ |
80 |
| - printf("Hello world.\n"); |
81 |
| - |
82 |
| - /* Success! */ |
83 |
| - return 0; |
84 |
| - } |
85 |
| -EOF |
86 |
| - |
87 |
| -test_expect_success 'diff -W' ' |
88 |
| - git diff -W >actual && |
89 |
| - compare_diff_patch actual expected |
| 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 && |
| 50 | +
|
| 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 && |
| 54 | +
|
| 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 && |
| 58 | +
|
| 59 | + cat "$dir/appended1.c" >>file.c && |
| 60 | + commit_and_tag appended file.c && |
| 61 | +
|
| 62 | + cat "$dir/appended2.c" >>file.c && |
| 63 | + commit_and_tag extended file.c && |
| 64 | +
|
| 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 && |
| 68 | +
|
| 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 |
| 74 | +' |
| 75 | + |
| 76 | +check_diff changed_hello 'changed function' |
| 77 | + |
| 78 | +test_expect_success ' context includes begin' ' |
| 79 | + grep "^ .*Begin of hello" changed_hello.diff |
| 80 | +' |
| 81 | + |
| 82 | +test_expect_success ' context includes end' ' |
| 83 | + grep "^ .*End of hello" changed_hello.diff |
| 84 | +' |
| 85 | + |
| 86 | +test_expect_success ' context does not include other functions' ' |
| 87 | + test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1 |
| 88 | +' |
| 89 | + |
| 90 | +test_expect_success ' context does not include preceding empty lines' ' |
| 91 | + test "$(first_context_line <changed_hello.diff)" != " " |
| 92 | +' |
| 93 | + |
| 94 | +test_expect_success ' context does not include trailing empty lines' ' |
| 95 | + test "$(last_context_line <changed_hello.diff)" != " " |
| 96 | +' |
| 97 | + |
| 98 | +check_diff changed_includes 'changed includes' |
| 99 | + |
| 100 | +test_expect_success ' context includes begin' ' |
| 101 | + grep "^ .*Begin.h" changed_includes.diff |
| 102 | +' |
| 103 | + |
| 104 | +test_expect_success ' context includes end' ' |
| 105 | + grep "^ .*End.h" changed_includes.diff |
| 106 | +' |
| 107 | + |
| 108 | +test_expect_success ' context does not include other functions' ' |
| 109 | + test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1 |
| 110 | +' |
| 111 | + |
| 112 | +test_expect_success ' context does not include trailing empty lines' ' |
| 113 | + test "$(last_context_line <changed_includes.diff)" != " " |
| 114 | +' |
| 115 | + |
| 116 | +check_diff appended 'appended function' |
| 117 | + |
| 118 | +test_expect_success ' context includes begin' ' |
| 119 | + grep "^[+].*Begin of first part" appended.diff |
| 120 | +' |
| 121 | + |
| 122 | +test_expect_success ' context includes end' ' |
| 123 | + grep "^[+].*End of first part" appended.diff |
| 124 | +' |
| 125 | + |
| 126 | +test_expect_success ' context does not include other functions' ' |
| 127 | + test $(grep -c "^[ +-].*Begin" appended.diff) -le 1 |
| 128 | +' |
| 129 | + |
| 130 | +check_diff extended 'appended function part' |
| 131 | + |
| 132 | +test_expect_success ' context includes begin' ' |
| 133 | + grep "^ .*Begin of first part" extended.diff |
| 134 | +' |
| 135 | + |
| 136 | +test_expect_success ' context includes end' ' |
| 137 | + grep "^[+].*End of second part" extended.diff |
| 138 | +' |
| 139 | + |
| 140 | +test_expect_success ' context does not include other functions' ' |
| 141 | + test $(grep -c "^[ +-].*Begin" extended.diff) -le 2 |
| 142 | +' |
| 143 | + |
| 144 | +test_expect_success ' context does not include preceding empty lines' ' |
| 145 | + test "$(first_context_line <extended.diff)" != " " |
| 146 | +' |
| 147 | + |
| 148 | +check_diff long_common_tail 'change with long common tail and no context' -U0 |
| 149 | + |
| 150 | +test_expect_success ' context includes begin' ' |
| 151 | + grep "^ .*Begin of first part" long_common_tail.diff |
| 152 | +' |
| 153 | + |
| 154 | +test_expect_success ' context includes end' ' |
| 155 | + grep "^ .*End of second part" long_common_tail.diff |
| 156 | +' |
| 157 | + |
| 158 | +test_expect_success ' context does not include other functions' ' |
| 159 | + test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2 |
| 160 | +' |
| 161 | + |
| 162 | +test_expect_success ' context does not include preceding empty lines' ' |
| 163 | + test "$(first_context_line <long_common_tail.diff.diff)" != " " |
| 164 | +' |
| 165 | + |
| 166 | +check_diff changed_hello_appended 'changed function plus appended function' |
| 167 | + |
| 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 |
| 171 | +' |
| 172 | + |
| 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 |
| 176 | +' |
| 177 | + |
| 178 | +test_expect_success ' context does not include other functions' ' |
| 179 | + test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2 |
90 | 180 | '
|
91 | 181 |
|
92 | 182 | test_done
|
0 commit comments