• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 bazel_skylib/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  ln -sf "$(rlocation bazel_skylib/rules/analysis_test.bzl)" rules/analysis_test.bzl
62
63  mkdir -p fakerules
64  cat > fakerules/rules.bzl <<EOF
65load("//rules:analysis_test.bzl", "analysis_test")
66
67def _fake_rule_impl(ctx):
68    fail("This rule should never work")
69
70fake_rule = rule(
71    implementation = _fake_rule_impl,
72)
73
74def _fake_depending_rule_impl(ctx):
75    return []
76
77fake_depending_rule = rule(
78    implementation = _fake_depending_rule_impl,
79    attrs = {"deps" : attr.label_list()},
80)
81EOF
82
83  cat > fakerules/BUILD <<EOF
84exports_files(["*.bzl"])
85EOF
86
87  mkdir -p testdir
88  cat > testdir/dummy.cc <<EOF
89int dummy() { return 0; }
90EOF
91
92  cat > testdir/BUILD <<EOF
93load("//rules:analysis_test.bzl", "analysis_test")
94load("//fakerules:rules.bzl", "fake_rule", "fake_depending_rule")
95
96fake_rule(name = "target_fails")
97
98fake_depending_rule(name = "dep_fails",
99    deps = [":target_fails"])
100
101analysis_test(
102    name = "direct_target_fails",
103    targets = [":target_fails"],
104)
105
106analysis_test(
107    name = "transitive_target_fails",
108    targets = [":dep_fails"],
109)
110
111# Use it in a non-test target
112cc_library(
113    name = "dummy_cc_library",
114    srcs = ["dummy.cc"],
115)
116
117analysis_test(
118    name = "target_succeeds",
119    targets = [":dummy_cc_library"],
120)
121EOF
122}
123
124function test_target_succeeds() {
125  local -r pkg="${FUNCNAME[0]}"
126  create_pkg "$pkg"
127
128  bazel test testdir:target_succeeds >"$TEST_log" 2>&1 || fail "Expected test to pass"
129
130  expect_log "PASSED"
131}
132
133function test_direct_target_fails() {
134  local -r pkg="${FUNCNAME[0]}"
135  create_pkg "$pkg"
136
137  bazel test testdir:direct_target_fails --test_output=all --verbose_failures \
138      >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
139
140  expect_log "This rule should never work"
141}
142
143function test_transitive_target_fails() {
144  local -r pkg="${FUNCNAME[0]}"
145  create_pkg "$pkg"
146
147  bazel test testdir:transitive_target_fails --test_output=all --verbose_failures \
148      >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
149
150  expect_log "This rule should never work"
151}
152
153cd "$TEST_TMPDIR"
154run_suite "analysis_test test suite"
155