summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorrillig <rillig@pkgsrc.org>2020-03-19 16:58:35 +0000
committerrillig <rillig@pkgsrc.org>2020-03-19 16:58:35 +0000
commitb101c0b0460d79c01506dd43a394fa342d3d7ea1 (patch)
tree49d4639897a2ff5bb212f8ef0830fbf7c21afb6a /regress
parent94fc62a22eb43bf4842fbf4e0f35f5a63f69f7a3 (diff)
downloadpkgsrc-b101c0b0460d79c01506dd43a394fa342d3d7ea1.tar.gz
regress/infra-unittests: add tests for mk/subst.mk
Diffstat (limited to 'regress')
-rw-r--r--regress/infra-unittests/spec7
-rw-r--r--regress/infra-unittests/subst.sh307
-rw-r--r--regress/infra-unittests/test.subr36
3 files changed, 343 insertions, 7 deletions
diff --git a/regress/infra-unittests/spec b/regress/infra-unittests/spec
index b7bde4b3717..73ad2ca3f9f 100644
--- a/regress/infra-unittests/spec
+++ b/regress/infra-unittests/spec
@@ -1,10 +1,11 @@
-# $NetBSD: spec,v 1.1 2019/03/21 21:45:30 rillig Exp $
+# $NetBSD: spec,v 1.2 2020/03/19 16:58:35 rillig Exp $
#
# Unit tests for the pkgsrc infrastructure.
do_test() {
- sh ./mocked-include.sh || TEST_EXITSTATUS=$?
- sh ./tools-bison.sh || TEST_EXITSTATUS=$?
+ for testfile in ./*.sh; do
+ sh "$testfile" || TEST_EXITSTATUS=$?
+ done
}
check_result() {
diff --git a/regress/infra-unittests/subst.sh b/regress/infra-unittests/subst.sh
new file mode 100644
index 00000000000..5365766b68b
--- /dev/null
+++ b/regress/infra-unittests/subst.sh
@@ -0,0 +1,307 @@
+#! /bin/sh
+#
+# Tests for mk/subst.mk.
+#
+set -eu
+
+. "./test.subr"
+
+testcase() {
+ test_name="$1"
+
+ rm -rf "$tmpdir/".subst_*_done
+ rm -rf "$tmpdir"/*
+ ls -A "$tmpdir"
+
+ create_file "prepare-subst.mk" <<EOF
+
+# The tools that are used by subst.mk
+CHMOD= chmod-is-not-used
+CMP= cmp
+ECHO= echo
+MV= mv
+RM= rm
+SED= sed
+TEST= test
+TOUCH= touch
+TOUCH_FLAGS= # none
+TR= tr
+TRUE= true
+
+# Commands that are specific to pkgsrc
+RUN= @: set -x;
+STEP_MSG= echo "=>"
+DO_NADA= : do-nada
+INFO_MSG= echo "info:"
+WARNING_MSG= echo "warning:"
+FAIL_MSG= sh $PWD/$real_pkgsrcdir/mk/scripts/fail echo "fail:"
+
+WRKDIR= $tmpdir
+WRKSRC= $tmpdir
+EOF
+}
+
+
+if testcase "single file"; then
+
+ # A single file is patched successfully.
+
+ create_file "subst-single.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= subst-single.txt
+SUBST_SED.class= -e 's,before,after,'
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "subst-single.txt" \
+ "before"
+
+ test_file "subst-single.mk" > "$tmpdir/output"
+
+ assert_that "output" --file-contains-exactly "=> Substituting \"class\" in subst-single.txt"
+ assert_that "subst-single.txt" --file-contains-exactly "after"
+fi
+
+
+if testcase "several individual files"; then
+
+ # Several individual files are patched successfully.
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= first second third
+SUBST_SED.class= -e 's,file,example,'
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "first" "the first file"
+ create_file_lines "second" "the second file"
+ create_file_lines "third" "the third file"
+
+ output=$(test_file "testcase.mk")
+
+ assert_that "$output" --equals "=> Substituting \"class\" in first second third"
+ assert_that "first" --file-contains-exactly "the first example"
+ assert_that "second" --file-contains-exactly "the second example"
+ assert_that "third" --file-contains-exactly "the third example"
+fi
+
+
+if testcase "several files by pattern"; then
+
+ # Several files are patched successfully.
+ # The filenames are given by a pattern.
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= pattern-*
+SUBST_SED.class= -e 's,file,example,'
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "pattern-first" "the first file"
+ create_file_lines "pattern-second" "the second file"
+ create_file_lines "pattern-third" "the third file"
+
+ output=$(test_file "testcase.mk")
+
+ assert_that "$output" --equals "=> Substituting \"class\" in pattern-*"
+ assert_that "pattern-first" --file-contains-exactly "the first example"
+ assert_that "pattern-second" --file-contains-exactly "the second example"
+ assert_that "pattern-third" --file-contains-exactly "the third example"
+fi
+
+
+if testcase "pattern with 1 noop"; then
+
+ # Several files are given via a pattern.
+ # Most of the files are patched, but one stays the same.
+ # Since it is easier to give a too broad pattern like *.py
+ # than to exclude a few files from such a pattern,
+ # only an info is logged.
+ # This is not an error.
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= pattern-*
+SUBST_SED.class= -e 's,file,example,'
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "pattern-first" "the first file"
+ create_file_lines "pattern-second" "the second is already an example"
+ create_file_lines "pattern-third" "the third file"
+
+ test_file "testcase.mk" > "$tmpdir/actual-output"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in pattern-*' \
+ 'info: [subst.mk:class] Nothing changed in ./pattern-second.'
+
+ assert_that "actual-output" --file-equals "expected-output"
+ assert_that "pattern-first" --file-contains-exactly "the first example"
+ assert_that "pattern-second" --file-contains-exactly "the second is already an example"
+ assert_that "pattern-third" --file-contains-exactly "the third example"
+fi
+
+
+if testcase "single file noop"; then
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= single
+SUBST_SED.class= -e 's,file,example,'
+SUBST_NOOP_OK.class= yes
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "single" "already an example"
+
+ test_file "testcase.mk" > "$tmpdir/actual-output" && exitcode=0 || exitcode=$?
+ assert_that "$exitcode" --equals "0"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in single' \
+ 'info: [subst.mk:class] Nothing changed in ./single.'
+
+ assert_that "actual-output" --file-equals "expected-output"
+ assert_that "single" --file-contains-exactly "already an example"
+fi
+
+
+if testcase "single file noop ok"; then
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= single
+SUBST_SED.class= -e 's,file,example,'
+SUBST_NOOP_OK.class= no
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "single" "already an example"
+
+ test_file "testcase.mk" > "$tmpdir/actual-output" && exitcode=0 || exitcode=$?
+ assert_that "$exitcode" --equals "1"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in single' \
+ 'info: [subst.mk:class] Nothing changed in ./single.' \
+ 'fail: [subst.mk:class] The pattern single has no effect.' \
+ '*** Error code 1' \
+ '' \
+ 'Stop.' \
+ "$make: stopped in $PWD"
+
+ assert_that "actual-output" --file-equals "expected-output"
+ assert_that "single" --file-contains-exactly "already an example"
+fi
+
+
+if testcase "single file nonexistent"; then
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= nonexistent
+SUBST_SED.class= -e 's,file,example,'
+SUBST_NOOP_OK.class= no
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ test_file "testcase.mk" > "$tmpdir/actual-output" && exitcode=0 || exitcode=$?
+ assert_that "$exitcode" --equals "1"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in nonexistent' \
+ 'warning: [subst.mk:class] Ignoring non-existent file "./nonexistent".' \
+ 'fail: [subst.mk:class] The pattern nonexistent has no effect.' \
+ '*** Error code 1' \
+ '' \
+ 'Stop.' \
+ "$make: stopped in $PWD"
+
+ assert_that "actual-output" --file-equals "expected-output"
+fi
+
+
+if testcase "single file nonexistent ok"; then
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= nonexistent
+SUBST_SED.class= -e 's,file,example,'
+SUBST_NOOP_OK.class= yes
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ test_file "testcase.mk" > "$tmpdir/actual-output" && exitcode=0 || exitcode=$?
+ assert_that "$exitcode" --equals "0"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in nonexistent' \
+ 'warning: [subst.mk:class] Ignoring non-existent file "./nonexistent".'
+
+ assert_that "actual-output" --file-equals "expected-output"
+fi
+
+
+if testcase "several patterns, 1 nonexistent"; then
+
+ create_file "testcase.mk" <<EOF
+SUBST_CLASSES+= class
+SUBST_STAGE.class= pre-configure
+SUBST_FILES.class= *exist* *not-found*
+SUBST_SED.class= -e 's,file,example,'
+
+.include "prepare-subst.mk"
+.include "mk/subst.mk"
+
+all: subst-class
+EOF
+
+ create_file_lines "exists" "this file exists"
+
+ test_file "testcase.mk" > "$tmpdir/actual-output" && exitcode=0 || exitcode=$?
+ assert_that "$exitcode" --equals "0"
+ create_file_lines "expected-output" \
+ '=> Substituting "class" in *exist* *not-found*' \
+ 'warning: [subst.mk:class] Ignoring non-existent file "./*not-found*".'
+
+ assert_that "actual-output" --file-equals "expected-output"
+ assert_that "exists" --file-contains-exactly "this example exists"
+fi
diff --git a/regress/infra-unittests/test.subr b/regress/infra-unittests/test.subr
index edf3fc43019..f78748d37e0 100644
--- a/regress/infra-unittests/test.subr
+++ b/regress/infra-unittests/test.subr
@@ -1,5 +1,5 @@
#! /bin/sh
-# $NetBSD: test.subr,v 1.1 2019/03/21 21:45:30 rillig Exp $
+# $NetBSD: test.subr,v 1.2 2020/03/19 16:58:35 rillig Exp $
set -eu
# This file defines utilities for testing Makefile fragments in a mocked
@@ -29,6 +29,10 @@ set -eu
# Creates a file in the temporary directory. The filename
# is relative to the temporary directory.
#
+# create_file_lines $filename $line1 $line2 ...
+# Creates a file in the temporary directory containing the
+# given lines.
+#
# create_pkgsrc_file $filename <<EOF ... EOF
# Creates a file in the temporary pkgsrc directory. This
# file will be included instead of the one in the real
@@ -48,9 +52,11 @@ set -eu
# assert_that $actual --equals $expected
# Complains loudly if $actual is not equal to $expected.
-: ${cleanup:=yes}
+: "${cleanup:=yes}"
+: "${make:=bmake}"
tmpdir="${TMP:-/tmp}/infra-unittests-$$"
mocked_pkgsrcdir="$tmpdir/pkgsrc"
+test_name="unknown test"
rm -rf "$tmpdir"
mkdir -p "$mocked_pkgsrcdir"
@@ -116,6 +122,11 @@ create_file() {
cat > "$tmpdir/$1"
}
+create_file_lines() {
+ _cfl_filename="$1"; shift
+ printf '%s\n' "$@" > "$tmpdir/$_cfl_filename"
+}
+
create_pkgsrc_file() {
mkdir -p "$mocked_pkgsrcdir/$(dirname "$1")"
cat > "$mocked_pkgsrcdir/$1"
@@ -130,15 +141,32 @@ PKGSRCDIR= $real_pkgsrcdir
EOF
shift
- bmake -f "$tmpdir/test.subr.main.mk" "$@"
+ "$make" -f "$tmpdir/test.subr.main.mk" "$@"
}
assert_that() {
case "$2" in
(--equals)
[ "x$1" = "x$3" ] && return 0
- printf 'assertion failed:\nexpected: <%s>\nbut was: <%s>\n' "$3" "$1" 1>&2
+ printf 'assertion failed in "%s":\nexpected: <%s>\nbut was: <%s>\n' "$test_name" "$3" "$1" 1>&2
+ exit 1
+ ;;
+
+ (--file-contains-exactly)
+ printf '%s\n' "$3" > "$tmpdir/expected"
+ diff -u "$tmpdir/expected" "$tmpdir/$1" > /dev/null && return 0
+ printf 'assertion failed in "%s": file %s has unexpected content:\n' "$test_name" "$1" 1>&2
+ diff -u "$tmpdir/expected" "$tmpdir/$1" 1>&2
+ exit 1
+ ;;
+
+ (--file-equals)
+ diff -u "$tmpdir/$3" "$tmpdir/$1" > /dev/null && return 0
+ printf 'assertion failed in "%s": files %s and %s differ:\n' "$test_name" "$1" "$3" 1>&2
+ diff -u "$tmpdir/$3" "$tmpdir/$1" 1>&2
exit 1
+ ;;
+
(*)
printf 'usage: assert_that <expr> --equals <expr>\n' 1>&2
exit 1