summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorrillig <rillig@pkgsrc.org>2020-06-07 05:53:53 +0000
committerrillig <rillig@pkgsrc.org>2020-06-07 05:53:53 +0000
commit57eccee032b59e35a46c035997de80cf1a3acfdb (patch)
treec7bfcd875c42bd0137a729fd25fee52d052adb8b /regress
parenta859c84d8f7ac860e9e088f2c9a0ce16052ee191 (diff)
downloadpkgsrc-57eccee032b59e35a46c035997de80cf1a3acfdb.tar.gz
regress/infra-unittests: add test case for no-op detection
Diffstat (limited to 'regress')
-rw-r--r--regress/infra-unittests/subst.sh82
-rw-r--r--regress/infra-unittests/test.subr13
2 files changed, 91 insertions, 4 deletions
diff --git a/regress/infra-unittests/subst.sh b/regress/infra-unittests/subst.sh
index 407cb47e32c..8de6faf5a92 100644
--- a/regress/infra-unittests/subst.sh
+++ b/regress/infra-unittests/subst.sh
@@ -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 @@ if test_case_begin 'several substitution, only one applies to file'; then
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 --git a/regress/infra-unittests/test.subr b/regress/infra-unittests/test.subr
index f7452bcd443..61e0a6d691b 100644
--- a/regress/infra-unittests/test.subr
+++ b/regress/infra-unittests/test.subr
@@ -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 @@ mock_cmd() {
cmdname="$1"
shift 1
+ . "$pkgsrcdir/mk/tools/shquote.sh"
+
{
printf '#! /bin/sh\n'
printf '\n'
@@ -221,15 +223,20 @@ mock_cmd() {
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