From cd11d001fe8619f491fcbf89517a14169411ac3e Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Wed, 4 Mar 2020 17:21:51 -0800 Subject: [PATCH] Support skipping tests from test{,_device}() Most of the time, test requirements can be checked without much setup. However, in some cases, it's not possible to know if the test can be run until we're halfway through the test. Allow setting SKIP_REASON from the test function. This should only be used as a last resort. --- check | 4 +++- new | 6 ++++++ tests/meta/014 | 13 +++++++++++++ tests/meta/014.out | 2 ++ tests/meta/015 | 13 +++++++++++++ tests/meta/015.out | 2 ++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100755 tests/meta/014 create mode 100644 tests/meta/014.out create mode 100755 tests/meta/015 create mode 100644 tests/meta/015.out diff --git a/check b/check index 0b6f2d6..398eca0 100755 --- a/check +++ b/check @@ -364,7 +364,9 @@ _call_test() { _cleanup - if ! diff "tests/${TEST_NAME}.out" "${seqres}.out" >/dev/null; then + if [[ -v SKIP_REASON ]]; then + TEST_RUN["status"]="not run" + elif ! diff "tests/${TEST_NAME}.out" "${seqres}.out" >/dev/null; then mv "${seqres}.out" "${seqres}.out.bad" TEST_RUN["status"]=fail TEST_RUN["reason"]=output diff --git a/new b/new index 9a2d255..31973ed 100755 --- a/new +++ b/new @@ -187,6 +187,12 @@ DESCRIPTION="" # failure. You should prefer letting the test fail because of broken output # over, say, checking the exit status of every command you run. # +# If the test cannot be run, this function may set the \$SKIP_REASON variable +# and return. In that case, the return value and output are ignored, and the +# test is considered skipped. This should only be done when the requirements +# can only be detected with a non-trivial amount of setup; use +# group_requires(), requires(), and/or device_requires() instead when possible. +# # Various variables are defined for the test: # - \$TEST_NAME -- the full name of the test. # - \$TMPDIR -- a temporary directory deleted after the test is run. diff --git a/tests/meta/014 b/tests/meta/014 new file mode 100755 index 0000000..0db1fb8 --- /dev/null +++ b/tests/meta/014 @@ -0,0 +1,13 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-3.0+ +# Copyright (C) 2020 Omar Sandoval +# +# Test skipping from test(). + +. tests/meta/rc + +DESCRIPTION="skip in test()" + +test() { + SKIP_REASON="(╯°□°)╯︵ ┻━┻" +} diff --git a/tests/meta/014.out b/tests/meta/014.out new file mode 100644 index 0000000..b6a7d3b --- /dev/null +++ b/tests/meta/014.out @@ -0,0 +1,2 @@ +Running meta/014 +Test complete diff --git a/tests/meta/015 b/tests/meta/015 new file mode 100755 index 0000000..3dc588b --- /dev/null +++ b/tests/meta/015 @@ -0,0 +1,13 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-3.0+ +# Copyright (C) 2020 Omar Sandoval +# +# Test skipping from test_device(). + +. tests/meta/rc + +DESCRIPTION="skip in test_device()" + +test_device() { + SKIP_REASON="(╯°□°)╯︵ ┻━┻" +} diff --git a/tests/meta/015.out b/tests/meta/015.out new file mode 100644 index 0000000..88092d0 --- /dev/null +++ b/tests/meta/015.out @@ -0,0 +1,2 @@ +Running meta/015 +Test complete -- 2.49.0