1#!/usr/bin/env bash 2 3# Copyright 2019 The Bazel Authors. All rights reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17# End to end tests for unittest.bzl. 18# 19# Specifically, end to end tests of unittest.bzl cover verification that 20# analysis-phase tests written with unittest.bzl appropriately 21# cause test failures in cases where violated assertions are made. 22 23# --- begin runfiles.bash initialization --- 24set -euo pipefail 25if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 26 if [[ -f "$0.runfiles_manifest" ]]; then 27 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" 28 elif [[ -f "$0.runfiles/MANIFEST" ]]; then 29 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" 30 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 31 export RUNFILES_DIR="$0.runfiles" 32 fi 33fi 34if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 35 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" 36elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 37 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ 38 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" 39else 40 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" 41 exit 1 42fi 43# --- end runfiles.bash initialization --- 44 45source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ 46 || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } 47 48function create_pkg() { 49 local -r pkg="$1" 50 mkdir -p "$pkg" 51 cd "$pkg" 52 53 cat > WORKSPACE <<EOF 54workspace(name = 'bazel_skylib') 55 56load("//lib:unittest.bzl", "register_unittest_toolchains") 57 58register_unittest_toolchains() 59EOF 60 61 # Copy relevant skylib sources into the current workspace. 62 mkdir -p tests 63 touch tests/BUILD 64 cat > tests/BUILD <<EOF 65exports_files(["*.bzl"]) 66EOF 67 ln -sf "$(rlocation $TEST_WORKSPACE/tests/unittest_tests.bzl)" tests/unittest_tests.bzl 68 69 mkdir -p lib 70 touch lib/BUILD 71 cat > lib/BUILD <<EOF 72exports_files(["*.bzl"]) 73EOF 74 ln -sf "$(rlocation $TEST_WORKSPACE/lib/dicts.bzl)" lib/dicts.bzl 75 ln -sf "$(rlocation $TEST_WORKSPACE/lib/new_sets.bzl)" lib/new_sets.bzl 76 ln -sf "$(rlocation $TEST_WORKSPACE/lib/partial.bzl)" lib/partial.bzl 77 ln -sf "$(rlocation $TEST_WORKSPACE/lib/sets.bzl)" lib/sets.bzl 78 ln -sf "$(rlocation $TEST_WORKSPACE/lib/types.bzl)" lib/types.bzl 79 ln -sf "$(rlocation $TEST_WORKSPACE/lib/unittest.bzl)" lib/unittest.bzl 80 81 mkdir -p toolchains/unittest 82 # Remove `package(default_applicable_license = ...)` line to avoid depending on rules_license inside this test 83 sed -e '/package(default_applicable_licenses = .*)/d' \ 84 "$(rlocation $TEST_WORKSPACE/toolchains/unittest/BUILD)" \ 85 > toolchains/unittest/BUILD 86 87 # Create test files. 88 mkdir -p testdir 89 cat > testdir/BUILD <<'EOF' 90load("//tests:unittest_tests.bzl", 91 "basic_passing_test", 92 "basic_failing_test", 93 "failure_message_test", 94 "fail_unexpected_passing_test", 95 "fail_unexpected_passing_fake_rule") 96 97basic_passing_test(name = "basic_passing_test") 98 99basic_failing_test(name = "basic_failing_test") 100 101failure_message_test( 102 name = "shell_escape_failure_message_test", 103 message = "Contains $FOO", 104) 105 106failure_message_test( 107 name = "cmd_escape_failure_message_test", 108 message = "Contains %FOO%", 109) 110 111failure_message_test( 112 name = "eof_failure_message_test", 113 message = "\nEOF\n more after EOF", 114) 115 116fail_unexpected_passing_test( 117 name = "fail_unexpected_passing_test", 118 target_under_test = ":fail_unexpected_passing_fake_target", 119) 120 121fail_unexpected_passing_fake_rule( 122 name = "fail_unexpected_passing_fake_target", 123 tags = ["manual"]) 124EOF 125} 126 127function test_basic_passing_test() { 128 local -r pkg="${FUNCNAME[0]}" 129 create_pkg "$pkg" 130 131 bazel test testdir:basic_passing_test >"$TEST_log" 2>&1 || fail "Expected test to pass" 132 133 expect_log "PASSED" 134} 135 136function test_basic_failing_test() { 137 local -r pkg="${FUNCNAME[0]}" 138 create_pkg "$pkg" 139 140 bazel test testdir:basic_failing_test --test_output=all --verbose_failures \ 141 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 142 143 expect_log "In test _basic_failing_test from //tests:unittest_tests.bzl: Expected \"1\", but got \"2\"" 144} 145 146function test_shell_escape_failure_message_test() { 147 local -r pkg="${FUNCNAME[0]}" 148 create_pkg "$pkg" 149 150 bazel test testdir:shell_escape_failure_message_test --test_output=all --verbose_failures \ 151 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 152 153 expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains $FOO"' 154} 155 156function test_cmd_escape_failure_message_test() { 157 local -r pkg="${FUNCNAME[0]}" 158 create_pkg "$pkg" 159 160 bazel test testdir:cmd_escape_failure_message_test --test_output=all --verbose_failures \ 161 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 162 163 expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains %FOO%"' 164} 165 166function test_eof_failure_message_test() { 167 local -r pkg="${FUNCNAME[0]}" 168 create_pkg "$pkg" 169 170 bazel test testdir:eof_failure_message_test --test_output=all --verbose_failures \ 171 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 172 173 expect_log '^ more after EOF' 174} 175 176function test_fail_unexpected_passing_test() { 177 local -r pkg="${FUNCNAME[0]}" 178 create_pkg "$pkg" 179 180 bazel test testdir:fail_unexpected_passing_test --test_output=all --verbose_failures \ 181 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 182 183 expect_log "Expected failure of target_under_test, but found success" 184} 185 186cd "$TEST_TMPDIR" 187run_suite "unittest test suite" 188