blob: 604fa64b4f438dfd3be1290927fc60b1706b3fce [file] [log] [blame]
Alex Humesky152feb02016-06-20 19:43:11 +00001// Copyright 2016 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
15package com.google.devtools.build.lib.analysis;
16
17import static com.google.common.truth.Truth.assertThat;
adgarbce6d262021-05-03 13:56:28 -070018import static com.google.devtools.build.lib.analysis.OutputGroupInfo.DEFAULT;
dslomov69c45f82017-12-14 11:15:43 -050019import static com.google.devtools.build.lib.analysis.OutputGroupInfo.determineOutputGroups;
Alex Humesky152feb02016-06-20 19:43:11 +000020import static java.util.Arrays.asList;
21
adgarbce6d262021-05-03 13:56:28 -070022import com.google.common.collect.ImmutableList;
Alex Humesky152feb02016-06-20 19:43:11 +000023import com.google.common.collect.ImmutableSet;
kmb0d897402021-04-22 09:42:46 -070024import com.google.devtools.build.lib.analysis.OutputGroupInfo.ValidationMode;
adgarbce6d262021-05-03 13:56:28 -070025import com.google.testing.junit.testparameterinjector.TestParameter;
26import com.google.testing.junit.testparameterinjector.TestParameterInjector;
ahumesky60205bb2019-10-14 13:50:00 -070027import java.util.Set;
Alex Humesky152feb02016-06-20 19:43:11 +000028import org.junit.Test;
29import org.junit.runner.RunWith;
Alex Humesky152feb02016-06-20 19:43:11 +000030
adgarbce6d262021-05-03 13:56:28 -070031/** Tests for {@link OutputGroupInfo}. */
32@RunWith(TestParameterInjector.class)
Alex Humesky152feb02016-06-20 19:43:11 +000033public final class OutputGroupProviderTest {
34
35 @Test
adgarbce6d262021-05-03 13:56:28 -070036 public void testDetermineOutputGroupsOverridesDefaults(@TestParameter boolean shouldRunTests) {
Alex Humesky152feb02016-06-20 19:43:11 +000037 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -070038 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -070039 ImmutableSet.of("x", "y", "z"),
40 asList("a", "b", "c"),
41 ValidationMode.OFF,
42 /*shouldRunTests=*/ shouldRunTests);
43 assertThat(outputGroups)
44 .containsExactlyElementsIn(
45 outputGroupsWithDefaultIfRunningTests(shouldRunTests, "a", "b", "c"));
Alex Humesky152feb02016-06-20 19:43:11 +000046 }
47
48 @Test
adgarbce6d262021-05-03 13:56:28 -070049 public void testDetermineOutputGroupsAddsToDefaults(@TestParameter boolean shouldRunTests) {
Alex Humesky152feb02016-06-20 19:43:11 +000050 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -070051 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -070052 ImmutableSet.of("x", "y", "z"),
53 asList("+a"),
54 ValidationMode.OFF,
55 /*shouldRunTests=*/ shouldRunTests);
56 assertThat(outputGroups)
57 .containsExactlyElementsIn(
58 outputGroupsWithDefaultIfRunningTests(shouldRunTests, "x", "y", "z", "a"));
59 }
60
61 @Test
62 public void testDetermineOutputGroupsRemovesFromDefaults(@TestParameter boolean shouldRunTests) {
63 Set<String> outputGroups =
64 determineOutputGroups(
65 ImmutableSet.of("x", "y", "z"),
66 asList("-y"),
67 ValidationMode.OFF,
68 /*shouldRunTests=*/ shouldRunTests);
69 assertThat(outputGroups)
70 .containsExactlyElementsIn(outputGroupsWithDefaultIfRunningTests(shouldRunTests, "x", "z"));
71 }
72
73 @Test
74 public void testDetermineOutputGroupsMixedOverrideAdditionOverrides(
75 @TestParameter boolean shouldRunTests) {
76 Set<String> outputGroups =
77 determineOutputGroups(
78 ImmutableSet.of("x", "y", "z"),
79 asList("a", "+b"),
80 ValidationMode.OFF,
81 /*shouldRunTests=*/ shouldRunTests);
Alex Humesky152feb02016-06-20 19:43:11 +000082 // The plain "a" causes the default output groups to be overridden.
adgarbce6d262021-05-03 13:56:28 -070083 assertThat(outputGroups)
84 .containsExactlyElementsIn(outputGroupsWithDefaultIfRunningTests(shouldRunTests, "a", "b"));
Alex Humesky152feb02016-06-20 19:43:11 +000085 }
86
87 @Test
adgarbce6d262021-05-03 13:56:28 -070088 public void testDetermineOutputGroupsIgnoresUnknownGroup(@TestParameter boolean shouldRunTests) {
Alex Humesky152feb02016-06-20 19:43:11 +000089 Set<String> outputGroups =
adgarbce6d262021-05-03 13:56:28 -070090 determineOutputGroups(
91 ImmutableSet.of("x", "y", "z"),
92 asList("-foo"),
93 ValidationMode.OFF,
94 /*shouldRunTests=*/ shouldRunTests);
Alex Humesky152feb02016-06-20 19:43:11 +000095 // "foo" doesn't exist, but that shouldn't be a problem.
adgarbce6d262021-05-03 13:56:28 -070096 assertThat(outputGroups)
97 .containsExactlyElementsIn(
98 outputGroupsWithDefaultIfRunningTests(shouldRunTests, "x", "y", "z"));
Alex Humesky152feb02016-06-20 19:43:11 +000099 }
100
101 @Test
adgarbce6d262021-05-03 13:56:28 -0700102 public void testDetermineOutputGroupsRemovesPreviouslyAddedGroup(
103 @TestParameter boolean shouldRunTests) {
Alex Humesky152feb02016-06-20 19:43:11 +0000104 Set<String> outputGroups;
kmb0d897402021-04-22 09:42:46 -0700105 outputGroups =
106 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700107 ImmutableSet.of("x", "y", "z"),
108 asList("+a", "-a"),
109 ValidationMode.OFF,
110 /*shouldRunTests=*/ shouldRunTests);
111 assertThat(outputGroups)
112 .containsExactlyElementsIn(
113 outputGroupsWithDefaultIfRunningTests(shouldRunTests, "x", "y", "z"));
Alex Humesky152feb02016-06-20 19:43:11 +0000114
115 // Order matters here.
kmb0d897402021-04-22 09:42:46 -0700116 outputGroups =
117 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700118 ImmutableSet.of("x", "y", "z"),
119 asList("-a", "+a"),
120 ValidationMode.OFF,
121 /*shouldRunTests=*/ shouldRunTests);
122 assertThat(outputGroups)
123 .containsExactlyElementsIn(
124 outputGroupsWithDefaultIfRunningTests(shouldRunTests, "x", "y", "z", "a"));
Alex Humesky152feb02016-06-20 19:43:11 +0000125 }
ahumesky60205bb2019-10-14 13:50:00 -0700126
127 @Test
adgarbce6d262021-05-03 13:56:28 -0700128 public void testDetermineOutputGroupsContainsValidationGroup(
129 @TestParameter boolean shouldRunTests) {
ahumesky60205bb2019-10-14 13:50:00 -0700130 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -0700131 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700132 ImmutableSet.of("x", "y", "z"),
133 asList(),
134 ValidationMode.OUTPUT_GROUP,
135 /*shouldRunTests=*/ shouldRunTests);
136 assertThat(outputGroups)
137 .containsExactlyElementsIn(
138 outputGroupsWithDefaultIfRunningTests(
139 shouldRunTests, "x", "y", "z", OutputGroupInfo.VALIDATION));
ahumesky60205bb2019-10-14 13:50:00 -0700140 }
141
142 @Test
adgarbce6d262021-05-03 13:56:28 -0700143 public void testDetermineOutputGroupsContainsValidationGroupAfterOverride(
144 @TestParameter boolean shouldRunTests) {
ahumesky60205bb2019-10-14 13:50:00 -0700145 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -0700146 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700147 ImmutableSet.of("x", "y", "z"),
148 asList("foo"),
149 ValidationMode.OUTPUT_GROUP,
150 /*shouldRunTests=*/ shouldRunTests);
151 assertThat(outputGroups)
152 .containsExactlyElementsIn(
153 outputGroupsWithDefaultIfRunningTests(
154 shouldRunTests, "foo", OutputGroupInfo.VALIDATION));
ahumesky60205bb2019-10-14 13:50:00 -0700155 }
156
157 @Test
adgarbce6d262021-05-03 13:56:28 -0700158 public void testDetermineOutputGroupsContainsValidationGroupAfterAdd(
159 @TestParameter boolean shouldRunTests) {
ahumesky60205bb2019-10-14 13:50:00 -0700160 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -0700161 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700162 ImmutableSet.of("x", "y", "z"),
163 asList("+a"),
164 ValidationMode.OUTPUT_GROUP,
165 /*shouldRunTests=*/ shouldRunTests);
166 assertThat(outputGroups)
167 .containsExactlyElementsIn(
168 outputGroupsWithDefaultIfRunningTests(
169 shouldRunTests, "x", "y", "z", "a", OutputGroupInfo.VALIDATION));
ahumesky60205bb2019-10-14 13:50:00 -0700170 }
171
172 @Test
adgarbce6d262021-05-03 13:56:28 -0700173 public void testDetermineOutputGroupsContainsValidationGroupAfterRemove(
174 @TestParameter boolean shouldRunTests) {
ahumesky60205bb2019-10-14 13:50:00 -0700175 Set<String> outputGroups =
kmb0d897402021-04-22 09:42:46 -0700176 determineOutputGroups(
adgarbce6d262021-05-03 13:56:28 -0700177 ImmutableSet.of("x", "y", "z"),
178 asList("-x"),
179 ValidationMode.OUTPUT_GROUP,
180 /*shouldRunTests=*/ shouldRunTests);
181 assertThat(outputGroups)
182 .containsExactlyElementsIn(
183 outputGroupsWithDefaultIfRunningTests(
184 shouldRunTests, "y", "z", OutputGroupInfo.VALIDATION));
ahumesky60205bb2019-10-14 13:50:00 -0700185 }
kmb0d897402021-04-22 09:42:46 -0700186
187 @Test
adgarbce6d262021-05-03 13:56:28 -0700188 public void testDetermineOutputGroupsContainsValidationGroupDespiteRemove(
189 @TestParameter boolean shouldRunTests) {
kmb0d897402021-04-22 09:42:46 -0700190 Set<String> outputGroups =
191 determineOutputGroups(
192 ImmutableSet.of("x", "y", "z"),
193 asList("-" + OutputGroupInfo.VALIDATION),
adgarbce6d262021-05-03 13:56:28 -0700194 ValidationMode.OUTPUT_GROUP,
195 /*shouldRunTests=*/ shouldRunTests);
196 assertThat(outputGroups)
197 .containsExactlyElementsIn(
198 outputGroupsWithDefaultIfRunningTests(
199 shouldRunTests, "x", "y", "z", OutputGroupInfo.VALIDATION));
kmb0d897402021-04-22 09:42:46 -0700200 }
201
202 @Test
adgarbce6d262021-05-03 13:56:28 -0700203 public void testDetermineOutputGroupsContainsTopLevelValidationGroup(
204 @TestParameter boolean shouldRunTests) {
kmb0d897402021-04-22 09:42:46 -0700205 Set<String> outputGroups =
206 determineOutputGroups(
207 ImmutableSet.of("x", "y", "z"),
208 asList("-" + OutputGroupInfo.VALIDATION_TOP_LEVEL),
adgarbce6d262021-05-03 13:56:28 -0700209 ValidationMode.ASPECT,
210 /*shouldRunTests=*/ shouldRunTests);
211 assertThat(outputGroups)
212 .containsExactlyElementsIn(
213 outputGroupsWithDefaultIfRunningTests(
214 shouldRunTests, "x", "y", "z", OutputGroupInfo.VALIDATION_TOP_LEVEL));
215 }
216
217 private static Iterable<String> outputGroupsWithDefaultIfRunningTests(
218 boolean shouldRunTests, String... groups) {
219 ImmutableList.Builder<String> result = ImmutableList.builder();
220 result.add(groups);
221 if (shouldRunTests) {
222 result.add(DEFAULT);
223 }
224 return result.build();
kmb0d897402021-04-22 09:42:46 -0700225 }
Alex Humesky152feb02016-06-20 19:43:11 +0000226}