|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2021 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_failure_test.bzl. |
| 18 | +# |
| 19 | +# End to end tests of analysis_failure_test.bzl cover verification that |
| 20 | +# analysis_failure_test tests succeed when their underlying test targets fail analysis with |
| 21 | +# a given error message. |
| 22 | + |
| 23 | +# --- begin runfiles.bash initialization --- |
| 24 | +set -euo pipefail |
| 25 | +if [[ ! -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 |
| 33 | +fi |
| 34 | +if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 35 | + source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
| 36 | +elif [[ -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-)" |
| 39 | +else |
| 40 | + echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | +# --- end runfiles.bash initialization --- |
| 44 | + |
| 45 | +source "$(rlocation bazel_skylib/tests/unittest.bash)" \ |
| 46 | + || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } |
| 47 | + |
| 48 | +function create_pkg() { |
| 49 | + local -r pkg="$1" |
| 50 | + mkdir -p "$pkg" |
| 51 | + cd "$pkg" |
| 52 | + |
| 53 | + cat > WORKSPACE <<EOF |
| 54 | +workspace(name = 'bazel_skylib') |
| 55 | +
|
| 56 | +load("//lib:unittest.bzl", "register_unittest_toolchains") |
| 57 | +
|
| 58 | +register_unittest_toolchains() |
| 59 | +EOF |
| 60 | + |
| 61 | + mkdir -p rules |
| 62 | + cat > rules/BUILD <<EOF |
| 63 | +exports_files(["*.bzl"]) |
| 64 | +EOF |
| 65 | + ln -sf "$(rlocation bazel_skylib/rules/analysis_failure_test.bzl)" rules/analysis_failure_test.bzl |
| 66 | + |
| 67 | + mkdir -p lib |
| 68 | + cat > lib/BUILD <<EOF |
| 69 | +exports_files(["*.bzl"]) |
| 70 | +EOF |
| 71 | + ln -sf "$(rlocation bazel_skylib/lib/unittest.bzl)" lib/unittest.bzl |
| 72 | + ln -sf "$(rlocation bazel_skylib/lib/types.bzl)" lib/types.bzl |
| 73 | + ln -sf "$(rlocation bazel_skylib/lib/partial.bzl)" lib/partial.bzl |
| 74 | + ln -sf "$(rlocation bazel_skylib/lib/new_sets.bzl)" lib/new_sets.bzl |
| 75 | + ln -sf "$(rlocation bazel_skylib/lib/dicts.bzl)" lib/dicts.bzl |
| 76 | + |
| 77 | + mkdir -p toolchains/unittest |
| 78 | + ln -sf "$(rlocation bazel_skylib/toolchains/unittest/BUILD)" toolchains/unittest/BUILD |
| 79 | + |
| 80 | + mkdir -p fakerules |
| 81 | + cat > fakerules/rules.bzl <<EOF |
| 82 | +load("//rules:analysis_failure_test.bzl", "analysis_failure_test") |
| 83 | +
|
| 84 | +def _fake_rule_impl(ctx): |
| 85 | + fail("This rule fails at analysis phase") |
| 86 | +
|
| 87 | +fake_rule = rule( |
| 88 | + implementation = _fake_rule_impl, |
| 89 | +) |
| 90 | +
|
| 91 | +def _fake_depending_rule_impl(ctx): |
| 92 | + return [] |
| 93 | +
|
| 94 | +fake_depending_rule = rule( |
| 95 | + implementation = _fake_depending_rule_impl, |
| 96 | + attrs = {"deps" : attr.label_list()}, |
| 97 | +) |
| 98 | +EOF |
| 99 | + |
| 100 | + cat > fakerules/BUILD <<EOF |
| 101 | +exports_files(["*.bzl"]) |
| 102 | +EOF |
| 103 | + |
| 104 | + mkdir -p testdir |
| 105 | + cat > testdir/BUILD <<EOF |
| 106 | +load("//rules:analysis_failure_test.bzl", "analysis_failure_test") |
| 107 | +load("//fakerules:rules.bzl", "fake_rule", "fake_depending_rule") |
| 108 | +
|
| 109 | +fake_rule(name = "target_fails") |
| 110 | +
|
| 111 | +fake_depending_rule( |
| 112 | + name = "dep_fails", |
| 113 | + deps = [":target_fails"], |
| 114 | +) |
| 115 | +
|
| 116 | +fake_depending_rule( |
| 117 | + name = "rule_that_does_not_fail", |
| 118 | + deps = [], |
| 119 | +) |
| 120 | +
|
| 121 | +analysis_failure_test( |
| 122 | + name = "direct_target_fails", |
| 123 | + target_under_test = ":target_fails", |
| 124 | + error_message = "This rule fails at analysis phase", |
| 125 | +) |
| 126 | +
|
| 127 | +analysis_failure_test( |
| 128 | + name = "transitive_target_fails", |
| 129 | + target_under_test = ":dep_fails", |
| 130 | + error_message = "This rule fails at analysis phase", |
| 131 | +) |
| 132 | +
|
| 133 | +analysis_failure_test( |
| 134 | + name = "fails_with_wrong_error_message", |
| 135 | + target_under_test = ":dep_fails", |
| 136 | + error_message = "This is the wrong error message", |
| 137 | +) |
| 138 | +
|
| 139 | +analysis_failure_test( |
| 140 | + name = "fails_with_target_that_does_not_fail", |
| 141 | + target_under_test = ":rule_that_does_not_fail", |
| 142 | + error_message = "This rule fails at analysis phase", |
| 143 | +) |
| 144 | +EOF |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +function test_direct_target_succeeds() { |
| 149 | + local -r pkg="${FUNCNAME[0]}" |
| 150 | + create_pkg "$pkg" |
| 151 | + |
| 152 | + bazel test testdir:direct_target_fails >"$TEST_log" 2>&1 || fail "Expected test to pass" |
| 153 | + |
| 154 | + expect_log "PASSED" |
| 155 | +} |
| 156 | + |
| 157 | +function test_transitive_target_succeeds() { |
| 158 | + local -r pkg="${FUNCNAME[0]}" |
| 159 | + create_pkg "$pkg" |
| 160 | + |
| 161 | + bazel test testdir:transitive_target_fails >"$TEST_log" 2>&1 || fail "Expected test to pass" |
| 162 | + |
| 163 | + expect_log "PASSED" |
| 164 | +} |
| 165 | + |
| 166 | +function test_with_wrong_error_message_fails() { |
| 167 | + local -r pkg="${FUNCNAME[0]}" |
| 168 | + create_pkg "$pkg" |
| 169 | + |
| 170 | + bazel test testdir:fails_with_wrong_error_message --test_output=all --verbose_failures \ |
| 171 | + >"$TEST_log" 2>&1 && fail "Expected test to fail" || true |
| 172 | + |
| 173 | + expect_log "Expected errors to contain 'This is the wrong error message' but did not. Actual errors:" |
| 174 | +} |
| 175 | + |
| 176 | +function test_with_rule_that_does_not_fail_fails() { |
| 177 | + local -r pkg="${FUNCNAME[0]}" |
| 178 | + create_pkg "$pkg" |
| 179 | + |
| 180 | + bazel test testdir:fails_with_target_that_does_not_fail --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 | + |
| 186 | + |
| 187 | +cd "$TEST_TMPDIR" |
| 188 | +run_suite "analysis_failure_test test suite" |
0 commit comments