|
| 1 | +# Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from src.test.py.bazel import test_base |
| 18 | + |
| 19 | + |
| 20 | +class OptionsTest(test_base.TestBase): |
| 21 | + |
| 22 | + def testCanOverrideStarlarkFlagInBazelrcConfigStanza(self): |
| 23 | + self.ScratchFile("WORKSPACE.bazel") |
| 24 | + self.ScratchFile("bazelrc", [ |
| 25 | + "build:red --//f:color=red", |
| 26 | + ]) |
| 27 | + self.ScratchFile("f/BUILD.bazel", [ |
| 28 | + 'load(":f.bzl", "color", "r")', |
| 29 | + "color(", |
| 30 | + ' name = "color",', |
| 31 | + ' build_setting_default = "white",', |
| 32 | + ")", |
| 33 | + 'r(name = "r")', |
| 34 | + ]) |
| 35 | + self.ScratchFile("f/f.bzl", [ |
| 36 | + 'ColorValue = provider("color")', |
| 37 | + "def _color_impl(ctx):", |
| 38 | + " return [ColorValue(color = ctx.build_setting_value)]", |
| 39 | + "color = rule(", |
| 40 | + " implementation = _color_impl,", |
| 41 | + "build_setting = config.string(flag = True),", |
| 42 | + ")", |
| 43 | + "def _r_impl(ctx):", |
| 44 | + " print(ctx.attr._color[ColorValue].color)", |
| 45 | + " return [DefaultInfo()]", |
| 46 | + "r = rule(", |
| 47 | + " implementation = _r_impl,", |
| 48 | + ' attrs = {"_color": attr.label(default = "//f:color")},', |
| 49 | + ")", |
| 50 | + ]) |
| 51 | + |
| 52 | + exit_code, _, stderr = self.RunBazel([ |
| 53 | + "--bazelrc=bazelrc", |
| 54 | + "build", |
| 55 | + "--nobuild", |
| 56 | + "//f:r", |
| 57 | + "--config=red", |
| 58 | + "--//f:color=green", |
| 59 | + ]) |
| 60 | + self.AssertExitCode(exit_code, 0, stderr) |
| 61 | + self.assertTrue( |
| 62 | + any("/f/f.bzl:9:10: green" in line for line in stderr), |
| 63 | + "\n".join(stderr), |
| 64 | + ) |
| 65 | + |
| 66 | + exit_code, _, stderr = self.RunBazel([ |
| 67 | + "--bazelrc=bazelrc", |
| 68 | + "build", |
| 69 | + "--nobuild", |
| 70 | + "//f:r", |
| 71 | + "--//f:color=green", |
| 72 | + "--config=red", |
| 73 | + ]) |
| 74 | + self.AssertExitCode(exit_code, 0, stderr) |
| 75 | + self.assertTrue( |
| 76 | + any("/f/f.bzl:9:10: red" in line for line in stderr), |
| 77 | + "\n".join(stderr), |
| 78 | + ) |
| 79 | + |
| 80 | + def testAllSupportedPseudoCommand(self): |
| 81 | + self.ScratchFile("WORKSPACE.bazel") |
| 82 | + self.ScratchFile(".bazelrc", [ |
| 83 | + "all-supported --copt=-Dfoo", |
| 84 | + "all-supported --copt -Dbar", |
| 85 | + "all-supported --force_pic", |
| 86 | + "all-supported:my-config --copt=-Dbaz", |
| 87 | + "all-supported:my-config --copt -Dquz", |
| 88 | + "all-supported:my-config --noforce_pic", |
| 89 | + ]) |
| 90 | + self.ScratchFile("pkg/BUILD.bazel", [ |
| 91 | + "cc_binary(name='main',srcs=['main.cc'])", |
| 92 | + ]) |
| 93 | + self.ScratchFile("pkg/main.cc", [ |
| 94 | + "#include <stdio.h>", |
| 95 | + "int main() {", |
| 96 | + "#ifdef foo", |
| 97 | + " printf(\"foo\\n\");", |
| 98 | + "#endif", |
| 99 | + "#ifdef bar", |
| 100 | + " printf(\"bar\\n\");", |
| 101 | + "#endif", |
| 102 | + "#ifdef baz", |
| 103 | + " printf(\"baz\\n\");", |
| 104 | + "#endif", |
| 105 | + "#ifdef quz", |
| 106 | + " printf(\"quz\\n\");", |
| 107 | + "#endif", |
| 108 | + " return 0;", |
| 109 | + "}", |
| 110 | + ]) |
| 111 | + |
| 112 | + # Check that run honors the all-supported flags. |
| 113 | + exit_code, stdout, stderr = self.RunBazel([ |
| 114 | + "run", |
| 115 | + "//pkg:main", |
| 116 | + ]) |
| 117 | + self.AssertExitCode(exit_code, 0, stderr) |
| 118 | + self.assertEquals( |
| 119 | + ["foo", "bar"], |
| 120 | + stdout, |
| 121 | + ) |
| 122 | + |
| 123 | + exit_code, stdout, stderr = self.RunBazel([ |
| 124 | + "run", |
| 125 | + "--config=my-config", |
| 126 | + "//pkg:main", |
| 127 | + ]) |
| 128 | + self.AssertExitCode(exit_code, 0, stderr) |
| 129 | + self.assertEquals( |
| 130 | + ["foo", "bar", "baz", "quz"], |
| 131 | + stdout, |
| 132 | + ) |
| 133 | + |
| 134 | + # Check that aquery honors the all-supported flags. |
| 135 | + exit_code, stdout, stderr = self.RunBazel([ |
| 136 | + "aquery", |
| 137 | + "mnemonic(CppLink, //pkg:main)", |
| 138 | + ]) |
| 139 | + self.AssertExitCode(exit_code, 0, stderr) |
| 140 | + self.assertTrue( |
| 141 | + any("-pie" in line for line in stdout), |
| 142 | + stdout, |
| 143 | + ) |
| 144 | + |
| 145 | + exit_code, stdout, stderr = self.RunBazel([ |
| 146 | + "aquery", |
| 147 | + "--config=my-config", |
| 148 | + "mnemonic(CppLink, //pkg:main)", |
| 149 | + ]) |
| 150 | + self.AssertExitCode(exit_code, 0, stderr) |
| 151 | + self.assertFalse( |
| 152 | + any("-pie" in line for line in stdout), |
| 153 | + stdout, |
| 154 | + ) |
| 155 | + |
| 156 | + # Check that query ignores the unsupported all-supported flags. |
| 157 | + exit_code, stdout, stderr = self.RunBazel([ |
| 158 | + "query", |
| 159 | + "//pkg:main", |
| 160 | + ]) |
| 161 | + self.AssertExitCode(exit_code, 0, stderr) |
| 162 | + |
| 163 | + exit_code, stdout, stderr = self.RunBazel([ |
| 164 | + "query", |
| 165 | + "--config=my-config", |
| 166 | + "//pkg:main", |
| 167 | + ]) |
| 168 | + self.AssertExitCode(exit_code, 0, stderr) |
| 169 | + |
| 170 | + def testAllSupportedPseudoCommand_unsupportedOptionValue(self): |
| 171 | + self.ScratchFile("WORKSPACE.bazel") |
| 172 | + self.ScratchFile(".bazelrc", [ |
| 173 | + "all-supported --output=starlark", |
| 174 | + ]) |
| 175 | + self.ScratchFile("pkg/BUILD.bazel", [ |
| 176 | + "cc_binary(name='main',srcs=['main.cc'])", |
| 177 | + ]) |
| 178 | + |
| 179 | + # Check that cquery honors the all-supported flag. |
| 180 | + exit_code, stdout, stderr = self.RunBazel([ |
| 181 | + "cquery", |
| 182 | + "--starlark:expr=target.label.name", |
| 183 | + "//pkg:main", |
| 184 | + ]) |
| 185 | + self.AssertExitCode(exit_code, 0, stderr) |
| 186 | + self.assertEquals( |
| 187 | + ["main"], |
| 188 | + stdout, |
| 189 | + ) |
| 190 | + |
| 191 | + # Check that query fails as it supports the --output flag, but not its |
| 192 | + # value. |
| 193 | + exit_code, stdout, stderr = self.RunBazel([ |
| 194 | + "query", |
| 195 | + "//pkg:main", |
| 196 | + ]) |
| 197 | + self.AssertExitCode(exit_code, 2, stderr) |
| 198 | + self.assertTrue( |
| 199 | + any("ERROR: Invalid output format 'starlark'." in line for line in stderr), |
| 200 | + stderr, |
| 201 | + ) |
| 202 | + |
| 203 | + |
| 204 | +if __name__ == "__main__": |
| 205 | + unittest.main() |
0 commit comments