Skip to content

Commit d3621de

Browse files
rscharfegitster
authored andcommitted
t4051: rewrite, add more tests
Remove the tests that checked against a fixed result and replace them with more focused checks of desired properties of the created diffs. That way we get more detailed and meaningful diagnostics. Store test file contents in files in a subdirectory in order to avoid cluttering the test script with them. Use tagged commits to store the changes to test diff -W against instead of using changes to the worktree. Use the worktree instead to try and apply the generated patch in order to validate it. Document unwanted features: trailing empty lines, too much context for appended functions, insufficient context at the end with -U0. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90f7b16 commit d3621de

File tree

6 files changed

+240
-74
lines changed

6 files changed

+240
-74
lines changed

t/t4051-diff-function-context.sh

Lines changed: 142 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,158 @@
33
test_description='diff function context'
44

55
. ./test-lib.sh
6-
. "$TEST_DIRECTORY"/diff-lib.sh
76

7+
dir="$TEST_DIRECTORY/t4051"
88

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"
1716
}
1817

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+
'
2623
}
27-
static int b(void)
28-
{
29-
/*
30-
* Dummy, too.
31-
*/
24+
25+
last_context_line () {
26+
sed -ne \$p
3227
}
3328

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+
'
3944
}
40-
EOF
4145

4246
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)" != " "
90158
'
91159

92160
test_done

t/t4051/appended1.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
int appended(void) // Begin of first part
3+
{
4+
int i;
5+
char *s = "a string";
6+
7+
printf("%s\n", s);
8+
9+
for (i = 99;
10+
i >= 0;
11+
i--) {
12+
printf("%d bottles of beer on the wall\n", i);
13+
}
14+
15+
printf("End of first part\n");

t/t4051/appended2.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
printf("Begin of second part\n");
2+
3+
/*
4+
* Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
5+
* sed diam nonumy eirmod tempor invidunt ut labore et dolore
6+
* magna aliquyam erat, sed diam voluptua. At vero eos et
7+
* accusam et justo duo dolores et ea rebum. Stet clita kasd
8+
* gubergren, no sea takimata sanctus est Lorem ipsum dolor
9+
* sit amet.
10+
*
11+
* Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
12+
* sed diam nonumy eirmod tempor invidunt ut labore et dolore
13+
* magna aliquyam erat, sed diam voluptua. At vero eos et
14+
* accusam et justo duo dolores et ea rebum. Stet clita kasd
15+
* gubergren, no sea takimata sanctus est Lorem ipsum dolor
16+
* sit amet.
17+
*
18+
* Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
19+
* sed diam nonumy eirmod tempor invidunt ut labore et dolore
20+
* magna aliquyam erat, sed diam voluptua. At vero eos et
21+
* accusam et justo duo dolores et ea rebum. Stet clita kasd
22+
* gubergren, no sea takimata sanctus est Lorem ipsum dolor
23+
* sit amet.
24+
*
25+
* Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
26+
* sed diam nonumy eirmod tempor invidunt ut labore et dolore
27+
* magna aliquyam erat, sed diam voluptua. At vero eos et
28+
* accusam et justo duo dolores et ea rebum. Stet clita kasd
29+
* gubergren, no sea takimata sanctus est Lorem ipsum dolor
30+
* sit amet.
31+
*
32+
*/
33+
34+
return 0;
35+
} // End of second part

t/t4051/dummy.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
static int dummy(void) // Begin of dummy
3+
{
4+
int rc = 0;
5+
6+
return rc;
7+
} // End of dummy

t/t4051/hello.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
static void hello(void) // Begin of hello
3+
{
4+
/*
5+
* Classic.
6+
*/
7+
putchar('H');
8+
putchar('e');
9+
putchar('l');
10+
putchar('l');
11+
putchar('o');
12+
putchar(' ');
13+
/* delete me from hello */
14+
putchar('w');
15+
putchar('o');
16+
putchar('r');
17+
putchar('l');
18+
putchar('d');
19+
putchar('.');
20+
putchar('\n');
21+
} // End of hello

t/t4051/includes.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <Begin.h>
2+
#include <unistd.h>
3+
#include <stdio.h>
4+
#include <sys/stat.h>
5+
#include <fcntl.h>
6+
#include <stddef.h>
7+
#include <stdlib.h>
8+
#include <stdarg.h>
9+
/* delete me from includes */
10+
#include <string.h>
11+
#include <sys/types.h>
12+
#include <dirent.h>
13+
#include <sys/time.h>
14+
#include <time.h>
15+
#include <signal.h>
16+
#include <assert.h>
17+
#include <regex.h>
18+
#include <utime.h>
19+
#include <syslog.h>
20+
#include <End.h>

0 commit comments

Comments
 (0)