1#!/bin/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/sets.bzl)" lib/sets.bzl 77 ln -sf "$(rlocation bazel_skylib/lib/types.bzl)" lib/types.bzl 78 ln -sf "$(rlocation bazel_skylib/lib/unittest.bzl)" lib/unittest.bzl 79 80 mkdir -p toolchains/unittest 81 ln -sf "$(rlocation bazel_skylib/toolchains/unittest/BUILD)" toolchains/unittest/BUILD 82 83 # Create test files. 84 mkdir -p testdir 85 cat > testdir/BUILD <<EOF 86load("//tests:unittest_tests.bzl", 87 "basic_passing_test", 88 "basic_failing_test", 89 "fail_unexpected_passing_test", 90 "fail_unexpected_passing_fake_rule") 91 92basic_passing_test(name = "basic_passing_test") 93 94basic_failing_test(name = "basic_failing_test") 95 96fail_unexpected_passing_test( 97 name = "fail_unexpected_passing_test", 98 target_under_test = ":fail_unexpected_passing_fake_target", 99) 100fail_unexpected_passing_fake_rule( 101 name = "fail_unexpected_passing_fake_target", 102 tags = ["manual"]) 103EOF 104} 105 106function test_basic_passing_test() { 107 local -r pkg="${FUNCNAME[0]}" 108 create_pkg "$pkg" 109 110 bazel test testdir:basic_passing_test >"$TEST_log" 2>&1 || fail "Expected test to pass" 111 112 expect_log "PASSED" 113} 114 115function test_basic_failing_test() { 116 local -r pkg="${FUNCNAME[0]}" 117 create_pkg "$pkg" 118 119 bazel test testdir:basic_failing_test --test_output=all --verbose_failures \ 120 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 121 122 expect_log "In test _basic_failing_test from //tests:unittest_tests.bzl: Expected \"1\", but got \"2\"" 123} 124 125function test_fail_unexpected_passing_test() { 126 local -r pkg="${FUNCNAME[0]}" 127 create_pkg "$pkg" 128 129 bazel test testdir:fail_unexpected_passing_test --test_output=all --verbose_failures \ 130 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 131 132 expect_log "Expected failure of target_under_test, but found success" 133} 134 135cd "$TEST_TMPDIR" 136run_suite "unittest test suite" 137