3
test_description='diff function context'
4
5
. ./test-lib.sh
6
-. "$TEST_DIRECTORY"/diff-lib.sh
6
7
+dir="$TEST_DIRECTORY/t4051"
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"
16
}
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
+ '
23
}
27
-static int b(void)
28
-{
29
- /*
30
- * Dummy, too.
31
- */
24
+
25
+last_context_line () {
26
+ sed -ne \$p
27
}
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
+ '
44
}
40
-EOF
45
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
+
70
+check_diff changed_hello 'changed function'
71
+
72
+test_expect_success ' context includes begin' '
73
+ grep "^ .*Begin of hello" changed_hello.diff
74
+'
75
+
76
+test_expect_success ' context includes end' '
77
+ grep "^ .*End of hello" changed_hello.diff
78
+'
79
+
80
+test_expect_success ' context does not include other functions' '
81
+ test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1
82
+'
83
+
84
+test_expect_success ' context does not include preceding empty lines' '
85
+ test "$(first_context_line <changed_hello.diff)" != " "
86
+'
87
+
88
+test_expect_failure ' context does not include trailing empty lines' '
89
+ test "$(last_context_line <changed_hello.diff)" != " "
90
+'
91
+
92
+check_diff changed_includes 'changed includes'
93
+
94
+test_expect_success ' context includes begin' '
95
+ grep "^ .*Begin.h" changed_includes.diff
96
+'
97
+
98
+test_expect_success ' context includes end' '
99
+ grep "^ .*End.h" changed_includes.diff
100
+'
101
+
102
+test_expect_success ' context does not include other functions' '
103
+ test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1
104
+'
105
+
106
+test_expect_failure ' context does not include trailing empty lines' '
107
+ test "$(last_context_line <changed_includes.diff)" != " "
108
+'
109
+
110
+check_diff appended 'appended function'
111
+
112
+test_expect_success ' context includes begin' '
113
+ grep "^[+].*Begin of first part" appended.diff
114
+'
115
+
116
+test_expect_success ' context includes end' '
117
+ grep "^[+].*End of first part" appended.diff
118
+'
119
+
120
+test_expect_failure ' context does not include other functions' '
121
+ test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
122
+'
123
+
124
+check_diff extended 'appended function part'
125
+
126
+test_expect_success ' context includes begin' '
127
+ grep "^ .*Begin of first part" extended.diff
128
+'
129
+
130
+test_expect_success ' context includes end' '
131
+ grep "^[+].*End of second part" extended.diff
132
+'
133
+
134
+test_expect_failure ' context does not include other functions' '
135
+ test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
136
+'
137
+
138
+test_expect_success ' context does not include preceding empty lines' '
139
+ test "$(first_context_line <extended.diff)" != " "
140
+'
141
+
142
+check_diff long_common_tail 'change with long common tail and no context' -U0
143
+
144
+test_expect_success ' context includes begin' '
145
+ grep "^ .*Begin of first part" long_common_tail.diff
146
+'
147
+
148
+test_expect_failure ' context includes end' '
149
+ grep "^ .*End of second part" long_common_tail.diff
150
+'
151
+
152
+test_expect_success ' context does not include other functions' '
153
+ test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2
154
+'
155
+
156
+test_expect_success ' context does not include preceding empty lines' '
157
+ test "$(first_context_line <long_common_tail.diff.diff)" != " "
158
'
159
160
test_done