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