Received: by mail.netbsd.org (Postfix, from userid 605) id 8D8EB84E20; Thu, 19 Mar 2020 16:58:37 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mail.netbsd.org (Postfix) with ESMTP id 1449F84E1F for ; Thu, 19 Mar 2020 16:58:37 +0000 (UTC) X-Virus-Scanned: amavisd-new at netbsd.org Received: from mail.netbsd.org ([127.0.0.1]) by localhost (mail.netbsd.org [127.0.0.1]) (amavisd-new, port 10025) with ESMTP id o6UuBE7hdDuY for ; Thu, 19 Mar 2020 16:58:36 +0000 (UTC) Received: from cvs.NetBSD.org (ivanova.NetBSD.org [IPv6:2001:470:a085:999:28c:faff:fe03:5984]) by mail.netbsd.org (Postfix) with ESMTP id 1D2E484D2D for ; Thu, 19 Mar 2020 16:58:36 +0000 (UTC) Received: by cvs.NetBSD.org (Postfix, from userid 500) id 14B2AFB27; Thu, 19 Mar 2020 16:58:36 +0000 (UTC) Content-Transfer-Encoding: 7bit Content-Type: multipart/mixed; boundary="_----------=_1584637116282740" MIME-Version: 1.0 Date: Thu, 19 Mar 2020 16:58:36 +0000 From: "Roland Illig" Subject: CVS commit: pkgsrc/regress/infra-unittests To: pkgsrc-changes@NetBSD.org Reply-To: rillig@netbsd.org X-Mailer: log_accum Message-Id: <20200319165836.14B2AFB27@cvs.NetBSD.org> Sender: pkgsrc-changes-owner@NetBSD.org List-Id: pkgsrc-changes.NetBSD.org Precedence: bulk List-Unsubscribe: This is a multi-part message in MIME format. --_----------=_1584637116282740 Content-Disposition: inline Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" Module Name: pkgsrc Committed By: rillig Date: Thu Mar 19 16:58:36 UTC 2020 Modified Files: pkgsrc/regress/infra-unittests: spec test.subr Added Files: pkgsrc/regress/infra-unittests: subst.sh Log Message: regress/infra-unittests: add tests for mk/subst.mk To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 pkgsrc/regress/infra-unittests/spec \ pkgsrc/regress/infra-unittests/test.subr cvs rdiff -u -r0 -r1.1 pkgsrc/regress/infra-unittests/subst.sh Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. --_----------=_1584637116282740 Content-Disposition: inline Content-Length: 11694 Content-Transfer-Encoding: binary Content-Type: text/x-diff; charset=us-ascii Modified files: Index: pkgsrc/regress/infra-unittests/spec diff -u pkgsrc/regress/infra-unittests/spec:1.1 pkgsrc/regress/infra-unittests/spec:1.2 --- pkgsrc/regress/infra-unittests/spec:1.1 Thu Mar 21 21:45:30 2019 +++ pkgsrc/regress/infra-unittests/spec Thu Mar 19 16:58:35 2020 @@ -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() { Index: pkgsrc/regress/infra-unittests/test.subr diff -u pkgsrc/regress/infra-unittests/test.subr:1.1 pkgsrc/regress/infra-unittests/test.subr:1.2 --- pkgsrc/regress/infra-unittests/test.subr:1.1 Thu Mar 21 21:45:30 2019 +++ pkgsrc/regress/infra-unittests/test.subr Thu Mar 19 16:58:35 2020 @@ -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 < "$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 --equals \n' 1>&2 exit 1 Added files: Index: pkgsrc/regress/infra-unittests/subst.sh diff -u /dev/null pkgsrc/regress/infra-unittests/subst.sh:1.1 --- /dev/null Thu Mar 19 16:58:36 2020 +++ pkgsrc/regress/infra-unittests/subst.sh Thu Mar 19 16:58:35 2020 @@ -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" <" +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" < "$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" < 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" < 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" < "$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" < "$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" < "$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" < "$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" < "$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" < "$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 --_----------=_1584637116282740--