ls-files: update test style
Update test style in t/t30[*].sh for uniformity, that's to keep test title the same line with helper function itself, and fix some indentions. Add a new section "recommended style" in t/README to encourage people to use more modern style in test. Signed-off-by: Li Linchao <lilinchao@oschina.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Li Linchao committed
Jul 3, 2022 at 15:49 UTC
18337d406f170bebc303f1b29bd53019ee851a41
5 files changed
+132
-71
t/README
+55
@@ -547,6 +547,61 @@ This test harness library does the following things:
547
consistently when command line arguments --verbose (or -v),
548
--debug (or -d), and --immediate (or -i) is given.
549
550
+Recommended style
551
+-----------------
552
+Here are some recommented styles when writing test case.
553
+
554
+ - Keep test title the same line with test helper function itself.
555
+
556
+ Take test_expect_success helper for example, write it like:
557
+
558
+ test_expect_success 'test title' '
559
+ ... test body ...
560
+ '
561
+
562
+ Instead of:
563
+
564
+ test_expect_success \
565
+ 'test title' \
566
+ '... test body ...'
567
+
568
+
569
+ - End the line with a single quote.
570
+
571
+ - Indent the body of here-document, and use "<<-" instead of "<<"
572
+ to strip leading TABs used for indentation:
573
+
574
+ test_expect_success 'test something' '
575
+ cat >expect <<-\EOF &&
576
+ one
577
+ two
578
+ three
579
+ EOF
580
+ test_something > actual &&
581
+ test_cmp expect actual
582
+ '
583
+
584
+ Instead of:
585
+
586
+ test_expect_success 'test something' '
587
+ cat >expect <<\EOF &&
588
+ one
589
+ two
590
+ three
591
+ EOF
592
+ test_something > actual &&
593
+ test_cmp expect actual
594
+ '
595
+
596
+ - Quote or escape the EOF delimiter that begins a here-document if
597
+ there is no parameter and other expansion in it, to signal readers
598
+ that they can skim it more casually:
599
+
600
+ cmd <<-\EOF
601
+ literal here-document text without any expansion
602
+ EOF
603
+
604
+
605
Do's & don'ts
606
-------------
607
t/t3001-ls-files-others-exclude.sh
+21
-21
@@ -67,26 +67,26 @@ echo '!*.2
67
68
allignores='.gitignore one/.gitignore one/two/.gitignore'
69
70
-test_expect_success \
71
- 'git ls-files --others with various exclude options.' \
72
- 'git ls-files --others \
70
+test_expect_success 'git ls-files --others with various exclude options.' '
71
+ git ls-files --others \
72
--exclude=\*.6 \
73
--exclude-per-directory=.gitignore \
74
--exclude-from=.git/ignore \
76
- >output &&
77
- test_cmp expect output'
75
+ >output &&
76
+ test_cmp expect output
77
+'
78
79
# Test \r\n (MSDOS-like systems)
80
printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
81
82
-test_expect_success \
83
- 'git ls-files --others with \r\n line endings.' \
84
- 'git ls-files --others \
82
+test_expect_success 'git ls-files --others with \r\n line endings.' '
83
+ git ls-files --others \
84
--exclude=\*.6 \
85
--exclude-per-directory=.gitignore \
86
--exclude-from=.git/ignore \
88
- >output &&
89
- test_cmp expect output'
87
+ >output &&
88
+ test_cmp expect output
89
+'
90
91
test_expect_success 'setup skip-worktree gitignore' '
92
git add $allignores &&
@@ -94,14 +94,14 @@ test_expect_success 'setup skip-worktree gitignore' '
94
rm $allignores
95
'
96
97
-test_expect_success \
98
- 'git ls-files --others with various exclude options.' \
99
- 'git ls-files --others \
97
+test_expect_success 'git ls-files --others with various exclude options.' '
98
+ git ls-files --others \
99
--exclude=\*.6 \
100
--exclude-per-directory=.gitignore \
101
--exclude-from=.git/ignore \
103
- >output &&
104
- test_cmp expect output'
102
+ >output &&
103
+ test_cmp expect output
104
+'
105
106
test_expect_success !SANITIZE_LEAK 'restore gitignore' '
107
git checkout --ignore-skip-worktree-bits $allignores &&
@@ -283,12 +283,12 @@ test_expect_success 'pattern matches prefix completely' '
283
'
284
285
test_expect_success 'ls-files with "**" patterns' '
286
- cat <<\EOF >expect &&
287
-a.1
288
-one/a.1
289
-one/two/a.1
290
-three/a.1
291
-EOF
286
+ cat <<-\EOF >expect &&
287
+ a.1
288
+ one/a.1
289
+ one/two/a.1
290
+ three/a.1
291
+ EOF
292
git ls-files -o -i --exclude "**/a.1" >actual &&
293
test_cmp expect actual
294
'
t/t3002-ls-files-dashpath.sh
+46
-40
@@ -16,56 +16,62 @@ filesystem.
16
TEST_PASSES_SANITIZE_LEAK=true
17
. ./test-lib.sh
18
19
-test_expect_success \
20
- setup \
21
- 'echo frotz >path0 &&
19
+test_expect_success 'setup' '
20
+ echo frotz >path0 &&
21
echo frotz >./-foo &&
23
- echo frotz >./--'
22
+ echo frotz >./--
23
+'
24
25
-test_expect_success \
26
- 'git ls-files without path restriction.' \
27
- 'git ls-files --others >output &&
28
- test_cmp output - <<EOF
29
---
30
--foo
31
-output
32
-path0
33
-EOF
25
+test_expect_success 'git ls-files without path restriction.' '
26
+ test_when_finished "rm -f expect" &&
27
+ git ls-files --others >output &&
28
+ cat >expect <<-\EOF &&
29
+ --
30
+ -foo
31
+ output
32
+ path0
33
+ EOF
34
+ test_cmp output expect
35
'
36
36
-test_expect_success \
37
- 'git ls-files with path restriction.' \
38
- 'git ls-files --others path0 >output &&
39
- test_cmp output - <<EOF
40
-path0
41
-EOF
37
+test_expect_success 'git ls-files with path restriction.' '
38
+ test_when_finished "rm -f expect" &&
39
+ git ls-files --others path0 >output &&
40
+ cat >expect <<-\EOF &&
41
+ path0
42
+ EOF
43
+ test_cmp output expect
44
'
45
44
-test_expect_success \
45
- 'git ls-files with path restriction with --.' \
46
- 'git ls-files --others -- path0 >output &&
47
- test_cmp output - <<EOF
48
-path0
49
-EOF
46
+test_expect_success 'git ls-files with path restriction with --.' '
47
+ test_when_finished "rm -f expect" &&
48
+ git ls-files --others -- path0 >output &&
49
+ cat >expect <<-\EOF &&
50
+ path0
51
+ EOF
52
+ test_cmp output expect
53
'
54
52
-test_expect_success \
53
- 'git ls-files with path restriction with -- --.' \
54
- 'git ls-files --others -- -- >output &&
55
- test_cmp output - <<EOF
56
---
57
-EOF
55
+test_expect_success 'git ls-files with path restriction with -- --.' '
56
+ test_when_finished "rm -f expect" &&
57
+ git ls-files --others -- -- >output &&
58
+ cat >expect <<-\EOF &&
59
+ --
60
+ EOF
61
+ test_cmp output expect
62
'
63
60
-test_expect_success \
61
- 'git ls-files with no path restriction.' \
62
- 'git ls-files --others -- >output &&
63
- test_cmp output - <<EOF
64
---
65
--foo
66
-output
67
-path0
68
-EOF
64
+test_expect_success 'git ls-files with no path restriction.' '
65
+ test_when_finished "rm -f expect" &&
66
+ git ls-files --others -- >output &&
67
+ cat >expect <<-\EOF &&
68
+ --
69
+ -foo
70
+ output
71
+ path0
72
+ EOF
73
+ test_cmp output expect
74
+
75
'
76
77
test_done
t/t3020-ls-files-error-unmatch.sh
+6
-6
@@ -19,12 +19,12 @@ test_expect_success 'setup' '
19
git commit -m "add foo bar"
20
'
21
22
-test_expect_success \
23
- 'git ls-files --error-unmatch should fail with unmatched path.' \
24
- 'test_must_fail git ls-files --error-unmatch foo bar-does-not-match'
22
+test_expect_success 'git ls-files --error-unmatch should fail with unmatched path.' '
23
+ test_must_fail git ls-files --error-unmatch foo bar-does-not-match
24
+'
25
26
-test_expect_success \
27
- 'git ls-files --error-unmatch should succeed with matched paths.' \
28
- 'git ls-files --error-unmatch foo bar'
26
+test_expect_success 'git ls-files --error-unmatch should succeed with matched paths.' '
27
+ git ls-files --error-unmatch foo bar
28
+'
29
30
test_done
t/t3060-ls-files-with-tree.sh
+4
-4
@@ -10,7 +10,7 @@ a scenario known to trigger a crash with some versions of git.
10
'
11
. ./test-lib.sh
12
13
-test_expect_success setup '
13
+test_expect_success 'setup' '
14
15
# The bug we are exercising requires a fair number of entries
16
# in a sub-directory so that add_index_entry will trigger a
@@ -62,9 +62,9 @@ test_expect_success 'git ls-files --with-tree should succeed from subdir' '
62
)
63
'
64
65
-test_expect_success \
66
- 'git ls-files --with-tree should add entries from named tree.' \
67
- 'test_cmp expected output'
65
+test_expect_success 'git ls-files --with-tree should add entries from named tree.' '
66
+ test_cmp expected output
67
+'
68
69
test_expect_success 'no duplicates in --with-tree output' '
70
git ls-files --with-tree=HEAD >actual &&