diff mbox series

[3/3] t/chainlint: add tests for test body in heredoc

Message ID 20240706060754.GC700151@coredump.intra.peff.net (mailing list archive)
State New, archived
Headers show
Series [1/3] chainlint.pl: fix line number reporting | expand

Commit Message

Jeff King July 6, 2024, 6:07 a.m. UTC
The chainlint.pl script recently learned about our new:

  test_expect_success 'some test' - <<\EOT
	TEST_BODY
  EOT

syntax, where TEST_BODY should be checked in the usual way. Let's make
sure this works by adding a few tests. The "here-doc-body" file tests
the basic syntax, including an embedded here-doc which we should still
be able to recognize.

Likewise the "here-doc-body-indent" checks the same thing, but using the
"<<-" operator. We wouldn't expect this to be used normally, but we
would not want to accidentally miss a body that uses it.

The "here-doc-double" tests the handling of two here-doc tags on the
same line. This is not something we'd expect anybody to do in practice,
but the code was written defensively to handle this, so let's make sure
it works.

Signed-off-by: Jeff King <peff@peff.net>
---
These could also be squashed into Eric's patch which introduces the new
functionality.

 t/chainlint/here-doc-body-indent.expect |  2 ++
 t/chainlint/here-doc-body-indent.test   |  4 ++++
 t/chainlint/here-doc-body.expect        |  7 +++++++
 t/chainlint/here-doc-body.test          |  9 +++++++++
 t/chainlint/here-doc-double.expect      |  2 ++
 t/chainlint/here-doc-double.test        | 10 ++++++++++
 6 files changed, 34 insertions(+)
 create mode 100644 t/chainlint/here-doc-body-indent.expect
 create mode 100644 t/chainlint/here-doc-body-indent.test
 create mode 100644 t/chainlint/here-doc-body.expect
 create mode 100644 t/chainlint/here-doc-body.test
 create mode 100644 t/chainlint/here-doc-double.expect
 create mode 100644 t/chainlint/here-doc-double.test

Comments

Eric Sunshine July 8, 2024, 2:43 a.m. UTC | #1
On Sat, Jul 6, 2024 at 2:07 AM Jeff King <peff@peff.net> wrote:
> The chainlint.pl script recently learned about our new:
>
>   test_expect_success 'some test' - <<\EOT
>         TEST_BODY
>   EOT
>
> syntax, where TEST_BODY should be checked in the usual way. Let's make
> sure this works by adding a few tests. [...]
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> These could also be squashed into Eric's patch which introduces the new
> functionality.

As the author of these tests, you should get credit, so I'd proposed
not squashing this into my patch.

> diff --git a/t/chainlint/here-doc-body.test b/t/chainlint/here-doc-body.test
> @@ -0,0 +1,9 @@
> +test_expect_success 'here-doc-body' - <<\EOT
> +       echo "missing chain before"
> +       cat >file <<-\EOF &&
> +       inside inner here-doc
> +       these are not shell commands
> +       EOF
> +       echo "missing chain after"
> +       echo "but this line is OK because it's the end"
> +EOT

This one made me think of an additional pathological case, though I'm
not sure it's worth having a test:

    test_expect_success 'pathological-here-doc-body' - <<\EOF
        echo "missing chain before"
        cat >file <<-\EOF &&
        inside inner here-doc
        these are not shell commands
        EOF
        echo "missing chain after"
        echo "but this line is OK because it's the end"
   EOF

It's exactly the same as your test except that the same tag ("EOF") is
used for both outer and inner heredocs. It works because the outer
heredoc is introduced with `<<` whereas the inner with `<<-`. The
opposite case, in which outer is introduced with `<<-` and inner with
`<<`, obviously would be bogus.
Jeff King July 8, 2024, 8:59 a.m. UTC | #2
On Sun, Jul 07, 2024 at 10:43:45PM -0400, Eric Sunshine wrote:

> This one made me think of an additional pathological case, though I'm
> not sure it's worth having a test:
> 
>     test_expect_success 'pathological-here-doc-body' - <<\EOF
>         echo "missing chain before"
>         cat >file <<-\EOF &&
>         inside inner here-doc
>         these are not shell commands
>         EOF
>         echo "missing chain after"
>         echo "but this line is OK because it's the end"
>    EOF
> 
> It's exactly the same as your test except that the same tag ("EOF") is
> used for both outer and inner heredocs. It works because the outer
> heredoc is introduced with `<<` whereas the inner with `<<-`. The
> opposite case, in which outer is introduced with `<<-` and inner with
> `<<`, obviously would be bogus.

Ooh, that's devious. I'll add it.

-Peff
diff mbox series

Patch

diff --git a/t/chainlint/here-doc-body-indent.expect b/t/chainlint/here-doc-body-indent.expect
new file mode 100644
index 0000000000..ba280af56e
--- /dev/null
+++ b/t/chainlint/here-doc-body-indent.expect
@@ -0,0 +1,2 @@ 
+	echo "we should find this" ?!AMP?!
+	echo "even though our heredoc has its indent stripped"
diff --git a/t/chainlint/here-doc-body-indent.test b/t/chainlint/here-doc-body-indent.test
new file mode 100644
index 0000000000..39ff970ef3
--- /dev/null
+++ b/t/chainlint/here-doc-body-indent.test
@@ -0,0 +1,4 @@ 
+test_expect_success 'here-doc-body-indent' - <<-\EOT
+	echo "we should find this"
+	echo "even though our heredoc has its indent stripped"
+EOT
diff --git a/t/chainlint/here-doc-body.expect b/t/chainlint/here-doc-body.expect
new file mode 100644
index 0000000000..3d21ad2fd6
--- /dev/null
+++ b/t/chainlint/here-doc-body.expect
@@ -0,0 +1,7 @@ 
+	echo "missing chain before" ?!AMP?!
+	cat >file <<-\EOF &&
+	inside inner here-doc
+	these are not shell commands
+	EOF
+	echo "missing chain after" ?!AMP?!
+	echo "but this line is OK because it's the end"
diff --git a/t/chainlint/here-doc-body.test b/t/chainlint/here-doc-body.test
new file mode 100644
index 0000000000..989ac2f4e1
--- /dev/null
+++ b/t/chainlint/here-doc-body.test
@@ -0,0 +1,9 @@ 
+test_expect_success 'here-doc-body' - <<\EOT
+	echo "missing chain before"
+	cat >file <<-\EOF &&
+	inside inner here-doc
+	these are not shell commands
+	EOF
+	echo "missing chain after"
+	echo "but this line is OK because it's the end"
+EOT
diff --git a/t/chainlint/here-doc-double.expect b/t/chainlint/here-doc-double.expect
new file mode 100644
index 0000000000..e164050d06
--- /dev/null
+++ b/t/chainlint/here-doc-double.expect
@@ -0,0 +1,2 @@ 
+	echo "actual test commands" ?!AMP?!
+	echo "that should be checked"
diff --git a/t/chainlint/here-doc-double.test b/t/chainlint/here-doc-double.test
new file mode 100644
index 0000000000..777389f0d9
--- /dev/null
+++ b/t/chainlint/here-doc-double.test
@@ -0,0 +1,10 @@ 
+# This is obviously a ridiculous thing to do, but we should be able
+# to handle two here-docs on the same line, and attribute them
+# correctly.
+test_expect_success "$(cat <<END_OF_PREREQS)" 'here-doc-double' - <<\EOT
+SOME
+PREREQS
+END_OF_PREREQS
+	echo "actual test commands"
+	echo "that should be checked"
+EOT