Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix common prefix for instrumentation filter #12607

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 java.io.IOException;
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<Target>();
listOfTargets.add(getTarget("//foo:t"));
listOfTargets.add(getTarget("//foobar:t"));
Collection<Target> targets = Collections.unmodifiableCollection(listOfTargets);
String expectedFilter = "^//foo[/:],^//foobar[/:]";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If prefered, I could split the PR in two commits to see more clear how the test would change with the changes.
Before the this PR the expected Filter would have been just ^//foo[/:]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. We squash the PR into a single commit anyway, so there's limited value in splitting everything up incrementally in this case.

assertThat(InstrumentationFilterSupport.computeInstrumentationFilter(events, targets)).isEqualTo(expectedFilter);
}
}