Skip to content

Commit

Permalink
Fix common prefix for instrumentation filter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
limdor authored and Copybara-Service committed Dec 7, 2020
1 parent 923519f commit a9419f3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
Expand Down Expand Up @@ -124,9 +125,9 @@ private static void optimizeFilterSet(SortedSet<String> packageFilters) {
if (iterator.hasNext()) {
// Optimize away nested filters.
iterator = packageFilters.iterator();
String prev = iterator.next();
PathFragment prev = PathFragment.create(iterator.next());
while (iterator.hasNext()) {
String current = iterator.next();
PathFragment current = PathFragment.create(iterator.next());
if (current.startsWith(prev)) {
iterator.remove();
} else {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/google/devtools/build/lib/buildtool/BUILD
Expand Up @@ -311,6 +311,21 @@ java_test(
],
)

java_test(
name = "InstrumentationFilterSupportTest",
srcs = [
"InstrumentationFilterSupportTest.java",
],
deps = [
"//src/main/java/com/google/devtools/build/lib:runtime",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//third_party:junit4",
"//third_party:truth",
],
)

java_test(
name = "LabelCrossesPackageBoundaryTest",
srcs = ["LabelCrossesPackageBoundaryTest.java"],
Expand Down
@@ -0,0 +1,47 @@
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.buildtool;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.events.EventCollector;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.packages.Target;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests {@link com.google.devtools.build.lib.buildtool.InstrumentationFilterSupport}. */
@RunWith(JUnit4.class)
public class InstrumentationFilterSupportTest extends BuildViewTestCase {

@Test
public void testComputeInstrumentationFilter() throws Exception {
EventCollector events = new EventCollector(EventKind.INFO);
scratch.file("foo/BUILD", "sh_test(name='t', srcs=['t.sh'])");
scratch.file("foobar/BUILD", "sh_test(name='t', srcs=['t.sh'])");
List<Target> listOfTargets = new ArrayList<>();
listOfTargets.add(getTarget("//foo:t"));
listOfTargets.add(getTarget("//foobar:t"));
Collection<Target> targets = Collections.unmodifiableCollection(listOfTargets);
String expectedFilter = "^//foo[/:],^//foobar[/:]";
assertThat(InstrumentationFilterSupport.computeInstrumentationFilter(events, targets))
.isEqualTo(expectedFilter);
}
}

0 comments on commit a9419f3

Please sign in to comment.