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 <sunsh...@sunshineco.com>
---
 .../close-nested-and-parent-together.expect       |  4 ++++
 t/chainlint/close-nested-and-parent-together.test |  3 +++
 t/chainlint/complex-if-in-cuddled-loop.expect     | 10 ++++++++++
 t/chainlint/complex-if-in-cuddled-loop.test       | 11 +++++++++++
 t/chainlint/if-in-loop.expect                     | 12 ++++++++++++
 t/chainlint/if-in-loop.test                       | 15 +++++++++++++++
 t/chainlint/loop-in-if.expect                     | 12 ++++++++++++
 t/chainlint/loop-in-if.test                       | 15 +++++++++++++++
 8 files changed, 82 insertions(+)
 create mode 100644 t/chainlint/close-nested-and-parent-together.expect
 create mode 100644 t/chainlint/close-nested-and-parent-together.test
 create mode 100644 t/chainlint/complex-if-in-cuddled-loop.expect
 create mode 100644 t/chainlint/complex-if-in-cuddled-loop.test
 create mode 100644 t/chainlint/if-in-loop.expect
 create mode 100644 t/chainlint/if-in-loop.test
 create mode 100644 t/chainlint/loop-in-if.expect
 create mode 100644 t/chainlint/loop-in-if.test

diff --git a/t/chainlint/close-nested-and-parent-together.expect 
b/t/chainlint/close-nested-and-parent-together.expect
new file mode 100644
index 0000000000..2a910f9d66
--- /dev/null
+++ b/t/chainlint/close-nested-and-parent-together.expect
@@ -0,0 +1,4 @@
+(
+cd foo &&
+       (bar &&
+>>>            baz))
diff --git a/t/chainlint/close-nested-and-parent-together.test 
b/t/chainlint/close-nested-and-parent-together.test
new file mode 100644
index 0000000000..72d482f76d
--- /dev/null
+++ b/t/chainlint/close-nested-and-parent-together.test
@@ -0,0 +1,3 @@
+(cd foo &&
+       (bar &&
+               baz))
diff --git a/t/chainlint/complex-if-in-cuddled-loop.expect 
b/t/chainlint/complex-if-in-cuddled-loop.expect
new file mode 100644
index 0000000000..9674b88cf2
--- /dev/null
+++ b/t/chainlint/complex-if-in-cuddled-loop.expect
@@ -0,0 +1,10 @@
+(
+for i in a b c; do
+   if test "$(echo $(waffle bat))" = "eleventeen" &&
+     test "$x" = "$y"; then
+     :
+   else
+     echo >file
+   fi
+> done) &&
+test ! -f file
diff --git a/t/chainlint/complex-if-in-cuddled-loop.test 
b/t/chainlint/complex-if-in-cuddled-loop.test
new file mode 100644
index 0000000000..571bbd85cd
--- /dev/null
+++ b/t/chainlint/complex-if-in-cuddled-loop.test
@@ -0,0 +1,11 @@
+# LINT: 'for' loop cuddled with "(" and ")" and nested 'if' with complex
+# LINT: multi-line condition; indented with spaces, not tabs
+(for i in a b c; do
+   if test "$(echo $(waffle bat))" = "eleventeen" &&
+     test "$x" = "$y"; then
+     :
+   else
+     echo >file
+   fi
+ done) &&
+test ! -f file
diff --git a/t/chainlint/if-in-loop.expect b/t/chainlint/if-in-loop.expect
new file mode 100644
index 0000000000..03d3ceb22d
--- /dev/null
+++ b/t/chainlint/if-in-loop.expect
@@ -0,0 +1,12 @@
+(
+       for i in a b c
+       do
+               if false
+               then
+?!AMP?!                        echo "err"
+                       exit 1
+?!AMP?!                fi
+               foo
+?!AMP?!        done
+       bar
+>)
diff --git a/t/chainlint/if-in-loop.test b/t/chainlint/if-in-loop.test
new file mode 100644
index 0000000000..daf22da164
--- /dev/null
+++ b/t/chainlint/if-in-loop.test
@@ -0,0 +1,15 @@
+(
+       for i in a b c
+       do
+               if false
+               then
+# LINT: missing "&&" on 'echo'
+                       echo "err"
+                       exit 1
+# LINT: missing "&&" on 'fi'
+               fi
+               foo
+# LINT: missing "&&" on 'done'
+       done
+       bar
+)
diff --git a/t/chainlint/loop-in-if.expect b/t/chainlint/loop-in-if.expect
new file mode 100644
index 0000000000..088e622c31
--- /dev/null
+++ b/t/chainlint/loop-in-if.expect
@@ -0,0 +1,12 @@
+(
+       if true
+       then
+               while true
+               do
+?!AMP?!                        echo "pop"
+                       echo "glup"
+?!AMP?!                done
+               foo
+?!AMP?!        fi
+       bar
+>)
diff --git a/t/chainlint/loop-in-if.test b/t/chainlint/loop-in-if.test
new file mode 100644
index 0000000000..93e8ba8e4d
--- /dev/null
+++ b/t/chainlint/loop-in-if.test
@@ -0,0 +1,15 @@
+(
+       if true
+       then
+               while true
+               do
+# LINT: missing "&&" on 'echo'
+                       echo "pop"
+                       echo "glup"
+# LINT: missing "&&" on 'done'
+               done
+               foo
+# LINT: missing "&&" on 'fi'
+       fi
+       bar
+)
-- 
2.18.0.203.gfac676dfb9

Reply via email to