aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSamuel Freilich <sfreilich@google.com>2021-10-04 12:03:48 -0400
committerGitHub <noreply@github.com>2021-10-04 12:03:48 -0400
commit506c17293ef4043c68f8cb0ba5fa05d758e1231a (patch)
treee2e9bd8bb1ed5d06437903e4bb9823cc749343d5 /tests
parentb2ed61686ebca2a44d44857fef5b3e1d31cc2483 (diff)
downloadbazel-skylib-506c17293ef4043c68f8cb0ba5fa05d758e1231a.tar.gz
Improve escaping in unittest failure message (#320)
Diffstat (limited to 'tests')
-rwxr-xr-xtests/unittest_test.sh49
-rw-r--r--tests/unittest_tests.bzl24
2 files changed, 71 insertions, 2 deletions
diff --git a/tests/unittest_test.sh b/tests/unittest_test.sh
index 9455d8e..1d941b3 100755
--- a/tests/unittest_test.sh
+++ b/tests/unittest_test.sh
@@ -83,10 +83,11 @@ EOF
# Create test files.
mkdir -p testdir
- cat > testdir/BUILD <<EOF
+ cat > testdir/BUILD <<'EOF'
load("//tests:unittest_tests.bzl",
"basic_passing_test",
"basic_failing_test",
+ "failure_message_test",
"fail_unexpected_passing_test",
"fail_unexpected_passing_fake_rule")
@@ -94,10 +95,26 @@ basic_passing_test(name = "basic_passing_test")
basic_failing_test(name = "basic_failing_test")
+failure_message_test(
+ name = "shell_escape_failure_message_test",
+ message = "Contains $FOO",
+)
+
+failure_message_test(
+ name = "cmd_escape_failure_message_test",
+ message = "Contains %FOO%",
+)
+
+failure_message_test(
+ name = "eof_failure_message_test",
+ message = "\nEOF\n more after EOF",
+)
+
fail_unexpected_passing_test(
name = "fail_unexpected_passing_test",
target_under_test = ":fail_unexpected_passing_fake_target",
)
+
fail_unexpected_passing_fake_rule(
name = "fail_unexpected_passing_fake_target",
tags = ["manual"])
@@ -123,6 +140,36 @@ function test_basic_failing_test() {
expect_log "In test _basic_failing_test from //tests:unittest_tests.bzl: Expected \"1\", but got \"2\""
}
+function test_shell_escape_failure_message_test() {
+ local -r pkg="${FUNCNAME[0]}"
+ create_pkg "$pkg"
+
+ bazel test testdir:shell_escape_failure_message_test --test_output=all --verbose_failures \
+ >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
+
+ expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains $FOO"'
+}
+
+function test_cmd_escape_failure_message_test() {
+ local -r pkg="${FUNCNAME[0]}"
+ create_pkg "$pkg"
+
+ bazel test testdir:cmd_escape_failure_message_test --test_output=all --verbose_failures \
+ >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
+
+ expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains %FOO%"'
+}
+
+function test_eof_failure_message_test() {
+ local -r pkg="${FUNCNAME[0]}"
+ create_pkg "$pkg"
+
+ bazel test testdir:eof_failure_message_test --test_output=all --verbose_failures \
+ >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
+
+ expect_log '^ more after EOF'
+}
+
function test_fail_unexpected_passing_test() {
local -r pkg="${FUNCNAME[0]}"
create_pkg "$pkg"
diff --git a/tests/unittest_tests.bzl b/tests/unittest_tests.bzl
index abddbdd..5541d74 100644
--- a/tests/unittest_tests.bzl
+++ b/tests/unittest_tests.bzl
@@ -18,8 +18,9 @@ load("//lib:partial.bzl", "partial")
load("//lib:unittest.bzl", "analysistest", "asserts", "unittest")
###################################
-####### fail_basic_test ###########
+####### basic_failing_test ########
###################################
+
def _basic_failing_test(ctx):
"""Unit tests for a basic library verification test that fails."""
env = unittest.begin(ctx)
@@ -31,6 +32,27 @@ def _basic_failing_test(ctx):
basic_failing_test = unittest.make(_basic_failing_test)
###################################
+####### failure_message_test ######
+###################################
+
+def _failure_message_test(ctx):
+ """Failing unit test with arbitrary content in the message."""
+ env = unittest.begin(ctx)
+
+ if not ctx.attr.message:
+ unittest.fail(env, "Message must be non-empty.")
+ asserts.equals(env, "", ctx.attr.message)
+
+ return unittest.end(env)
+
+failure_message_test = unittest.make(
+ _failure_message_test,
+ attrs = {
+ "message": attr.string(),
+ },
+)
+
+###################################
####### basic_passing_test ########
###################################
def _basic_passing_test(ctx):