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

This commit is contained in:
rillig 2020-06-07 05:53:53 +00:00
parent c7940cfb7f
commit 6b0624ddd0
2 changed files with 91 additions and 4 deletions

View file

@ -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

View file

@ -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