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