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 bazel_skylib/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 bazel_skylib/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 bazel_skylib/lib/dicts.bzl)" lib/dicts.bzl 75 ln -sf "$(rlocation bazel_skylib/lib/new_sets.bzl)" lib/new_sets.bzl 76 ln -sf "$(rlocation bazel_skylib/lib/partial.bzl)" lib/partial.bzl 77 ln -sf "$(rlocation bazel_skylib/lib/sets.bzl)" lib/sets.bzl 78 ln -sf "$(rlocation bazel_skylib/lib/types.bzl)" lib/types.bzl 79 ln -sf "$(rlocation bazel_skylib/lib/unittest.bzl)" lib/unittest.bzl 80 81 mkdir -p toolchains/unittest 82 ln -sf "$(rlocation bazel_skylib/toolchains/unittest/BUILD)" toolchains/unittest/BUILD 83 84 # Create test files. 85 mkdir -p testdir 86 cat > testdir/BUILD <<'EOF' 87load("//tests:unittest_tests.bzl", 88 "basic_passing_test", 89 "basic_failing_test", 90 "failure_message_test", 91 "fail_unexpected_passing_test", 92 "fail_unexpected_passing_fake_rule") 93 94basic_passing_test(name = "basic_passing_test") 95 96basic_failing_test(name = "basic_failing_test") 97 98failure_message_test( 99 name = "shell_escape_failure_message_test", 100 message = "Contains $FOO", 101) 102 103failure_message_test( 104 name = "cmd_escape_failure_message_test", 105 message = "Contains %FOO%", 106) 107 108failure_message_test( 109 name = "eof_failure_message_test", 110 message = "\nEOF\n more after EOF", 111) 112 113fail_unexpected_passing_test( 114 name = "fail_unexpected_passing_test", 115 target_under_test = ":fail_unexpected_passing_fake_target", 116) 117 118fail_unexpected_passing_fake_rule( 119 name = "fail_unexpected_passing_fake_target", 120 tags = ["manual"]) 121EOF 122} 123 124function test_basic_passing_test() { 125 local -r pkg="${FUNCNAME[0]}" 126 create_pkg "$pkg" 127 128 bazel test testdir:basic_passing_test >"$TEST_log" 2>&1 || fail "Expected test to pass" 129 130 expect_log "PASSED" 131} 132 133function test_basic_failing_test() { 134 local -r pkg="${FUNCNAME[0]}" 135 create_pkg "$pkg" 136 137 bazel test testdir:basic_failing_test --test_output=all --verbose_failures \ 138 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 139 140 expect_log "In test _basic_failing_test from //tests:unittest_tests.bzl: Expected \"1\", but got \"2\"" 141} 142 143function test_shell_escape_failure_message_test() { 144 local -r pkg="${FUNCNAME[0]}" 145 create_pkg "$pkg" 146 147 bazel test testdir:shell_escape_failure_message_test --test_output=all --verbose_failures \ 148 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 149 150 expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains $FOO"' 151} 152 153function test_cmd_escape_failure_message_test() { 154 local -r pkg="${FUNCNAME[0]}" 155 create_pkg "$pkg" 156 157 bazel test testdir:cmd_escape_failure_message_test --test_output=all --verbose_failures \ 158 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 159 160 expect_log 'In test _failure_message_test from //tests:unittest_tests.bzl: Expected "", but got "Contains %FOO%"' 161} 162 163function test_eof_failure_message_test() { 164 local -r pkg="${FUNCNAME[0]}" 165 create_pkg "$pkg" 166 167 bazel test testdir:eof_failure_message_test --test_output=all --verbose_failures \ 168 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 169 170 expect_log '^ more after EOF' 171} 172 173function test_fail_unexpected_passing_test() { 174 local -r pkg="${FUNCNAME[0]}" 175 create_pkg "$pkg" 176 177 bazel test testdir:fail_unexpected_passing_test --test_output=all --verbose_failures \ 178 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 179 180 expect_log "Expected failure of target_under_test, but found success" 181} 182 183cd "$TEST_TMPDIR" 184run_suite "unittest test suite" 185