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 analysis_test.bzl. 18# 19# End to end tests of analysis_test.bzl cover verification that 20# analysis_test tests fail when their underlying test targets fail analysis. 21 22# --- begin runfiles.bash initialization --- 23set -euo pipefail 24if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 25 if [[ -f "$0.runfiles_manifest" ]]; then 26 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" 27 elif [[ -f "$0.runfiles/MANIFEST" ]]; then 28 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" 29 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 30 export RUNFILES_DIR="$0.runfiles" 31 fi 32fi 33if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 34 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" 35elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 36 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ 37 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" 38else 39 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" 40 exit 1 41fi 42# --- end runfiles.bash initialization --- 43 44source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \ 45 || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } 46 47function create_pkg() { 48 local -r pkg="$1" 49 mkdir -p "$pkg" 50 cd "$pkg" 51 52 cat > WORKSPACE <<EOF 53workspace(name = 'bazel_skylib') 54EOF 55 56 mkdir -p rules 57 cat > rules/BUILD <<EOF 58exports_files(["*.bzl"]) 59EOF 60 61 mkdir -p lib 62 cat > lib/BUILD <<EOF 63exports_files(["*.bzl"]) 64EOF 65 cat > lib/types.bzl <<EOF 66_a_tuple_type = type(()) 67 68def _is_tuple(v): 69 return type(v) == _a_tuple_type 70 71types = struct( 72 is_tuple = _is_tuple, 73) 74EOF 75 76 ln -sf "$(rlocation $TEST_WORKSPACE/rules/analysis_test.bzl)" rules/analysis_test.bzl 77 78 mkdir -p fakerules 79 cat > fakerules/rules.bzl <<EOF 80load("//rules:analysis_test.bzl", "analysis_test") 81 82def _fake_rule_impl(ctx): 83 fail("This rule should never work") 84 85fake_rule = rule( 86 implementation = _fake_rule_impl, 87) 88 89def _fake_depending_rule_impl(ctx): 90 return [] 91 92fake_depending_rule = rule( 93 implementation = _fake_depending_rule_impl, 94 attrs = {"deps" : attr.label_list()}, 95) 96EOF 97 98 cat > fakerules/BUILD <<EOF 99exports_files(["*.bzl"]) 100EOF 101 102 mkdir -p testdir 103 cat > testdir/dummy.cc <<EOF 104int dummy() { return 0; } 105EOF 106 107 cat > testdir/BUILD <<EOF 108load("//rules:analysis_test.bzl", "analysis_test") 109load("//fakerules:rules.bzl", "fake_rule", "fake_depending_rule") 110 111fake_rule(name = "target_fails") 112 113fake_depending_rule(name = "dep_fails", 114 deps = [":target_fails"]) 115 116analysis_test( 117 name = "direct_target_fails", 118 targets = [":target_fails"], 119) 120 121analysis_test( 122 name = "transitive_target_fails", 123 targets = [":dep_fails"], 124) 125 126# Use it in a non-test target 127cc_library( 128 name = "dummy_cc_library", 129 srcs = ["dummy.cc"], 130) 131 132analysis_test( 133 name = "target_succeeds", 134 targets = [":dummy_cc_library"], 135) 136EOF 137} 138 139function test_target_succeeds() { 140 local -r pkg="${FUNCNAME[0]}" 141 create_pkg "$pkg" 142 143 bazel test testdir:target_succeeds >"$TEST_log" 2>&1 || fail "Expected test to pass" 144 145 expect_log "PASSED" 146} 147 148function test_direct_target_fails() { 149 local -r pkg="${FUNCNAME[0]}" 150 create_pkg "$pkg" 151 152 bazel test testdir:direct_target_fails --test_output=all --verbose_failures \ 153 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 154 155 expect_log "This rule should never work" 156} 157 158function test_transitive_target_fails() { 159 local -r pkg="${FUNCNAME[0]}" 160 create_pkg "$pkg" 161 162 bazel test testdir:transitive_target_fails --test_output=all --verbose_failures \ 163 >"$TEST_log" 2>&1 && fail "Expected test to fail" || true 164 165 expect_log "This rule should never work" 166} 167 168cd "$TEST_TMPDIR" 169run_suite "analysis_test test suite" 170