Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/bin/sh PR bin/53550



details:   https://anonhg.NetBSD.org/src/rev/5c8dcaae7c96
branches:  trunk
changeset: 1026399:5c8dcaae7c96
user:      kre <kre%NetBSD.org@localhost>
date:      Mon Nov 22 05:21:54 2021 +0000

description:
PR bin/53550

/bin/sh's processing of here doc expansions has changed.   Now it happens
in the context of the parent shell, so side effects are visible there,
just like all other redirection expansions.

We need to stop testing that that doesn't happen, and instead test
that it does.   This is that change.

Add another test case which is testing exactly the example from the PR
(well, with a different exit status, 1 is too generic and could happen
by accident) to make sure we don't reintroduce that bug sometime.

diffstat:

 tests/bin/sh/t_here.sh |  45 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 43 insertions(+), 2 deletions(-)

diffs (79 lines):

diff -r d3634ed26d46 -r 5c8dcaae7c96 tests/bin/sh/t_here.sh
--- a/tests/bin/sh/t_here.sh    Mon Nov 22 05:17:43 2021 +0000
+++ b/tests/bin/sh/t_here.sh    Mon Nov 22 05:21:54 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_here.sh,v 1.8 2021/09/09 00:04:51 kre Exp $
+# $NetBSD: t_here.sh,v 1.9 2021/11/22 05:21:54 kre Exp $
 #
 # Copyright (c) 2007 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -515,13 +515,36 @@
        results
 }
 
+#
+# This next test is really just testing what our shell happens to do.
+# There doesn't seem to be any spec on in which context expansions
+# in redirects are processed.   Most shells do them in the parent
+# shell context, meaning that side effects of the expansion become
+# visible to the shell - a couple process redirect expansions in
+# a subshell, meaning that side effects are lost.
+#
+# Before PR bin/53550 was fixed, the NetBSD sh evaluated all redirect
+# expansions, except here documents, in the context of the shell, and
+# here document redirects in a subshell context.   That distinction
+# makes no real sense (and only an old, and maybe still current, FreeBSD
+# shell shares that pecadillo.)   Afer that fix, the NetBSD shell joins
+# almost all others in expanding redirects (all of them) in the shell
+# context, meaning that side effects of here documenty expansions become
+# visible in the shell.
+#
+# Before the fix, we used to get "2\n1\n" as the output from this
+# test, now the variable assignment in the here document persists
+# and we get "2\n2\n" as do most other shells.  (bash is a notable
+# exception, but it does all redirect expansions in a subshell context)
+#
+
 atf_test_case side_effects
 side_effects_head() {
        atf_set "descr" "Tests how side effects in here documents are handled"
 }
 side_effects_body() {
 
-       atf_check -s exit:0 -o inline:'2\n1\n' -e empty ${TEST_SH} -c '
+       atf_check -s exit:0 -o inline:'2\n2\n' -e empty ${TEST_SH} -c '
                unset X
                cat <<-EOF
                ${X=2}
@@ -530,6 +553,23 @@
                '
 }
 
+# This is a test for the specific bug reported in PR bin/53550
+# This should work in any shell.
+
+atf_test_case exit_status
+exit_status_head() {
+       atf_set descr "Tests exit status of a command substitution in a heredoc"
+}
+exit_status_body() {
+
+       # PR bin/53550 test
+       atf_check -s exit:7 -o empty -e empty ${TEST_SH} -c '
+               <<-EOF
+               $(exit 7)
+               EOF
+               '
+}
+
 # The following tests a problem reported on the austin-list 2021-09-08
 # by oguzismailuysal%gmail.com@localhost ... it affected all ash derived shells
 atf_test_case hard_cases
@@ -623,6 +663,7 @@
 atf_init_test_cases() {
        atf_add_test_case do_simple     # not worthy of a comment
        atf_add_test_case end_markers   # the mundane, the weird, the bizarre
+       atf_add_test_case exit_status   # PR bin/53550, cmdsub in heredoc
        atf_add_test_case incomplete    # where the end marker isn't...
        atf_add_test_case lineends      # test weird line endings in heredocs
        atf_add_test_case multiple      # multiple << operators on one cmd



Home | Main Index | Thread Index | Old Index