t/chainlint: add chainlint "nested subshell" test cases
authorEric Sunshine <sunshine@sunshineco.com>
Wed, 11 Jul 2018 06:46:38 +0000 (02:46 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Jul 2018 16:15:14 +0000 (09:15 -0700)
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>
12 files changed:
t/chainlint/block.expect [new file with mode: 0644]
t/chainlint/block.test [new file with mode: 0644]
t/chainlint/multi-line-nested-command-substitution.expect [new file with mode: 0644]
t/chainlint/multi-line-nested-command-substitution.test [new file with mode: 0644]
t/chainlint/nested-cuddled-subshell.expect [new file with mode: 0644]
t/chainlint/nested-cuddled-subshell.test [new file with mode: 0644]
t/chainlint/nested-here-doc.expect [new file with mode: 0644]
t/chainlint/nested-here-doc.test [new file with mode: 0644]
t/chainlint/nested-subshell-comment.expect [new file with mode: 0644]
t/chainlint/nested-subshell-comment.test [new file with mode: 0644]
t/chainlint/nested-subshell.expect [new file with mode: 0644]
t/chainlint/nested-subshell.test [new file with mode: 0644]
diff --git a/t/chainlint/block.expect b/t/chainlint/block.expect
new file mode 100644 (file)
index 0000000..fed7e89
--- /dev/null
@@ -0,0 +1,12 @@
+(
+       foo &&
+       {
+               echo a
+               echo b
+       } &&
+       bar &&
+       {
+               echo c
+?!AMP?!        }
+       baz
+>)
diff --git a/t/chainlint/block.test b/t/chainlint/block.test
new file mode 100644 (file)
index 0000000..d859151
--- /dev/null
@@ -0,0 +1,15 @@
+(
+# LINT: missing "&&" in block not currently detected (for consistency with
+# LINT: --chain-lint at top level and to provide escape hatch if needed)
+       foo &&
+       {
+               echo a
+               echo b
+       } &&
+       bar &&
+# LINT: missing "&&" at closing "}"
+       {
+               echo c
+       }
+       baz
+)
diff --git a/t/chainlint/multi-line-nested-command-substitution.expect b/t/chainlint/multi-line-nested-command-substitution.expect
new file mode 100644 (file)
index 0000000..19c023b
--- /dev/null
@@ -0,0 +1,9 @@
+(
+       foo &&
+       x=$(
+               echo bar |
+               cat
+>>     ) &&
+       echo ok
+>) |
+sort
diff --git a/t/chainlint/multi-line-nested-command-substitution.test b/t/chainlint/multi-line-nested-command-substitution.test
new file mode 100644 (file)
index 0000000..ca0620a
--- /dev/null
@@ -0,0 +1,9 @@
+(
+       foo &&
+       x=$(
+               echo bar |
+               cat
+       ) &&
+       echo ok
+) |
+sort
diff --git a/t/chainlint/nested-cuddled-subshell.expect b/t/chainlint/nested-cuddled-subshell.expect
new file mode 100644 (file)
index 0000000..c2a59ff
--- /dev/null
@@ -0,0 +1,19 @@
+(
+       (cd foo &&
+               bar
+>>     ) &&
+       (cd foo &&
+               bar
+?!AMP?!>>      )
+       (
+               cd foo &&
+>>             bar) &&
+       (
+               cd foo &&
+?!AMP?!>>              bar)
+       (cd foo &&
+>>             bar) &&
+       (cd foo &&
+?!AMP?!>>              bar)
+       foobar
+>)
diff --git a/t/chainlint/nested-cuddled-subshell.test b/t/chainlint/nested-cuddled-subshell.test
new file mode 100644 (file)
index 0000000..8fd656c
--- /dev/null
@@ -0,0 +1,31 @@
+(
+# LINT: opening "(" cuddled with first nested subshell statement
+       (cd foo &&
+               bar
+       ) &&
+
+# LINT: same but "&&" missing
+       (cd foo &&
+               bar
+       )
+
+# LINT: closing ")" cuddled with final nested subshell statement
+       (
+               cd foo &&
+               bar) &&
+
+# LINT: same but "&&" missing
+       (
+               cd foo &&
+               bar)
+
+# LINT: "(" and ")" cuddled with first and final subshell statements
+       (cd foo &&
+               bar) &&
+
+# LINT: same but "&&" missing
+       (cd foo &&
+               bar)
+
+       foobar
+)
diff --git a/t/chainlint/nested-here-doc.expect b/t/chainlint/nested-here-doc.expect
new file mode 100644 (file)
index 0000000..559301e
--- /dev/null
@@ -0,0 +1,5 @@
+(
+       cat &&
+?!AMP?!        cat
+       foobar
+>)
diff --git a/t/chainlint/nested-here-doc.test b/t/chainlint/nested-here-doc.test
new file mode 100644 (file)
index 0000000..027e0bb
--- /dev/null
@@ -0,0 +1,23 @@
+(
+# LINT: inner "EOF" not misintrepreted as closing INPUT_END here-doc
+       cat <<-\INPUT_END &&
+       fish are mice
+       but geese go slow
+       data <<EOF
+               perl is lerp
+               and nothing else
+       EOF
+       toink
+       INPUT_END
+
+# LINT: same but missing "&&"
+       cat <<-\EOT
+       text goes here
+       data <<EOF
+               data goes here
+       EOF
+       more test here
+       EOT
+
+       foobar
+)
diff --git a/t/chainlint/nested-subshell-comment.expect b/t/chainlint/nested-subshell-comment.expect
new file mode 100644 (file)
index 0000000..15b68d4
--- /dev/null
@@ -0,0 +1,11 @@
+(
+       foo &&
+       (
+               bar &&
+               # bottles wobble while fiddles gobble
+               # minor numbers of cows (or do they?)
+               baz &&
+               snaff
+?!AMP?!>>      )
+       fuzzy
+>)
diff --git a/t/chainlint/nested-subshell-comment.test b/t/chainlint/nested-subshell-comment.test
new file mode 100644 (file)
index 0000000..0ff136a
--- /dev/null
@@ -0,0 +1,13 @@
+(
+       foo &&
+       (
+               bar &&
+# LINT: ")" in comment in nested subshell not misinterpreted as closing ")"
+               # bottles wobble while fiddles gobble
+               # minor numbers of cows (or do they?)
+               baz &&
+               snaff
+# LINT: missing "&&" on ')'
+       )
+       fuzzy
+)
diff --git a/t/chainlint/nested-subshell.expect b/t/chainlint/nested-subshell.expect
new file mode 100644 (file)
index 0000000..c8165ad
--- /dev/null
@@ -0,0 +1,12 @@
+(
+       cd foo &&
+       (
+               echo a &&
+               echo b
+>>     ) >file &&
+       cd foo &&
+       (
+               echo a
+               echo b
+>>     ) >file
+>)
diff --git a/t/chainlint/nested-subshell.test b/t/chainlint/nested-subshell.test
new file mode 100644 (file)
index 0000000..998b05a
--- /dev/null
@@ -0,0 +1,14 @@
+(
+       cd foo &&
+       (
+               echo a &&
+               echo b
+       ) >file &&
+
+       cd foo &&
+       (
+# LINT: nested multi-line subshell not presently checked for missing "&&"
+               echo a
+               echo b
+       ) >file
+)