Skip to content

Commit e0ada15

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e57e0de commit e0ada15

16 files changed

+206
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(
2+
foo &&
3+
?!AMP?! bar
4+
baz &&
5+
wop
6+
>)

t/chainlint/broken-chain.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(
2+
foo || exit 1
3+
bar &&
4+
baz
5+
>)

t/chainlint/exit-subshell.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(
2+
foo |
3+
bar |
4+
baz &&
5+
fish |
6+
?!AMP?! cow
7+
sunder
8+
>)

t/chainlint/pipe.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

0 commit comments

Comments
 (0)