Source-Changes-HG archive

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

[pkgsrc/trunk]: pkgsrc/regress/infra-unittests regress/infra-unittests: add t...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/4ed378ccc17d
branches:  trunk
changeset: 433667:4ed378ccc17d
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Jun 07 05:53:53 2020 +0000

description:
regress/infra-unittests: add test case for no-op detection

diffstat:

 regress/infra-unittests/subst.sh  |  82 ++++++++++++++++++++++++++++++++++++++-
 regress/infra-unittests/test.subr |  13 ++++-
 2 files changed, 91 insertions(+), 4 deletions(-)

diffs (136 lines):

diff -r 9dd1e7112d2e -r 4ed378ccc17d regress/infra-unittests/subst.sh
--- a/regress/infra-unittests/subst.sh  Sun Jun 07 04:41:58 2020 +0000
+++ b/regress/infra-unittests/subst.sh  Sun Jun 07 05:53:53 2020 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: subst.sh,v 1.45 2020/06/06 13:17:34 rillig Exp $
+# $NetBSD: subst.sh,v 1.46 2020/06/07 05:53:53 rillig Exp $
 #
 # Tests for mk/subst.mk.
 #
@@ -1668,3 +1668,83 @@
 
        test_case_end
 fi
+
+
+if test_case_begin 'identity substitution with newline'; then
+
+       # Ensures that the adjusted sed command line in the "found=" line
+       # in mk/subst.mk does not create shell syntax errors.
+
+       # This is the sed command after the "found=" line in mk/subst.mk.
+       # It tests whether any of the patterns is found.
+       # It only outputs the actually found lines (-n) by appending a "p"
+       # to the usual "s,from,to," commands.
+       mock_sed=$(
+               newline='
+'
+               args='-n'
+
+               # In this "usual" sed command, the "p" is added.
+               args="$args -e s,identity,identity,p"
+
+               # This is considered an "unusual" sed command because of
+               # the leading 1, therefore no "p" is added.
+               #
+               # Ideally this should be considered a "usual" sed command,
+               # even though it only applies to some of the lines.
+               # To do this, mk/scripts/subst-identity.awk has to parse
+               # sed addresses, in addition to substitutions.
+               args="$args -e 1s,first line,first line,"
+
+               # In the Makefile, the additional quotes at the beginning
+               # make this an "unusual" sed command, and the :C modifier
+               # in subst.mk doesn't see that after unquoting the word,
+               # the sed command is quite usual.  This is an edge case
+               # that doesn't occur in practice.
+               args="$args -e s,unusual,unusual,g"
+
+               # No "p" is added since this is not a "usual" substitution.
+               # If it had been found, the file would have changed anyway,
+               # and this sed command line would not be executed.
+               args="$args -e /not found/d"
+
+               # Same here.  Just make sure that the generated sed command
+               # line does not lead to a syntax error in the shell.
+               args="$args -e /not found/a\\${newline}added${newline}"
+
+               args="$args file"
+
+               mock_cmd 'mock-sed' \
+                       --when-args "$args" --then-output 'identity'
+       )
+       create_file 'testcase.mk' <<-EOF
+               SUBST_CLASSES+=         id
+               SUBST_FILES.id=         file
+               SUBST_SED.id=           -e 's,identity,identity,'
+               SUBST_SED.id+=          -e '1s,first line,first line,'
+               SUBST_SED.id+=          -e '''''s,unusual,unusual,g'
+               SUBST_SED.id+=          -e '/not found/d'
+               SUBST_SED.id+=          -e '/not found/a\\\${.newline}added\${.newline}'
+               # Use the standard sed for the main part.
+               SUBST_FILTER_CMD.id=    LC_ALL=C sed \${SUBST_SED.id}
+
+               .include "prepare-subst.mk"
+               # Use the mocked sed for the "found=" part.
+               SED=                    $mock_sed
+               .include "mk/subst.mk"
+               # ignore PKG_FAIL_REASON (SUBST_SED + SUBST_FILTER_CMD)
+       EOF
+       create_file 'file' <<-EOF
+               identity
+       EOF
+
+       run_bmake 'testcase.mk' 'subst-id' 1> "$tmpdir/output" 2>&1 \
+       && exitcode=0 || exitcode=$?
+
+       assert_that "$tmpdir/output" --file-is-lines \
+               '=> Substituting "id" in file'
+       assert_that 'file' --file-is-lines \
+               'identity'
+
+       test_case_end
+fi
diff -r 9dd1e7112d2e -r 4ed378ccc17d regress/infra-unittests/test.subr
--- a/regress/infra-unittests/test.subr Sun Jun 07 04:41:58 2020 +0000
+++ b/regress/infra-unittests/test.subr Sun Jun 07 05:53:53 2020 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: test.subr,v 1.15 2020/05/12 05:34:04 rillig Exp $
+# $NetBSD: test.subr,v 1.16 2020/06/07 05:53:53 rillig Exp $
 #
 # This file defines utilities for testing Makefile fragments and shell
 # programs from the pkgsrc infrastructure. While testing one part of the
@@ -214,6 +214,8 @@
        cmdname="$1"
        shift 1
 
+       . "$pkgsrcdir/mk/tools/shquote.sh"
+
        {
        printf '#! /bin/sh\n'
        printf '\n'
@@ -221,15 +223,20 @@
        while [ $# -ge 4 ]; do
                case $1,$3 in
                (--when-args,--then-output)
+                       shquote "$2"; shquoted_arg="$shquoted"
+                       shquote "$4"; shquoted_output="$shquoted"
                        cat <<EOF
-[ "x\$*" = "x$2" ] && { printf '%s\n' "$4" && exit 0; }
+[ x"\$*" = x$shquoted_arg ] && { printf '%s\n' $shquoted_output && exit 0; }
 
 EOF
                        shift 4
                        ;;
+
                (--when-args,--then-exit)
+                       shquote "$2"; shquoted_arg="$shquoted"
+                       shquote "$4"; shquoted_exit="$shquoted"
                        cat <<EOF
-[ "x\$*" = "x$2" ] && exit $4
+[ x"\$*" = x$shquoted_arg ] && exit $shquoted_exit
 
 EOF
                        shift 4



Home | Main Index | Thread Index | Old Index