Skip to content

Commit a9419f3

Browse files
limdorcopybara-github
authored andcommitted
Fix common prefix for instrumentation filter
In case that two path share a prefix, the instrumentation filter is not computed proper. bazelbuild#10949 This PR takes as a base the one that was created by @caiyulun and adds some tests. Original PR: bazelbuild#10949 Closes bazelbuild#12607. PiperOrigin-RevId: 346097533
1 parent 923519f commit a9419f3

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/main/java/com/google/devtools/build/lib/buildtool/InstrumentationFilterSupport.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.devtools.build.lib.packages.Rule;
2626
import com.google.devtools.build.lib.packages.Target;
2727
import com.google.devtools.build.lib.packages.TargetUtils;
28+
import com.google.devtools.build.lib.vfs.PathFragment;
2829
import java.util.Collection;
2930
import java.util.Iterator;
3031
import java.util.Set;
@@ -124,9 +125,9 @@ private static void optimizeFilterSet(SortedSet<String> packageFilters) {
124125
if (iterator.hasNext()) {
125126
// Optimize away nested filters.
126127
iterator = packageFilters.iterator();
127-
String prev = iterator.next();
128+
PathFragment prev = PathFragment.create(iterator.next());
128129
while (iterator.hasNext()) {
129-
String current = iterator.next();
130+
PathFragment current = PathFragment.create(iterator.next());
130131
if (current.startsWith(prev)) {
131132
iterator.remove();
132133
} else {

src/test/java/com/google/devtools/build/lib/buildtool/BUILD

+15
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ java_test(
311311
],
312312
)
313313

314+
java_test(
315+
name = "InstrumentationFilterSupportTest",
316+
srcs = [
317+
"InstrumentationFilterSupportTest.java",
318+
],
319+
deps = [
320+
"//src/main/java/com/google/devtools/build/lib:runtime",
321+
"//src/main/java/com/google/devtools/build/lib/events",
322+
"//src/main/java/com/google/devtools/build/lib/packages",
323+
"//src/test/java/com/google/devtools/build/lib/analysis/util",
324+
"//third_party:junit4",
325+
"//third_party:truth",
326+
],
327+
)
328+
314329
java_test(
315330
name = "LabelCrossesPackageBoundaryTest",
316331
srcs = ["LabelCrossesPackageBoundaryTest.java"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2020 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+
package com.google.devtools.build.lib.buildtool;
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
18+
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
19+
import com.google.devtools.build.lib.events.EventCollector;
20+
import com.google.devtools.build.lib.events.EventKind;
21+
import com.google.devtools.build.lib.packages.Target;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Tests {@link com.google.devtools.build.lib.buildtool.InstrumentationFilterSupport}. */
31+
@RunWith(JUnit4.class)
32+
public class InstrumentationFilterSupportTest extends BuildViewTestCase {
33+
34+
@Test
35+
public void testComputeInstrumentationFilter() throws Exception {
36+
EventCollector events = new EventCollector(EventKind.INFO);
37+
scratch.file("foo/BUILD", "sh_test(name='t', srcs=['t.sh'])");
38+
scratch.file("foobar/BUILD", "sh_test(name='t', srcs=['t.sh'])");
39+
List<Target> listOfTargets = new ArrayList<>();
40+
listOfTargets.add(getTarget("//foo:t"));
41+
listOfTargets.add(getTarget("//foobar:t"));
42+
Collection<Target> targets = Collections.unmodifiableCollection(listOfTargets);
43+
String expectedFilter = "^//foo[/:],^//foobar[/:]";
44+
assertThat(InstrumentationFilterSupport.computeInstrumentationFilter(events, targets))
45+
.isEqualTo(expectedFilter);
46+
}
47+
}

0 commit comments

Comments
 (0)