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