39
39
/**
40
40
* A test for {@link BuildOptions}.
41
41
*
42
- * Currently this tests native options and skylark options completely
43
- * separately since these two types of options do not interact. In the future when we begin to
44
- * migrate native options to skylark options, the format of this test class will need to accommodate
45
- * that overlap.
42
+ * <p>Currently this tests native options and skylark options completely separately since these two
43
+ * types of options do not interact. In the future when we begin to migrate native options to
44
+ * skylark options, the format of this test class will need to accommodate that overlap.
46
45
*/
47
46
@ RunWith (JUnit4 .class )
48
47
public class BuildOptionsTest {
49
- private static final ImmutableList <Class <? extends FragmentOptions >> TEST_OPTIONS =
48
+ private static final ImmutableList <Class <? extends FragmentOptions >> BUILD_CONFIG_OPTIONS =
50
49
ImmutableList .of (BuildConfiguration .Options .class );
51
50
52
51
@ Test
53
52
public void optionSetCaching () {
54
- BuildOptions a = BuildOptions .of (TEST_OPTIONS , OptionsParser .newOptionsParser (TEST_OPTIONS ));
55
- BuildOptions b = BuildOptions .of (TEST_OPTIONS , OptionsParser .newOptionsParser (TEST_OPTIONS ));
53
+ BuildOptions a =
54
+ BuildOptions .of (BUILD_CONFIG_OPTIONS , OptionsParser .newOptionsParser (BUILD_CONFIG_OPTIONS ));
55
+ BuildOptions b =
56
+ BuildOptions .of (BUILD_CONFIG_OPTIONS , OptionsParser .newOptionsParser (BUILD_CONFIG_OPTIONS ));
56
57
// The cache keys of the OptionSets must be equal even if these are
57
58
// different objects, if they were created with the same options (no options in this case).
58
59
assertThat (b .toString ()).isEqualTo (a .toString ());
@@ -62,26 +63,27 @@ public void optionSetCaching() {
62
63
@ Test
63
64
public void cachingSpecialCases () throws Exception {
64
65
// You can add options here to test that their string representations are good.
65
- String [] options = new String [] { "--run_under=//run_under" };
66
- BuildOptions a = BuildOptions .of (TEST_OPTIONS , options );
67
- BuildOptions b = BuildOptions .of (TEST_OPTIONS , options );
66
+ String [] options = new String [] {"--run_under=//run_under" };
67
+ BuildOptions a = BuildOptions .of (BUILD_CONFIG_OPTIONS , options );
68
+ BuildOptions b = BuildOptions .of (BUILD_CONFIG_OPTIONS , options );
68
69
assertThat (b .toString ()).isEqualTo (a .toString ());
69
70
}
70
71
71
72
@ Test
72
73
public void optionsEquality () throws Exception {
73
- String [] options1 = new String [] { "--compilation_mode=opt" };
74
- String [] options2 = new String [] { "--compilation_mode=dbg" };
74
+ String [] options1 = new String [] {"--compilation_mode=opt" };
75
+ String [] options2 = new String [] {"--compilation_mode=dbg" };
75
76
// Distinct instances with the same values are equal:
76
- assertThat (BuildOptions .of (TEST_OPTIONS , options1 ))
77
- .isEqualTo (BuildOptions .of (TEST_OPTIONS , options1 ));
77
+ assertThat (BuildOptions .of (BUILD_CONFIG_OPTIONS , options1 ))
78
+ .isEqualTo (BuildOptions .of (BUILD_CONFIG_OPTIONS , options1 ));
78
79
// Same fragments, different values aren't equal:
79
80
assertThat (
80
- BuildOptions .of (TEST_OPTIONS , options1 ).equals (BuildOptions .of (TEST_OPTIONS , options2 )))
81
+ BuildOptions .of (BUILD_CONFIG_OPTIONS , options1 )
82
+ .equals (BuildOptions .of (BUILD_CONFIG_OPTIONS , options2 )))
81
83
.isFalse ();
82
84
// Same values, different fragments aren't equal:
83
85
assertThat (
84
- BuildOptions .of (TEST_OPTIONS , options1 )
86
+ BuildOptions .of (BUILD_CONFIG_OPTIONS , options1 )
85
87
.equals (
86
88
BuildOptions .of (
87
89
ImmutableList .<Class <? extends FragmentOptions >>of (
@@ -92,9 +94,9 @@ public void optionsEquality() throws Exception {
92
94
93
95
@ Test
94
96
public void optionsDiff () throws Exception {
95
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
96
- BuildOptions two = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
97
- BuildOptions three = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
97
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
98
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
99
+ BuildOptions three = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
98
100
99
101
OptionsDiff diffOneTwo = BuildOptions .diff (one , two );
100
102
OptionsDiff diffTwoThree = BuildOptions .diff (two , three );
@@ -111,23 +113,22 @@ public void optionsDiff() throws Exception {
111
113
public void optionsDiff_differentFragments () throws Exception {
112
114
BuildOptions one =
113
115
BuildOptions .of (ImmutableList .<Class <? extends FragmentOptions >>of (CppOptions .class ));
114
- BuildOptions two = BuildOptions .of (TEST_OPTIONS );
116
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS );
115
117
116
118
OptionsDiff diff = BuildOptions .diff (one , two );
117
119
118
120
assertThat (diff .areSame ()).isFalse ();
119
121
assertThat (diff .getExtraFirstFragmentClassesForTesting ()).containsExactly (CppOptions .class );
120
122
assertThat (
121
- diff .getExtraSecondFragmentsForTesting ()
122
- .stream ()
123
+ diff .getExtraSecondFragmentsForTesting ().stream ()
123
124
.map (Object ::getClass )
124
125
.collect (Collectors .toSet ()))
125
- .containsExactlyElementsIn (TEST_OPTIONS );
126
+ .containsExactlyElementsIn (BUILD_CONFIG_OPTIONS );
126
127
}
127
128
128
129
@ Test
129
130
public void optionsDiff_nullOptionsThrow () throws Exception {
130
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
131
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
131
132
BuildOptions two = null ;
132
133
IllegalArgumentException e =
133
134
assertThrows (IllegalArgumentException .class , () -> BuildOptions .diff (one , two ));
@@ -149,8 +150,8 @@ public void optionsDiff_nullSecondValue() throws Exception {
149
150
150
151
@ Test
151
152
public void optionsDiff_differentBaseThrowException () throws Exception {
152
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
153
- BuildOptions two = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
153
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
154
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
154
155
BuildOptions three = BuildOptions .of (ImmutableList .of (CppOptions .class ), "--compiler=gcc" );
155
156
OptionsDiffForReconstruction diffForReconstruction =
156
157
BuildOptions .diffForReconstruction (one , two );
@@ -163,8 +164,8 @@ public void optionsDiff_differentBaseThrowException() throws Exception {
163
164
164
165
@ Test
165
166
public void optionsDiff_getEmptyAndApplyEmpty () throws Exception {
166
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
167
- BuildOptions two = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
167
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
168
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
168
169
OptionsDiffForReconstruction diffForReconstruction =
169
170
BuildOptions .diffForReconstruction (one , two );
170
171
BuildOptions reconstructed = one .applyDiff (diffForReconstruction );
@@ -173,8 +174,8 @@ public void optionsDiff_getEmptyAndApplyEmpty() throws Exception {
173
174
174
175
@ Test
175
176
public void applyDiff_nativeOptions () throws Exception {
176
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
177
- BuildOptions two = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
177
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
178
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
178
179
BuildOptions reconstructedTwo = one .applyDiff (BuildOptions .diffForReconstruction (one , two ));
179
180
assertThat (reconstructedTwo ).isEqualTo (two );
180
181
assertThat (reconstructedTwo ).isNotSameAs (two );
@@ -271,14 +272,14 @@ public void applyDiff_extraStarlarkOptions() throws Exception {
271
272
BuildOptions two = BuildOptions .of (ImmutableMap .of (flagNameTwo , flagValue ));
272
273
273
274
BuildOptions reconstructedTwo = one .applyDiff (BuildOptions .diffForReconstruction (one , two ));
274
-
275
+
275
276
assertThat (reconstructedTwo ).isEqualTo (two );
276
277
assertThat (reconstructedTwo ).isNotSameAs (two );
277
278
}
278
279
279
280
private static ImmutableList .Builder <Class <? extends FragmentOptions >> makeOptionsClassBuilder () {
280
281
return ImmutableList .<Class <? extends FragmentOptions >>builder ()
281
- .addAll (TEST_OPTIONS )
282
+ .addAll (BUILD_CONFIG_OPTIONS )
282
283
.add (CppOptions .class );
283
284
}
284
285
@@ -322,8 +323,8 @@ public void codecStability() throws Exception {
322
323
323
324
@ Test
324
325
public void repeatedCodec () throws Exception {
325
- BuildOptions one = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
326
- BuildOptions two = BuildOptions .of (TEST_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
326
+ BuildOptions one = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=opt" , "cpu=k8" );
327
+ BuildOptions two = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--compilation_mode=dbg" , "cpu=k8" );
327
328
OptionsDiffForReconstruction diff = BuildOptions .diffForReconstruction (one , two );
328
329
BuildOptions .OptionsDiffCache cache = new BuildOptions .FingerprintingKDiffToByteStringCache ();
329
330
assertThat (TestUtils .toBytes (diff , ImmutableMap .of (BuildOptions .OptionsDiffCache .class , cache )))
@@ -334,11 +335,56 @@ public void repeatedCodec() throws Exception {
334
335
@ Test
335
336
public void testMultiValueOptionImmutability () throws Exception {
336
337
BuildOptions options =
337
- BuildOptions .of (TEST_OPTIONS , OptionsParser .newOptionsParser (TEST_OPTIONS ));
338
+ BuildOptions .of (BUILD_CONFIG_OPTIONS , OptionsParser .newOptionsParser (BUILD_CONFIG_OPTIONS ));
338
339
BuildConfiguration .Options coreOptions = options .get (Options .class );
339
340
assertThrows (
340
341
UnsupportedOperationException .class ,
341
342
() ->
342
343
coreOptions .commandLineBuildVariables .add (new AbstractMap .SimpleEntry <>("foo" , "bar" )));
343
344
}
345
+
346
+ @ Test
347
+ public void parsingResultTransform () throws Exception {
348
+ BuildOptions original = BuildOptions .of (BUILD_CONFIG_OPTIONS , "--cpu=foo" );
349
+
350
+ OptionsParser parser = OptionsParser .newOptionsParser (BUILD_CONFIG_OPTIONS );
351
+ parser .parse ("--cpu=bar" );
352
+ parser .setStarlarkOptions (ImmutableMap .of ("//custom:flag" , "hello" ));
353
+
354
+ BuildOptions modified = original .applyParsingResult (parser );
355
+
356
+ assertThat (original .get (BuildConfiguration .Options .class ).cpu )
357
+ .isNotEqualTo (modified .get (BuildConfiguration .Options .class ).cpu );
358
+ assertThat (modified .get (BuildConfiguration .Options .class ).cpu ).isEqualTo ("bar" );
359
+ assertThat (modified .getStarlarkOptions ().get (Label .parseAbsoluteUnchecked ("//custom:flag" )))
360
+ .isEqualTo ("hello" );
361
+ }
362
+
363
+ @ Test
364
+ public void parsingResultTransformNativeIgnored () throws Exception {
365
+ ImmutableList .Builder <Class <? extends FragmentOptions >> fragmentClassesBuilder =
366
+ ImmutableList .<Class <? extends FragmentOptions >>builder ()
367
+ .add (BuildConfiguration .Options .class );
368
+
369
+ BuildOptions original = BuildOptions .of (fragmentClassesBuilder .build ());
370
+
371
+ fragmentClassesBuilder .add (CppOptions .class );
372
+
373
+ OptionsParser parser = OptionsParser .newOptionsParser (fragmentClassesBuilder .build ());
374
+ parser .parse ("--cxxopt=bar" );
375
+
376
+ BuildOptions modified = original .applyParsingResult (parser );
377
+
378
+ assertThat (modified .contains (CppOptions .class )).isFalse ();
379
+ }
380
+
381
+ @ Test
382
+ public void parsingResultTransformIllegalStarlarkLabel () throws Exception {
383
+ BuildOptions original = BuildOptions .of (BUILD_CONFIG_OPTIONS );
384
+
385
+ OptionsParser parser = OptionsParser .newOptionsParser (BUILD_CONFIG_OPTIONS );
386
+ parser .setStarlarkOptions (ImmutableMap .of ("@@@" , "hello" ));
387
+
388
+ assertThrows (IllegalArgumentException .class , () -> original .applyParsingResult (parser ));
389
+ }
344
390
}
0 commit comments