xref: /aosp_15_r20/external/bazel-skylib/tests/common_settings_test.sh (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
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 common_settings.bzl
18
19# --- begin runfiles.bash initialization ---
20set -euo pipefail
21if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
22  if [[ -f "$0.runfiles_manifest" ]]; then
23    export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
24  elif [[ -f "$0.runfiles/MANIFEST" ]]; then
25    export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
26  elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
27    export RUNFILES_DIR="$0.runfiles"
28  fi
29fi
30if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
31  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
32elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
33  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
34            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
35else
36  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
37  exit 1
38fi
39# --- end runfiles.bash initialization ---
40
41source "$(rlocation $TEST_WORKSPACE/tests/unittest.bash)" \
42  || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
43
44function create_volcano_pkg() {
45  local -r pkg="$1"
46  mkdir -p "$pkg"
47  cd "$pkg"
48
49  cat > WORKSPACE <<EOF
50workspace(name = 'bazel_skylib')
51EOF
52
53  mkdir -p rules
54  cat > rules/BUILD <<EOF
55exports_files(["*.bzl"])
56EOF
57
58  ln -sf "$(rlocation $TEST_WORKSPACE/rules/common_settings.bzl)" rules/common_settings.bzl
59
60  mkdir -p volcano
61  cat > volcano/rules.bzl <<EOF
62load("//rules:common_settings.bzl", "BuildSettingInfo")
63
64def _volcano_impl(ctx):
65  description = struct(
66    height = ctx.attr.height[BuildSettingInfo].value,
67    active = ctx.attr.active[BuildSettingInfo].value,
68    namer = ctx.attr.namer[BuildSettingInfo].value,
69    nicknames = ctx.attr.nicknames[BuildSettingInfo].value
70  )
71  print(description)
72
73volcano = rule(
74  implementation = _volcano_impl,
75  attrs = {
76    "height" : attr.label(),
77    "active" : attr.label(),
78    "namer": attr.label(),
79    "nicknames": attr.label(),
80  }
81)
82EOF
83
84  cat > volcano/BUILD <<EOF
85load(
86  "//rules:common_settings.bzl",
87  "int_flag",
88  "int_setting",
89  "bool_flag",
90  "string_flag",
91  "string_list_flag",
92)
93load("//volcano:rules.bzl", "volcano")
94
95int_flag(
96  name = "height-flag",
97  build_setting_default = 9677 # pre-1980 explosion
98)
99
100bool_flag(
101  name = "active-flag",
102  build_setting_default = True
103)
104
105string_flag(
106  name = "namer-flag",
107  build_setting_default = "cpt-george-vancouver",
108  values = ["cpt-george-vancouver", "puyallup-tribe"]
109)
110
111string_list_flag(
112  name = "nicknames-flag",
113  build_setting_default = ["loowit", "loowitiatkla", "lavelatla"]
114)
115
116int_setting(
117  name = "height-setting",
118  build_setting_default = 9677
119)
120
121volcano(
122  name = "mt-st-helens",
123  height = ":height-flag",
124  active = ":active-flag",
125  namer = ":namer-flag",
126  nicknames = ":nicknames-flag",
127)
128EOF
129
130}
131
132function test_can_set_flags() {
133  local -r pkg="${FUNCNAME[0]}"
134  create_volcano_pkg "$pkg"
135
136  bazel build volcano:mt-st-helens --//volcano:height-flag=8366 \
137    --//volcano:active-flag=False --//volcano:namer-flag=puyallup-tribe \
138    --//volcano:nicknames-flag=volcano-mc-volcanoface \
139    >"$TEST_log" 2>&1 || fail "Expected test to pass"
140
141  expect_log "active = False"
142  expect_log "height = 8366"
143  expect_log "namer = \"puyallup-tribe\""
144  expect_log "nicknames = \[\"volcano-mc-volcanoface\"\]"
145}
146
147function test_cannot_set_settings() {
148  local -r pkg="${FUNCNAME[0]}"
149  create_volcano_pkg "$pkg"
150
151  bazel build volcano:mt-st-helens --//volcano:height-setting=8366 \
152    >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
153
154  expect_log "Unrecognized option: //volcano:height-setting"
155}
156
157function test_not_allowed_value() {
158  local -r pkg="${FUNCNAME[0]}"
159  create_volcano_pkg "$pkg"
160
161  bazel build volcano:mt-st-helens --//volcano:namer-flag=me \
162    >"$TEST_log" 2>&1 && fail "Expected test to fail" || true
163
164  expect_log "Error setting //volcano:namer-flag: invalid value 'me'. Allowed values are"
165}
166
167
168cd "$TEST_TMPDIR"
169run_suite "common_settings test suite"
170