Skip to content

Commit ace1f99

Browse files
committed
Merge branch 'es/chain-lint-more'
Improve built-in facility to catch broken &&-chain in the tests. * es/chain-lint-more: chainlint: add test of pathological case which triggered false positive chainlint: recognize multi-line quoted strings more robustly chainlint: let here-doc and multi-line string commence on same line chainlint: recognize multi-line $(...) when command cuddled with "$(" chainlint: match 'quoted' here-doc tags chainlint: match arbitrary here-docs tags rather than hard-coded names
2 parents a15bfa5 + 4f69176 commit ace1f99

19 files changed

+213
-41
lines changed

t/chainlint.sed

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@
6161
# "else", and "fi" in if-then-else likewise must not end with "&&", thus
6262
# receives similar treatment.
6363
#
64+
# Swallowing here-docs with arbitrary tags requires a bit of finesse. When a
65+
# line such as "cat <<EOF >out" is seen, the here-doc tag is moved to the front
66+
# of the line enclosed in angle brackets as a sentinel, giving "<EOF>cat >out".
67+
# As each subsequent line is read, it is appended to the target line and a
68+
# (whitespace-loose) back-reference match /^<(.*)>\n\1$/ is attempted to see if
69+
# the content inside "<...>" matches the entirety of the newly-read line. For
70+
# instance, if the next line read is "some data", when concatenated with the
71+
# target line, it becomes "<EOF>cat >out\nsome data", and a match is attempted
72+
# to see if "EOF" matches "some data". Since it doesn't, the next line is
73+
# attempted. When a line consisting of only "EOF" (and possible whitespace) is
74+
# encountered, it is appended to the target line giving "<EOF>cat >out\nEOF",
75+
# in which case the "EOF" inside "<...>" does match the text following the
76+
# newline, thus the closing here-doc tag has been found. The closing tag line
77+
# and the "<...>" prefix on the target line are then discarded, leaving just
78+
# the target line "cat >out".
79+
#
6480
# To facilitate regression testing (and manual debugging), a ">" annotation is
6581
# applied to the line containing ")" which closes a subshell, ">>" to a line
6682
# closing a nested subshell, and ">>>" to a line closing both at once. This
@@ -78,14 +94,17 @@
7894

7995
# here-doc -- swallow it to avoid false hits within its body (but keep the
8096
# command to which it was attached)
81-
/<<[ ]*[-\\]*EOF[ ]*/ {
82-
s/[ ]*<<[ ]*[-\\]*EOF//
83-
h
97+
/<<[ ]*[-\\']*[A-Za-z0-9_]/ {
98+
s/^\(.*\)<<[ ]*[-\\']*\([A-Za-z0-9_][A-Za-z0-9_]*\)'*/<\2>\1<</
99+
s/[ ]*<<//
84100
:hereslurp
85101
N
86-
s/.*\n//
87-
/^[ ]*EOF[ ]*$/!bhereslurp
88-
x
102+
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
103+
s/\n.*$//
104+
bhereslurp
105+
}
106+
s/^<[^>]*>//
107+
s/\n.*$//
89108
}
90109

91110
# one-liner "(...) &&"
@@ -132,16 +151,15 @@ s/.*\n//
132151
:slurp
133152
# incomplete line "...\"
134153
/\\$/bincomplete
135-
# multi-line quoted string "...\n..."
136-
/^[^"]*"[^"]*$/bdqstring
137-
# multi-line quoted string '...\n...' (but not contraction in string "it's so")
138-
/^[^']*'[^']*$/{
154+
# multi-line quoted string "...\n..."?
155+
/"/bdqstring
156+
# multi-line quoted string '...\n...'? (but not contraction in string "it's")
157+
/'/{
139158
/"[^'"]*'[^'"]*"/!bsqstring
140159
}
160+
:folded
141161
# here-doc -- swallow it
142-
/<<[ ]*[-\\]*EOF/bheredoc
143-
/<<[ ]*[-\\]*EOT/bheredoc
144-
/<<[ ]*[-\\]*INPUT_END/bheredoc
162+
/<<[ ]*[-\\']*[A-Za-z0-9_]/bheredoc
145163
# comment or empty line -- discard since final non-comment, non-empty line
146164
# before closing ")", "done", "elsif", "else", or "fi" will need to be
147165
# re-visited to drop "suspect" marking since final line of those constructs
@@ -199,7 +217,7 @@ s/.*\n//
199217
# "$(...)" -- command substitution; not closing ")"
200218
/\$([^)][^)]*)[^)]*$/bcheckchain
201219
# multi-line "$(...\n...)" -- command substitution; treat as nested subshell
202-
/\$([ ]*$/bnest
220+
/\$([^)]*$/bnest
203221
# "=(...)" -- Bash array assignment; not closing ")"
204222
/=(/bcheckchain
205223
# closing "...) &&"
@@ -232,42 +250,48 @@ N
232250
s/\\\n//
233251
bslurp
234252

235-
# found multi-line double-quoted string "...\n..." -- slurp until end of string
253+
# check for multi-line double-quoted string "...\n..." -- fold to one line
236254
:dqstring
237-
s/"//g
255+
# remove all quote pairs
256+
s/"\([^"]*\)"/@!\1@!/g
257+
# done if no dangling quote
258+
/"/!bdqdone
259+
# otherwise, slurp next line and try again
238260
N
239261
s/\n//
240-
/"/!bdqstring
241-
bcheckchain
262+
bdqstring
263+
:dqdone
264+
s/@!/"/g
265+
bfolded
242266

243-
# found multi-line single-quoted string '...\n...' -- slurp until end of string
267+
# check for multi-line single-quoted string '...\n...' -- fold to one line
244268
:sqstring
245-
s/'//g
269+
# remove all quote pairs
270+
s/'\([^']*\)'/@!\1@!/g
271+
# done if no dangling quote
272+
/'/!bsqdone
273+
# otherwise, slurp next line and try again
246274
N
247275
s/\n//
248-
/'/!bsqstring
249-
bcheckchain
276+
bsqstring
277+
:sqdone
278+
s/@!/'/g
279+
bfolded
250280

251281
# found here-doc -- swallow it to avoid false hits within its body (but keep
252-
# the command to which it was attached); take care to handle here-docs nested
253-
# within here-docs by only recognizing closing tag matching outer here-doc
254-
# opening tag
282+
# the command to which it was attached)
255283
:heredoc
256-
/EOF/{ s/[ ]*<<[ ]*[-\\]*EOF//; s/^/EOF/; }
257-
/EOT/{ s/[ ]*<<[ ]*[-\\]*EOT//; s/^/EOT/; }
258-
/INPUT_END/{ s/[ ]*<<[ ]*[-\\]*INPUT_END//; s/^/INPUT_END/; }
284+
s/^\(.*\)<<[ ]*[-\\']*\([A-Za-z0-9_][A-Za-z0-9_]*\)'*/<\2>\1<</
285+
s/[ ]*<<//
259286
:hereslurpsub
260287
N
261-
/^EOF.*\n[ ]*EOF[ ]*$/bhereclose
262-
/^EOT.*\n[ ]*EOT[ ]*$/bhereclose
263-
/^INPUT_END.*\n[ ]*INPUT_END[ ]*$/bhereclose
264-
bhereslurpsub
265-
:hereclose
266-
s/^EOF//
267-
s/^EOT//
268-
s/^INPUT_END//
288+
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
289+
s/\n.*$//
290+
bhereslurpsub
291+
}
292+
s/^<[^>]*>//
269293
s/\n.*$//
270-
bcheckchain
294+
bfolded
271295

272296
# found "case ... in" -- pass through untouched
273297
:case
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(
2+
> cat)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(
2+
# LINT: line contains here-doc and closes nested subshell
3+
cat <<-\INPUT)
4+
fizz
5+
INPUT
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(
2+
x=$(bobble &&
3+
?!AMP?!>> wiffle)
4+
echo $x
5+
>)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(
2+
# LINT: line contains here-doc and opens multi-line $(...)
3+
x=$(bobble <<-\END &&
4+
fossil
5+
vegetable
6+
END
7+
wiffle)
8+
echo $x
9+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(
2+
?!AMP?! cat && echo "multi-line string"
3+
bap
4+
>)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(
2+
# LINT: line contains here-doc and opens multi-line string
3+
cat <<-\TXT && echo "multi-line
4+
string"
5+
fizzle
6+
TXT
7+
bap
8+
)

t/chainlint/here-doc.expect

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
boodle wobba gorgo snoot wafta snurb &&
22

3+
cat >foo &&
4+
5+
cat >bar &&
6+
37
horticulture

t/chainlint/here-doc.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ quoth the raven,
77
nevermore...
88
EOF
99

10+
# LINT: swallow here-doc with arbitrary tag
11+
cat <<-Arbitrary_Tag_42 >foo &&
12+
snoz
13+
boz
14+
woz
15+
Arbitrary_Tag_42
16+
17+
# LINT: swallow 'quoted' here-doc
18+
cat <<'FUMP' >bar &&
19+
snoz
20+
boz
21+
woz
22+
FUMP
23+
1024
# LINT: swallow here-doc (EOF is last line of test)
1125
horticulture <<\EOF
1226
gomez

t/chainlint/multi-line-nested-command-substitution.expect

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
>> ) &&
77
echo ok
88
>) |
9-
sort
9+
sort &&
10+
(
11+
bar &&
12+
x=$(echo bar |
13+
cat
14+
>> ) &&
15+
y=$(echo baz |
16+
>> fip) &&
17+
echo fail
18+
>)

t/chainlint/multi-line-nested-command-substitution.test

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
) &&
77
echo ok
88
) |
9-
sort
9+
sort &&
10+
(
11+
bar &&
12+
x=$(echo bar |
13+
cat
14+
) &&
15+
y=$(echo baz |
16+
fip) &&
17+
echo fail
18+
)

t/chainlint/multi-line-string.expect

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
(
2-
x=line 1 line 2 line 3" &&
3-
?!AMP?! y=line 1 line2'
2+
x="line 1 line 2 line 3" &&
3+
?!AMP?! y='line 1 line2'
44
foobar
55
>) &&
66
(
77
echo "there's nothing to see here" &&
88
exit
9+
>) &&
10+
(
11+
echo "xyz" "abc def ghi" &&
12+
echo 'xyz' 'abc def ghi' &&
13+
echo 'xyz' "abc def ghi" &&
14+
barfoo
915
>)

t/chainlint/multi-line-string.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@
1212
# LINT: starting multi-line single-quoted string
1313
echo "there's nothing to see here" &&
1414
exit
15+
) &&
16+
(
17+
echo "xyz" "abc
18+
def
19+
ghi" &&
20+
echo 'xyz' 'abc
21+
def
22+
ghi' &&
23+
echo 'xyz' "abc
24+
def
25+
ghi" &&
26+
barfoo
1527
)

t/chainlint/nested-here-doc.expect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cat >foop &&
2+
13
(
24
cat &&
35
?!AMP?! cat

t/chainlint/nested-here-doc.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# LINT: inner "EOF" not misintrepreted as closing ARBITRARY here-doc
2+
cat <<ARBITRARY >foop &&
3+
naddle
4+
fub <<EOF
5+
nozzle
6+
noodle
7+
EOF
8+
formp
9+
ARBITRARY
10+
111
(
212
# LINT: inner "EOF" not misintrepreted as closing INPUT_END here-doc
313
cat <<-\INPUT_END &&

t/chainlint/subshell-here-doc.expect

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
echo wobba gorgo snoot wafta snurb &&
33
?!AMP?! cat >bip
44
echo >bop
5+
>) &&
6+
(
7+
cat >bup &&
8+
cat >bup2 &&
9+
meep
510
>)

t/chainlint/subshell-here-doc.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@
2020
wednesday
2121
pugsly
2222
EOF
23+
) &&
24+
(
25+
# LINT: swallow here-doc with arbitrary tag
26+
cat <<-\ARBITRARY >bup &&
27+
glink
28+
FIZZ
29+
ARBITRARY
30+
cat <<-'ARBITRARY2' >bup2 &&
31+
glink
32+
FIZZ
33+
ARBITRARY2
34+
meep
2335
)

t/chainlint/t7900-subtree.expect

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(
2+
chks="sub1sub2sub3sub4" &&
3+
chks_sub=$(cat | sed 's,^,sub dir/,'
4+
>>) &&
5+
chkms="main-sub1main-sub2main-sub3main-sub4" &&
6+
chkms_sub=$(cat | sed 's,^,sub dir/,'
7+
>>) &&
8+
subfiles=$(git ls-files) &&
9+
check_equal "$subfiles" "$chkms$chks"
10+
>)

t/chainlint/t7900-subtree.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(
2+
chks="sub1
3+
sub2
4+
sub3
5+
sub4" &&
6+
chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
7+
$chks
8+
TXT
9+
) &&
10+
chkms="main-sub1
11+
main-sub2
12+
main-sub3
13+
main-sub4" &&
14+
chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
15+
$chkms
16+
TXT
17+
) &&
18+
19+
subfiles=$(git ls-files) &&
20+
check_equal "$subfiles" "$chkms
21+
$chks"
22+
)

0 commit comments

Comments
 (0)