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
This commit is contained in:
@ -61,6 +61,22 @@
|
|||||||
# "else", and "fi" in if-then-else likewise must not end with "&&", thus
|
# "else", and "fi" in if-then-else likewise must not end with "&&", thus
|
||||||
# receives similar treatment.
|
# receives similar treatment.
|
||||||
#
|
#
|
||||||
|
# Swallowing here-docs with arbitrary tags requires a bit of finesse. When a
|
||||||
|
# line such as "cat <<EOF >out" is seen, the here-doc tag is moved to the front
|
||||||
|
# of the line enclosed in angle brackets as a sentinel, giving "<EOF>cat >out".
|
||||||
|
# As each subsequent line is read, it is appended to the target line and a
|
||||||
|
# (whitespace-loose) back-reference match /^<(.*)>\n\1$/ is attempted to see if
|
||||||
|
# the content inside "<...>" matches the entirety of the newly-read line. For
|
||||||
|
# instance, if the next line read is "some data", when concatenated with the
|
||||||
|
# target line, it becomes "<EOF>cat >out\nsome data", and a match is attempted
|
||||||
|
# to see if "EOF" matches "some data". Since it doesn't, the next line is
|
||||||
|
# attempted. When a line consisting of only "EOF" (and possible whitespace) is
|
||||||
|
# encountered, it is appended to the target line giving "<EOF>cat >out\nEOF",
|
||||||
|
# in which case the "EOF" inside "<...>" does match the text following the
|
||||||
|
# newline, thus the closing here-doc tag has been found. The closing tag line
|
||||||
|
# and the "<...>" prefix on the target line are then discarded, leaving just
|
||||||
|
# the target line "cat >out".
|
||||||
|
#
|
||||||
# To facilitate regression testing (and manual debugging), a ">" annotation is
|
# To facilitate regression testing (and manual debugging), a ">" annotation is
|
||||||
# applied to the line containing ")" which closes a subshell, ">>" to a line
|
# applied to the line containing ")" which closes a subshell, ">>" to a line
|
||||||
# closing a nested subshell, and ">>>" to a line closing both at once. This
|
# closing a nested subshell, and ">>>" to a line closing both at once. This
|
||||||
@ -78,14 +94,17 @@
|
|||||||
|
|
||||||
# here-doc -- swallow it to avoid false hits within its body (but keep the
|
# here-doc -- swallow it to avoid false hits within its body (but keep the
|
||||||
# command to which it was attached)
|
# command to which it was attached)
|
||||||
/<<[ ]*[-\\]*EOF[ ]*/ {
|
/<<[ ]*[-\\']*[A-Za-z0-9_]/ {
|
||||||
s/[ ]*<<[ ]*[-\\]*EOF//
|
s/^\(.*\)<<[ ]*[-\\']*\([A-Za-z0-9_][A-Za-z0-9_]*\)'*/<\2>\1<</
|
||||||
h
|
s/[ ]*<<//
|
||||||
:hereslurp
|
:hereslurp
|
||||||
N
|
N
|
||||||
s/.*\n//
|
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
|
||||||
/^[ ]*EOF[ ]*$/!bhereslurp
|
s/\n.*$//
|
||||||
x
|
bhereslurp
|
||||||
|
}
|
||||||
|
s/^<[^>]*>//
|
||||||
|
s/\n.*$//
|
||||||
}
|
}
|
||||||
|
|
||||||
# one-liner "(...) &&"
|
# one-liner "(...) &&"
|
||||||
@ -132,16 +151,15 @@ s/.*\n//
|
|||||||
:slurp
|
:slurp
|
||||||
# incomplete line "...\"
|
# incomplete line "...\"
|
||||||
/\\$/bincomplete
|
/\\$/bincomplete
|
||||||
# multi-line quoted string "...\n..."
|
# multi-line quoted string "...\n..."?
|
||||||
/^[^"]*"[^"]*$/bdqstring
|
/"/bdqstring
|
||||||
# multi-line quoted string '...\n...' (but not contraction in string "it's so")
|
# multi-line quoted string '...\n...'? (but not contraction in string "it's")
|
||||||
/^[^']*'[^']*$/{
|
/'/{
|
||||||
/"[^'"]*'[^'"]*"/!bsqstring
|
/"[^'"]*'[^'"]*"/!bsqstring
|
||||||
}
|
}
|
||||||
|
:folded
|
||||||
# here-doc -- swallow it
|
# here-doc -- swallow it
|
||||||
/<<[ ]*[-\\]*EOF/bheredoc
|
/<<[ ]*[-\\']*[A-Za-z0-9_]/bheredoc
|
||||||
/<<[ ]*[-\\]*EOT/bheredoc
|
|
||||||
/<<[ ]*[-\\]*INPUT_END/bheredoc
|
|
||||||
# comment or empty line -- discard since final non-comment, non-empty line
|
# comment or empty line -- discard since final non-comment, non-empty line
|
||||||
# before closing ")", "done", "elsif", "else", or "fi" will need to be
|
# before closing ")", "done", "elsif", "else", or "fi" will need to be
|
||||||
# re-visited to drop "suspect" marking since final line of those constructs
|
# re-visited to drop "suspect" marking since final line of those constructs
|
||||||
@ -199,7 +217,7 @@ s/.*\n//
|
|||||||
# "$(...)" -- command substitution; not closing ")"
|
# "$(...)" -- command substitution; not closing ")"
|
||||||
/\$([^)][^)]*)[^)]*$/bcheckchain
|
/\$([^)][^)]*)[^)]*$/bcheckchain
|
||||||
# multi-line "$(...\n...)" -- command substitution; treat as nested subshell
|
# multi-line "$(...\n...)" -- command substitution; treat as nested subshell
|
||||||
/\$([ ]*$/bnest
|
/\$([^)]*$/bnest
|
||||||
# "=(...)" -- Bash array assignment; not closing ")"
|
# "=(...)" -- Bash array assignment; not closing ")"
|
||||||
/=(/bcheckchain
|
/=(/bcheckchain
|
||||||
# closing "...) &&"
|
# closing "...) &&"
|
||||||
@ -232,42 +250,48 @@ N
|
|||||||
s/\\\n//
|
s/\\\n//
|
||||||
bslurp
|
bslurp
|
||||||
|
|
||||||
# found multi-line double-quoted string "...\n..." -- slurp until end of string
|
# check for multi-line double-quoted string "...\n..." -- fold to one line
|
||||||
:dqstring
|
:dqstring
|
||||||
s/"//g
|
# remove all quote pairs
|
||||||
|
s/"\([^"]*\)"/@!\1@!/g
|
||||||
|
# done if no dangling quote
|
||||||
|
/"/!bdqdone
|
||||||
|
# otherwise, slurp next line and try again
|
||||||
N
|
N
|
||||||
s/\n//
|
s/\n//
|
||||||
/"/!bdqstring
|
bdqstring
|
||||||
bcheckchain
|
:dqdone
|
||||||
|
s/@!/"/g
|
||||||
|
bfolded
|
||||||
|
|
||||||
# found multi-line single-quoted string '...\n...' -- slurp until end of string
|
# check for multi-line single-quoted string '...\n...' -- fold to one line
|
||||||
:sqstring
|
:sqstring
|
||||||
s/'//g
|
# remove all quote pairs
|
||||||
|
s/'\([^']*\)'/@!\1@!/g
|
||||||
|
# done if no dangling quote
|
||||||
|
/'/!bsqdone
|
||||||
|
# otherwise, slurp next line and try again
|
||||||
N
|
N
|
||||||
s/\n//
|
s/\n//
|
||||||
/'/!bsqstring
|
bsqstring
|
||||||
bcheckchain
|
:sqdone
|
||||||
|
s/@!/'/g
|
||||||
|
bfolded
|
||||||
|
|
||||||
# found here-doc -- swallow it to avoid false hits within its body (but keep
|
# found here-doc -- swallow it to avoid false hits within its body (but keep
|
||||||
# the command to which it was attached); take care to handle here-docs nested
|
# the command to which it was attached)
|
||||||
# within here-docs by only recognizing closing tag matching outer here-doc
|
|
||||||
# opening tag
|
|
||||||
:heredoc
|
:heredoc
|
||||||
/EOF/{ s/[ ]*<<[ ]*[-\\]*EOF//; s/^/EOF/; }
|
s/^\(.*\)<<[ ]*[-\\']*\([A-Za-z0-9_][A-Za-z0-9_]*\)'*/<\2>\1<</
|
||||||
/EOT/{ s/[ ]*<<[ ]*[-\\]*EOT//; s/^/EOT/; }
|
s/[ ]*<<//
|
||||||
/INPUT_END/{ s/[ ]*<<[ ]*[-\\]*INPUT_END//; s/^/INPUT_END/; }
|
|
||||||
:hereslurpsub
|
:hereslurpsub
|
||||||
N
|
N
|
||||||
/^EOF.*\n[ ]*EOF[ ]*$/bhereclose
|
/^<\([^>]*\)>.*\n[ ]*\1[ ]*$/!{
|
||||||
/^EOT.*\n[ ]*EOT[ ]*$/bhereclose
|
s/\n.*$//
|
||||||
/^INPUT_END.*\n[ ]*INPUT_END[ ]*$/bhereclose
|
bhereslurpsub
|
||||||
bhereslurpsub
|
}
|
||||||
:hereclose
|
s/^<[^>]*>//
|
||||||
s/^EOF//
|
|
||||||
s/^EOT//
|
|
||||||
s/^INPUT_END//
|
|
||||||
s/\n.*$//
|
s/\n.*$//
|
||||||
bcheckchain
|
bfolded
|
||||||
|
|
||||||
# found "case ... in" -- pass through untouched
|
# found "case ... in" -- pass through untouched
|
||||||
:case
|
:case
|
||||||
|
2
t/chainlint/here-doc-close-subshell.expect
Normal file
2
t/chainlint/here-doc-close-subshell.expect
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
(
|
||||||
|
> cat)
|
5
t/chainlint/here-doc-close-subshell.test
Normal file
5
t/chainlint/here-doc-close-subshell.test
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
(
|
||||||
|
# LINT: line contains here-doc and closes nested subshell
|
||||||
|
cat <<-\INPUT)
|
||||||
|
fizz
|
||||||
|
INPUT
|
5
t/chainlint/here-doc-multi-line-command-subst.expect
Normal file
5
t/chainlint/here-doc-multi-line-command-subst.expect
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
(
|
||||||
|
x=$(bobble &&
|
||||||
|
?!AMP?!>> wiffle)
|
||||||
|
echo $x
|
||||||
|
>)
|
9
t/chainlint/here-doc-multi-line-command-subst.test
Normal file
9
t/chainlint/here-doc-multi-line-command-subst.test
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
(
|
||||||
|
# LINT: line contains here-doc and opens multi-line $(...)
|
||||||
|
x=$(bobble <<-\END &&
|
||||||
|
fossil
|
||||||
|
vegetable
|
||||||
|
END
|
||||||
|
wiffle)
|
||||||
|
echo $x
|
||||||
|
)
|
4
t/chainlint/here-doc-multi-line-string.expect
Normal file
4
t/chainlint/here-doc-multi-line-string.expect
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
(
|
||||||
|
?!AMP?! cat && echo "multi-line string"
|
||||||
|
bap
|
||||||
|
>)
|
8
t/chainlint/here-doc-multi-line-string.test
Normal file
8
t/chainlint/here-doc-multi-line-string.test
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
(
|
||||||
|
# LINT: line contains here-doc and opens multi-line string
|
||||||
|
cat <<-\TXT && echo "multi-line
|
||||||
|
string"
|
||||||
|
fizzle
|
||||||
|
TXT
|
||||||
|
bap
|
||||||
|
)
|
@ -1,3 +1,7 @@
|
|||||||
boodle wobba gorgo snoot wafta snurb &&
|
boodle wobba gorgo snoot wafta snurb &&
|
||||||
|
|
||||||
|
cat >foo &&
|
||||||
|
|
||||||
|
cat >bar &&
|
||||||
|
|
||||||
horticulture
|
horticulture
|
||||||
|
@ -7,6 +7,20 @@ quoth the raven,
|
|||||||
nevermore...
|
nevermore...
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
# LINT: swallow here-doc with arbitrary tag
|
||||||
|
cat <<-Arbitrary_Tag_42 >foo &&
|
||||||
|
snoz
|
||||||
|
boz
|
||||||
|
woz
|
||||||
|
Arbitrary_Tag_42
|
||||||
|
|
||||||
|
# LINT: swallow 'quoted' here-doc
|
||||||
|
cat <<'FUMP' >bar &&
|
||||||
|
snoz
|
||||||
|
boz
|
||||||
|
woz
|
||||||
|
FUMP
|
||||||
|
|
||||||
# LINT: swallow here-doc (EOF is last line of test)
|
# LINT: swallow here-doc (EOF is last line of test)
|
||||||
horticulture <<\EOF
|
horticulture <<\EOF
|
||||||
gomez
|
gomez
|
||||||
|
@ -6,4 +6,13 @@
|
|||||||
>> ) &&
|
>> ) &&
|
||||||
echo ok
|
echo ok
|
||||||
>) |
|
>) |
|
||||||
sort
|
sort &&
|
||||||
|
(
|
||||||
|
bar &&
|
||||||
|
x=$(echo bar |
|
||||||
|
cat
|
||||||
|
>> ) &&
|
||||||
|
y=$(echo baz |
|
||||||
|
>> fip) &&
|
||||||
|
echo fail
|
||||||
|
>)
|
||||||
|
@ -6,4 +6,13 @@
|
|||||||
) &&
|
) &&
|
||||||
echo ok
|
echo ok
|
||||||
) |
|
) |
|
||||||
sort
|
sort &&
|
||||||
|
(
|
||||||
|
bar &&
|
||||||
|
x=$(echo bar |
|
||||||
|
cat
|
||||||
|
) &&
|
||||||
|
y=$(echo baz |
|
||||||
|
fip) &&
|
||||||
|
echo fail
|
||||||
|
)
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
(
|
(
|
||||||
x=line 1 line 2 line 3" &&
|
x="line 1 line 2 line 3" &&
|
||||||
?!AMP?! y=line 1 line2'
|
?!AMP?! y='line 1 line2'
|
||||||
foobar
|
foobar
|
||||||
>) &&
|
>) &&
|
||||||
(
|
(
|
||||||
echo "there's nothing to see here" &&
|
echo "there's nothing to see here" &&
|
||||||
exit
|
exit
|
||||||
|
>) &&
|
||||||
|
(
|
||||||
|
echo "xyz" "abc def ghi" &&
|
||||||
|
echo 'xyz' 'abc def ghi' &&
|
||||||
|
echo 'xyz' "abc def ghi" &&
|
||||||
|
barfoo
|
||||||
>)
|
>)
|
||||||
|
@ -12,4 +12,16 @@
|
|||||||
# LINT: starting multi-line single-quoted string
|
# LINT: starting multi-line single-quoted string
|
||||||
echo "there's nothing to see here" &&
|
echo "there's nothing to see here" &&
|
||||||
exit
|
exit
|
||||||
|
) &&
|
||||||
|
(
|
||||||
|
echo "xyz" "abc
|
||||||
|
def
|
||||||
|
ghi" &&
|
||||||
|
echo 'xyz' 'abc
|
||||||
|
def
|
||||||
|
ghi' &&
|
||||||
|
echo 'xyz' "abc
|
||||||
|
def
|
||||||
|
ghi" &&
|
||||||
|
barfoo
|
||||||
)
|
)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
cat >foop &&
|
||||||
|
|
||||||
(
|
(
|
||||||
cat &&
|
cat &&
|
||||||
?!AMP?! cat
|
?!AMP?! cat
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# LINT: inner "EOF" not misintrepreted as closing ARBITRARY here-doc
|
||||||
|
cat <<ARBITRARY >foop &&
|
||||||
|
naddle
|
||||||
|
fub <<EOF
|
||||||
|
nozzle
|
||||||
|
noodle
|
||||||
|
EOF
|
||||||
|
formp
|
||||||
|
ARBITRARY
|
||||||
|
|
||||||
(
|
(
|
||||||
# LINT: inner "EOF" not misintrepreted as closing INPUT_END here-doc
|
# LINT: inner "EOF" not misintrepreted as closing INPUT_END here-doc
|
||||||
cat <<-\INPUT_END &&
|
cat <<-\INPUT_END &&
|
||||||
|
@ -2,4 +2,9 @@
|
|||||||
echo wobba gorgo snoot wafta snurb &&
|
echo wobba gorgo snoot wafta snurb &&
|
||||||
?!AMP?! cat >bip
|
?!AMP?! cat >bip
|
||||||
echo >bop
|
echo >bop
|
||||||
|
>) &&
|
||||||
|
(
|
||||||
|
cat >bup &&
|
||||||
|
cat >bup2 &&
|
||||||
|
meep
|
||||||
>)
|
>)
|
||||||
|
@ -20,4 +20,16 @@
|
|||||||
wednesday
|
wednesday
|
||||||
pugsly
|
pugsly
|
||||||
EOF
|
EOF
|
||||||
|
) &&
|
||||||
|
(
|
||||||
|
# LINT: swallow here-doc with arbitrary tag
|
||||||
|
cat <<-\ARBITRARY >bup &&
|
||||||
|
glink
|
||||||
|
FIZZ
|
||||||
|
ARBITRARY
|
||||||
|
cat <<-'ARBITRARY2' >bup2 &&
|
||||||
|
glink
|
||||||
|
FIZZ
|
||||||
|
ARBITRARY2
|
||||||
|
meep
|
||||||
)
|
)
|
||||||
|
10
t/chainlint/t7900-subtree.expect
Normal file
10
t/chainlint/t7900-subtree.expect
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
(
|
||||||
|
chks="sub1sub2sub3sub4" &&
|
||||||
|
chks_sub=$(cat | sed 's,^,sub dir/,'
|
||||||
|
>>) &&
|
||||||
|
chkms="main-sub1main-sub2main-sub3main-sub4" &&
|
||||||
|
chkms_sub=$(cat | sed 's,^,sub dir/,'
|
||||||
|
>>) &&
|
||||||
|
subfiles=$(git ls-files) &&
|
||||||
|
check_equal "$subfiles" "$chkms$chks"
|
||||||
|
>)
|
22
t/chainlint/t7900-subtree.test
Normal file
22
t/chainlint/t7900-subtree.test
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
(
|
||||||
|
chks="sub1
|
||||||
|
sub2
|
||||||
|
sub3
|
||||||
|
sub4" &&
|
||||||
|
chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
|
||||||
|
$chks
|
||||||
|
TXT
|
||||||
|
) &&
|
||||||
|
chkms="main-sub1
|
||||||
|
main-sub2
|
||||||
|
main-sub3
|
||||||
|
main-sub4" &&
|
||||||
|
chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
|
||||||
|
$chkms
|
||||||
|
TXT
|
||||||
|
) &&
|
||||||
|
|
||||||
|
subfiles=$(git ls-files) &&
|
||||||
|
check_equal "$subfiles" "$chkms
|
||||||
|
$chks"
|
||||||
|
)
|
Reference in New Issue
Block a user