t/chainlint: add chainlint "basic" test cases

The --chain-lint option uses heuristics and knowledge of shell syntax to detect broken &&-chains in subshells by pure textual inspection. The heuristics handle a range of stylistic variations in existing tests (evolved over the years), however, they are still best-guesses. As such, it is possible for future changes to accidentally break assumptions upon which the heuristics are based. Protect against this possibility by adding tests which check the linter itself for correctness. In addition to protecting against regressions, these tests help document (for humans) expected behavior, which is important since the linter's implementation language ('sed') does not necessarily lend itself to easy comprehension. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 11, 2018 at 02:46 UTC 5238710eb404cc81de511743811f92759cb7ca7b
16 files changed +206
t/chainlint/arithmetic-expansion.expect new
+9
@@ -0,0 +1,9 @@
1 +(
2 + foo &&
3 + bar=$((42 + 1)) &&
4 + baz
5 +>) &&
6 +(
7 +?!AMP?! bar=$((42 + 1))
8 + baz
9 +>)
t/chainlint/arithmetic-expansion.test new
+11
@@ -0,0 +1,11 @@
1 +(
2 + foo &&
3 +# LINT: closing ")" of $((...)) not misinterpreted as subshell-closing ")"
4 + bar=$((42 + 1)) &&
5 + baz
6 +) &&
7 +(
8 +# LINT: missing "&&" on $((...))
9 + bar=$((42 + 1))
10 + baz
11 +)
t/chainlint/broken-chain.expect new
+6
@@ -0,0 +1,6 @@
1 +(
2 + foo &&
3 +?!AMP?! bar
4 + baz &&
5 + wop
6 +>)
t/chainlint/broken-chain.test new
+8
@@ -0,0 +1,8 @@
1 +(
2 + foo &&
3 +# LINT: missing "&&" from 'bar'
4 + bar
5 + baz &&
6 +# LINT: final statement before closing ")" legitimately lacks "&&"
7 + wop
8 +)
t/chainlint/close-subshell.expect new
+25
@@ -0,0 +1,25 @@
1 +(
2 + foo
3 +>) &&
4 +(
5 + bar
6 +>) >out &&
7 +(
8 + baz
9 +>) 2>err &&
10 +(
11 + boo
12 +>) <input &&
13 +(
14 + bip
15 +>) | wuzzle &&
16 +(
17 + bop
18 +>) | fazz fozz &&
19 +(
20 + bup
21 +>) |
22 +fuzzle &&
23 +(
24 + yop
25 +>)
t/chainlint/close-subshell.test new
+27
@@ -0,0 +1,27 @@
1 +# LINT: closing ")" with various decorations ("&&", ">", "|", etc.)
2 +(
3 + foo
4 +) &&
5 +(
6 + bar
7 +) >out &&
8 +(
9 + baz
10 +) 2>err &&
11 +(
12 + boo
13 +) <input &&
14 +(
15 + bip
16 +) | wuzzle &&
17 +(
18 + bop
19 +) | fazz \
20 + fozz &&
21 +(
22 + bup
23 +) |
24 +fuzzle &&
25 +(
26 + yop
27 +)
t/chainlint/command-substitution.expect new
+9
@@ -0,0 +1,9 @@
1 +(
2 + foo &&
3 + bar=$(gobble) &&
4 + baz
5 +>) &&
6 +(
7 +?!AMP?! bar=$(gobble blocks)
8 + baz
9 +>)
t/chainlint/command-substitution.test new
+11
@@ -0,0 +1,11 @@
1 +(
2 + foo &&
3 +# LINT: closing ")" of $(...) not misinterpreted as subshell-closing ")"
4 + bar=$(gobble) &&
5 + baz
6 +) &&
7 +(
8 +# LINT: missing "&&" on $(...)
9 + bar=$(gobble blocks)
10 + baz
11 +)
t/chainlint/exit-subshell.expect new
+5
@@ -0,0 +1,5 @@
1 +(
2 + foo || exit 1
3 + bar &&
4 + baz
5 +>)
t/chainlint/exit-subshell.test new
+6
@@ -0,0 +1,6 @@
1 +(
2 +# LINT: "|| exit {n}" valid subshell escape without hurting &&-chain
3 + foo || exit 1
4 + bar &&
5 + baz
6 +)
t/chainlint/multi-line-string.expect new
+9
@@ -0,0 +1,9 @@
1 +(
2 + x=line 1 line 2 line 3" &&
3 +?!AMP?! y=line 1 line2'
4 + foobar
5 +>) &&
6 +(
7 + echo "there's nothing to see here" &&
8 + exit
9 +>)
t/chainlint/multi-line-string.test new
+15
@@ -0,0 +1,15 @@
1 +(
2 + x="line 1
3 + line 2
4 + line 3" &&
5 +# LINT: missing "&&" on assignment
6 + y='line 1
7 + line2'
8 + foobar
9 +) &&
10 +(
11 +# LINT: apostrophe (in a contraction) within string not misinterpreted as
12 +# LINT: starting multi-line single-quoted string
13 + echo "there's nothing to see here" &&
14 + exit
15 +)
t/chainlint/pipe.expect new
+8
@@ -0,0 +1,8 @@
1 +(
2 + foo |
3 + bar |
4 + baz &&
5 + fish |
6 +?!AMP?! cow
7 + sunder
8 +>)
t/chainlint/pipe.test new
+12
@@ -0,0 +1,12 @@
1 +(
2 +# LINT: no "&&" needed on line ending with "|"
3 + foo |
4 + bar |
5 + baz &&
6 +
7 +# LINT: final line of pipe sequence ('cow') lacking "&&"
8 + fish |
9 + cow
10 +
11 + sunder
12 +)
t/chainlint/semicolon.expect new
+20
@@ -0,0 +1,20 @@
1 +(
2 +?!AMP?!?!SEMI?! cat foo ; echo bar
3 +?!SEMI?! cat foo ; echo bar
4 +>) &&
5 +(
6 +?!SEMI?! cat foo ; echo bar &&
7 +?!SEMI?! cat foo ; echo bar
8 +>) &&
9 +(
10 + echo "foo; bar" &&
11 +?!SEMI?! cat foo; echo bar
12 +>) &&
13 +(
14 +?!SEMI?! foo;
15 +>) &&
16 +(
17 +cd foo &&
18 + for i in a b c; do
19 +?!SEMI?! echo;
20 +> done)
t/chainlint/semicolon.test new
+25
@@ -0,0 +1,25 @@
1 +(
2 +# LINT: missing internal "&&" and ending "&&"
3 + cat foo ; echo bar
4 +# LINT: final statement before ")" only missing internal "&&"
5 + cat foo ; echo bar
6 +) &&
7 +(
8 +# LINT: missing internal "&&"
9 + cat foo ; echo bar &&
10 + cat foo ; echo bar
11 +) &&
12 +(
13 +# LINT: not fooled by semicolon in string
14 + echo "foo; bar" &&
15 + cat foo; echo bar
16 +) &&
17 +(
18 +# LINT: unnecessary terminating semicolon
19 + foo;
20 +) &&
21 +(cd foo &&
22 + for i in a b c; do
23 +# LINT: unnecessary terminating semicolon
24 + echo;
25 + done)