Skip to content

Commit e2f9700

Browse files
Add unit tests for rule name filters
1 parent dc65b92 commit e2f9700

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/test/java/com/google/devtools/build/lib/packages/TargetUtilsTest.java

+42
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,48 @@ public void testFilterByTag() throws Exception {
9696
assertThat(tagFilter.apply(tag1b)).isFalse();
9797
}
9898

99+
@Test
100+
public void testFilterByRuleName() throws Exception {
101+
scratch.file(
102+
"tests/BUILD",
103+
"""
104+
load("//test_defs:foo_binary.bzl", "foo_binary")
105+
load("//test_defs:foo_test.bzl", "foo_test")
106+
foo_binary(
107+
name = "foo_binary",
108+
)
109+
110+
foo_test(
111+
name = "foo_test",
112+
)
113+
""");
114+
115+
Target fooBinary = getTarget("//tests:foo_binary");
116+
Target fooTest = getTarget("//tests:foo_test");
117+
118+
Predicate<Target> ruleFilter = TargetUtils.ruleFilter(Lists.<String>newArrayList());
119+
assertThat(ruleFilter.apply(fooBinary)).isTrue();
120+
assertThat(ruleFilter.apply(fooTest)).isTrue();
121+
ruleFilter = TargetUtils.ruleFilter(Lists.newArrayList("foo_binary", "foo_test"));
122+
assertThat(ruleFilter.apply(fooBinary)).isTrue();
123+
assertThat(ruleFilter.apply(fooTest)).isTrue();
124+
ruleFilter = TargetUtils.ruleFilter(Lists.newArrayList("foo_binary"));
125+
assertThat(ruleFilter.apply(fooBinary)).isTrue();
126+
assertThat(ruleFilter.apply(fooTest)).isFalse();
127+
ruleFilter = TargetUtils.ruleFilter(Lists.newArrayList("-foo_test"));
128+
assertThat(ruleFilter.apply(fooBinary)).isTrue();
129+
assertThat(ruleFilter.apply(fooTest)).isFalse();
130+
// Applying same tag as positive and negative filter produces an empty
131+
// result because the negative filter is applied first and positive filter will
132+
// not match anything.
133+
ruleFilter = TargetUtils.ruleFilter(Lists.newArrayList("foo_test", "-foo_test"));
134+
assertThat(ruleFilter.apply(fooBinary)).isFalse();
135+
assertThat(ruleFilter.apply(fooTest)).isFalse();
136+
ruleFilter = TargetUtils.ruleFilter(Lists.newArrayList("foo_test", "-foo_binary"));
137+
assertThat(ruleFilter.apply(fooBinary)).isFalse();
138+
assertThat(ruleFilter.apply(fooTest)).isTrue();
139+
}
140+
99141
@Test
100142
public void testExecutionInfo() throws Exception {
101143
scratch.file(

src/test/java/com/google/devtools/build/lib/packages/TestTargetUtilsTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,34 @@ public void testFilterBySize() {
118118
public void testFilterByLang() {
119119
LoadingOptions options = new LoadingOptions();
120120
options.testLangFilterList = ImmutableList.of("positive", "-negative");
121+
options.testRuleFilterList = ImmutableList.of();
122+
options.testSizeFilterSet = ImmutableSet.of();
123+
options.testTimeoutFilterSet = ImmutableSet.of();
124+
options.testTagFilterList = ImmutableList.of();
125+
TestFilter filter = TestFilter.forOptions(options);
126+
Package pkg = mock(Package.class);
127+
RuleClass ruleClass = mock(RuleClass.class);
128+
when(ruleClass.getDefaultImplicitOutputsFunction())
129+
.thenReturn(SafeImplicitOutputsFunction.NONE);
130+
when(ruleClass.getAttributeProvider()).thenReturn(mock(AttributeProvider.class));
131+
Rule mockRule =
132+
new Rule(
133+
pkg,
134+
Label.parseCanonicalUnchecked("//pkg:a"),
135+
ruleClass,
136+
Location.fromFile(""),
137+
/* interiorCallStack= */ null);
138+
when(ruleClass.getName()).thenReturn("positive_test");
139+
assertThat(filter.apply(mockRule)).isTrue();
140+
when(ruleClass.getName()).thenReturn("negative_test");
141+
assertThat(filter.apply(mockRule)).isFalse();
142+
}
143+
144+
@Test
145+
public void testFilterByRule() {
146+
LoadingOptions options = new LoadingOptions();
147+
options.testLangFilterList = ImmutableList.of();
148+
options.testRuleFilterList = ImmutableList.of("positive_test", "-negative_test");
121149
options.testSizeFilterSet = ImmutableSet.of();
122150
options.testTimeoutFilterSet = ImmutableSet.of();
123151
options.testTagFilterList = ImmutableList.of();

src/test/java/com/google/devtools/build/lib/skyframe/TargetPatternPhaseKeyTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void testEquality() throws Exception {
5353
ImmutableList.of(),
5454
PathFragment.EMPTY_FRAGMENT,
5555
ImmutableList.of(),
56+
ImmutableList.of(),
5657
false,
5758
true,
5859
null,
@@ -62,6 +63,7 @@ public void testEquality() throws Exception {
6263
ImmutableList.of(),
6364
PathFragment.EMPTY_FRAGMENT,
6465
ImmutableList.of(),
66+
ImmutableList.of(),
6567
false,
6668
false,
6769
null,
@@ -71,6 +73,7 @@ public void testEquality() throws Exception {
7173
ImmutableList.of(),
7274
PathFragment.EMPTY_FRAGMENT,
7375
ImmutableList.of(),
76+
ImmutableList.of(),
7477
true,
7578
true,
7679
null,
@@ -80,6 +83,7 @@ public void testEquality() throws Exception {
8083
ImmutableList.of(),
8184
PathFragment.EMPTY_FRAGMENT,
8285
ImmutableList.of(),
86+
ImmutableList.of(),
8387
true,
8488
false,
8589
null,
@@ -89,6 +93,7 @@ public void testEquality() throws Exception {
8993
ImmutableList.of(),
9094
PathFragment.EMPTY_FRAGMENT,
9195
ImmutableList.of(),
96+
ImmutableList.of(),
9297
false,
9398
true,
9499
emptyTestFilter(),
@@ -98,6 +103,7 @@ public void testEquality() throws Exception {
98103
ImmutableList.of(),
99104
PathFragment.EMPTY_FRAGMENT,
100105
ImmutableList.of(),
106+
ImmutableList.of(),
101107
true,
102108
true,
103109
emptyTestFilter(),
@@ -107,6 +113,7 @@ public void testEquality() throws Exception {
107113
ImmutableList.of(),
108114
PathFragment.EMPTY_FRAGMENT,
109115
ImmutableList.of(),
116+
ImmutableList.of(),
110117
false,
111118
true,
112119
emptyTestFilter(),
@@ -116,6 +123,7 @@ public void testEquality() throws Exception {
116123
ImmutableList.of(),
117124
PathFragment.EMPTY_FRAGMENT,
118125
ImmutableList.of(),
126+
ImmutableList.of(),
119127
true,
120128
true,
121129
emptyTestFilter(),
@@ -125,6 +133,7 @@ public void testEquality() throws Exception {
125133
ImmutableList.of(),
126134
PathFragment.EMPTY_FRAGMENT,
127135
ImmutableList.of("a"),
136+
ImmutableList.of("a"),
128137
false,
129138
true,
130139
null))
@@ -133,6 +142,7 @@ public void testEquality() throws Exception {
133142
ImmutableList.of(),
134143
PathFragment.EMPTY_FRAGMENT,
135144
ImmutableList.of("a"),
145+
ImmutableList.of("a"),
136146
true,
137147
true,
138148
null))
@@ -143,6 +153,7 @@ private static TargetPatternPhaseKey of(
143153
ImmutableList<String> targetPatterns,
144154
PathFragment offset,
145155
ImmutableList<String> buildTagFilter,
156+
ImmutableList<String> buildRuleFilter,
146157
boolean includeManualTests,
147158
boolean expandTestSuites,
148159
@Nullable TestFilter testFilter,
@@ -158,14 +169,15 @@ private static TargetPatternPhaseKey of(
158169
buildTestsOnly,
159170
determineTests,
160171
buildTagFilter,
172+
buildRuleFilter,
161173
includeManualTests,
162174
expandTestSuites,
163175
testFilter);
164176
}
165177

166178
private static TargetPatternPhaseKey of(
167179
ImmutableList<String> targetPatterns, PathFragment offset) {
168-
return of(targetPatterns, offset, ImmutableList.of(), false, true, null);
180+
return of(targetPatterns, offset, ImmutableList.of(), ImmutableList.of(), false, true, null);
169181
}
170182

171183
private static TestFilter emptyTestFilter() {

0 commit comments

Comments
 (0)