Skip to content

Commit bbf0427

Browse files
committed
Add tests
1 parent d2176dc commit bbf0427

File tree

4 files changed

+268
-89
lines changed

4 files changed

+268
-89
lines changed

src/test/java/com/google/devtools/common/options/OptionsParserTest.java

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.collect.ImmutableMap;
2424
import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
2525
import com.google.devtools.common.options.OptionPriority.PriorityCategory;
26+
import com.google.devtools.common.options.OptionsParser.ConstructionException;
2627
import java.util.ArrayList;
2728
import java.util.Collections;
2829
import java.util.HashMap;
@@ -209,20 +210,74 @@ public static class ExampleInternalOptions extends OptionsBase {
209210
public boolean privateBoolean;
210211

211212
@Option(
212-
name = "internal_string",
213-
metadataTags = {OptionMetadataTag.INTERNAL},
214-
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
215-
effectTags = {OptionEffectTag.NO_OP},
216-
defaultValue = "super secret"
213+
name = "internal_string",
214+
metadataTags = {OptionMetadataTag.INTERNAL},
215+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
216+
effectTags = {OptionEffectTag.NO_OP},
217+
defaultValue = "super secret"
217218
)
218219
public String privateString;
219220
}
220221

222+
public static class ExampleEquivalentWithFoo extends OptionsBase {
223+
224+
@Option(
225+
name = "foo",
226+
category = "one",
227+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
228+
effectTags = {OptionEffectTag.NO_OP},
229+
defaultValue = "differentDefault"
230+
)
231+
public String foo;
232+
233+
@Option(
234+
name = "bar",
235+
category = "one",
236+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
237+
effectTags = {OptionEffectTag.NO_OP},
238+
defaultValue = "differentDefault"
239+
)
240+
public String bar;
241+
242+
@Option(
243+
name = "not_foo",
244+
category = "one",
245+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
246+
effectTags = {OptionEffectTag.NO_OP},
247+
defaultValue = "differentDefault"
248+
)
249+
public String notFoo;
250+
251+
@Option(
252+
name = "not_bar",
253+
category = "one",
254+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
255+
effectTags = {OptionEffectTag.NO_OP},
256+
defaultValue = "differentDefault"
257+
)
258+
public String notBar;
259+
}
260+
261+
public static class ExampleIncompatibleWithFoo extends OptionsBase {
262+
263+
@Option(
264+
name = "foo",
265+
category = "one",
266+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
267+
effectTags = {OptionEffectTag.NO_OP},
268+
defaultValue = "true"
269+
)
270+
public boolean foo;
271+
}
272+
273+
221274
public static class StringConverter extends Converter.Contextless<String> {
275+
222276
@Override
223277
public String convert(String input) {
224278
return input;
225279
}
280+
226281
@Override
227282
public String getTypeDescription() {
228283
return "a string";
@@ -2401,6 +2456,31 @@ public void negativeExternalTargetPatternsInOptions_failsDistinctively() {
24012456
.contains("Flags corresponding to Starlark-defined build settings always start with '--'");
24022457
}
24032458

2459+
@Test
2460+
public void fallbackOptions_optionsParsingEquivalently()
2461+
throws OptionsParsingException {
2462+
OpaqueOptionsData fallbackData = OptionsParser.getFallbackOptionsData(
2463+
ImmutableList.of(ExampleFoo.class, ExampleEquivalentWithFoo.class));
2464+
OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build();
2465+
parser.parseWithSourceFunction(PriorityCategory.RC_FILE, o -> ".bazelrc",
2466+
ImmutableList.of("--foo=bar", "--not_foo=baz", "--bar", "1", "--not_bar", "baz"),
2467+
fallbackData);
2468+
2469+
assertThat(parser.getOptions(ExampleFoo.class)).isNotNull();
2470+
assertThat(parser.getOptions(ExampleFoo.class).foo).isEqualTo("bar");
2471+
assertThat(parser.getOptions(ExampleFoo.class).bar).isEqualTo(1);
2472+
2473+
assertThat(parser.getOptions(ExampleEquivalentWithFoo.class)).isNull();
2474+
}
2475+
2476+
@Test
2477+
public void fallbackOptions_optionsParsingDifferently() {
2478+
Exception e = assertThrows(ConstructionException.class,
2479+
() -> OptionsParser.getFallbackOptionsData(
2480+
ImmutableList.of(ExampleFoo.class, ExampleIncompatibleWithFoo.class)));
2481+
assertThat(e).hasCauseThat().isInstanceOf(DuplicateOptionDeclarationException.class);
2482+
}
2483+
24042484
private static OptionInstanceOrigin createInvocationPolicyOrigin() {
24052485
return createInvocationPolicyOrigin(/*implicitDependent=*/ null, /*expandedFrom=*/ null);
24062486
}

src/test/py/bazel/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ py_test(
331331
)
332332

333333
py_test(
334-
name = "starlark_options_test",
335-
srcs = ["starlark_options_test.py"],
334+
name = "options_test",
335+
srcs = ["options_test.py"],
336336
deps = [":test_base"],
337337
)
338338

src/test/py/bazel/options_test.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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:my-config --copt=-Dbaz",
86+
"all-supported:my-config --copt -Dquz",
87+
])
88+
self.ScratchFile("pkg/BUILD.bazel", [
89+
"cc_binary(name='main',srcs=['main.cc'])",
90+
])
91+
self.ScratchFile("pkg/main.cc", [
92+
"#include <stdio.h>",
93+
"int main() {",
94+
"#ifdef foo",
95+
" printf(\"foo\\n\");",
96+
"#endif",
97+
"#ifdef bar",
98+
" printf(\"bar\\n\");",
99+
"#endif",
100+
"#ifdef baz",
101+
" printf(\"baz\\n\");",
102+
"#endif",
103+
"#ifdef quz",
104+
" printf(\"quz\\n\");",
105+
"#endif",
106+
" return 0;",
107+
"}",
108+
])
109+
110+
# Check that run honors the all-supported flags.
111+
exit_code, stdout, stderr = self.RunBazel([
112+
"run",
113+
"//pkg:main",
114+
])
115+
self.AssertExitCode(exit_code, 0, stderr)
116+
self.assertEquals(
117+
["foo", "bar"],
118+
stdout,
119+
)
120+
121+
exit_code, stdout, stderr = self.RunBazel([
122+
"run",
123+
"--config=my-config",
124+
"//pkg:main",
125+
])
126+
self.AssertExitCode(exit_code, 0, stderr)
127+
self.assertEquals(
128+
["foo", "bar", "baz", "quz"],
129+
stdout,
130+
)
131+
132+
# Check that query ignores the unsupported all-supported flags.
133+
exit_code, stdout, stderr = self.RunBazel([
134+
"query",
135+
"//pkg:main",
136+
])
137+
self.AssertExitCode(exit_code, 0, stderr)
138+
139+
exit_code, stdout, stderr = self.RunBazel([
140+
"query",
141+
"--config=my-config",
142+
"//pkg:main",
143+
])
144+
self.AssertExitCode(exit_code, 0, stderr)
145+
146+
def testAllSupportedPseudoCommand_unsupportedOptionValue(self):
147+
self.ScratchFile("WORKSPACE.bazel")
148+
self.ScratchFile(".bazelrc", [
149+
"all-supported --output=starlark",
150+
])
151+
self.ScratchFile("pkg/BUILD.bazel", [
152+
"cc_binary(name='main',srcs=['main.cc'])",
153+
])
154+
155+
# Check that cquery honors the all-supported flag.
156+
exit_code, stdout, stderr = self.RunBazel([
157+
"cquery",
158+
"--starlark:expr=target.label.name",
159+
"//pkg:main",
160+
])
161+
self.AssertExitCode(exit_code, 0, stderr)
162+
self.assertEquals(
163+
["main"],
164+
stdout,
165+
)
166+
167+
# Check that query fails as it supports the --output flag, but not its
168+
# value.
169+
exit_code, stdout, stderr = self.RunBazel([
170+
"query",
171+
"//pkg:main",
172+
])
173+
self.AssertExitCode(exit_code, 2, stderr)
174+
self.assertTrue(
175+
any("ERROR: Invalid output format 'starlark'." in line for line in stderr),
176+
stderr,
177+
)
178+
179+
180+
if __name__ == "__main__":
181+
unittest.main()

src/test/py/bazel/starlark_options_test.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)