aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-01-22 22:02:10 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-22 22:04:44 -0800
commit40dc4b9372962a2c0de13ff3dbe7735fa0dff07a (patch)
tree926d70370f125f94f3c2c51fc7f506eba0097f97 /src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java
parente734c479956df7a675c61f531d769609d3af3e5d (diff)
Add codec for TestFilter, and clean up some things: use java.util.function.Predicate and move some code that was only called by TestFilter inside it.
PiperOrigin-RevId: 182884550
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java136
1 files changed, 105 insertions, 31 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java b/src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java
index ee18660ca4..5dbed752e7 100644
--- a/src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java
+++ b/src/main/java/com/google/devtools/build/lib/pkgcache/TestFilter.java
@@ -13,66 +13,84 @@
// limitations under the License.
package com.google.devtools.build.lib.pkgcache;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
+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.packages.TestSize;
-import com.google.devtools.build.lib.packages.TestTargetUtils;
import com.google.devtools.build.lib.packages.TestTimeout;
+import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
+import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
+import java.util.function.Predicate;
import javax.annotation.Nullable;
/**
- * Predicate that implements test filtering using the command-line options in {@link LoadingOptions}.
- * Implements {@link #hashCode} and {@link #equals} so it can be used as a Skyframe key.
+ * Predicate that implements test filtering using the command-line options in {@link
+ * LoadingOptions}. Implements {@link #hashCode} and {@link #equals} so it can be used as a Skyframe
+ * key.
*/
-public final class TestFilter implements Predicate<Target> {
+@AutoCodec
+public final class TestFilter implements com.google.common.base.Predicate<Target> {
+ static final ObjectCodec<TestFilter> CODEC = new TestFilter_AutoCodec();
+
+ private static final Predicate<Target> ALWAYS_TRUE = (t) -> true;
+
/** Convert the options into a test filter. */
public static TestFilter forOptions(
LoadingOptions options, ExtendedEventHandler eventHandler, Set<String> ruleNames) {
- Predicate<Target> testFilter = Predicates.alwaysTrue();
- if (!options.testSizeFilterSet.isEmpty()) {
- testFilter = Predicates.and(testFilter,
- TestTargetUtils.testSizeFilter(options.testSizeFilterSet));
- }
- if (!options.testTimeoutFilterSet.isEmpty()) {
- testFilter = Predicates.and(testFilter,
- TestTargetUtils.testTimeoutFilter(options.testTimeoutFilterSet));
- }
- if (!options.testTagFilterList.isEmpty()) {
- testFilter = Predicates.and(testFilter,
- TargetUtils.tagFilter(options.testTagFilterList));
- }
if (!options.testLangFilterList.isEmpty()) {
- testFilter = Predicates.and(testFilter,
- TestTargetUtils.testLangFilter(options.testLangFilterList, eventHandler, ruleNames));
+ checkLangFilters(options.testLangFilterList, eventHandler, ruleNames);
}
- return new TestFilter(options.testSizeFilterSet, options.testTimeoutFilterSet,
- options.testTagFilterList, options.testLangFilterList, testFilter);
+ return new TestFilter(
+ ImmutableSet.copyOf(options.testSizeFilterSet),
+ ImmutableSet.copyOf(options.testTimeoutFilterSet),
+ ImmutableList.copyOf(options.testTagFilterList),
+ ImmutableList.copyOf(options.testLangFilterList));
}
- private final Set<TestSize> testSizeFilterSet;
- private final Set<TestTimeout> testTimeoutFilterSet;
- private final List<String> testTagFilterList;
- private final List<String> testLangFilterList;
+ private final ImmutableSet<TestSize> testSizeFilterSet;
+ private final ImmutableSet<TestTimeout> testTimeoutFilterSet;
+ private final ImmutableList<String> testTagFilterList;
+ private final ImmutableList<String> testLangFilterList;
private final Predicate<Target> impl;
- private TestFilter(Set<TestSize> testSizeFilterSet, Set<TestTimeout> testTimeoutFilterSet,
- List<String> testTagFilterList, List<String> testLangFilterList, Predicate<Target> impl) {
+ @AutoCodec.VisibleForSerialization
+ TestFilter(
+ ImmutableSet<TestSize> testSizeFilterSet,
+ ImmutableSet<TestTimeout> testTimeoutFilterSet,
+ ImmutableList<String> testTagFilterList,
+ ImmutableList<String> testLangFilterList) {
this.testSizeFilterSet = testSizeFilterSet;
this.testTimeoutFilterSet = testTimeoutFilterSet;
this.testTagFilterList = testTagFilterList;
this.testLangFilterList = testLangFilterList;
- this.impl = impl;
+ Predicate<Target> testFilter = ALWAYS_TRUE;
+ if (!testSizeFilterSet.isEmpty()) {
+ testFilter = testFilter.and(testSizeFilter(testSizeFilterSet));
+ }
+ if (!testTimeoutFilterSet.isEmpty()) {
+ testFilter = testFilter.and(testTimeoutFilter(testTimeoutFilterSet));
+ }
+ if (!testTagFilterList.isEmpty()) {
+ testFilter = testFilter.and(TargetUtils.tagFilter(testTagFilterList));
+ }
+ if (!testLangFilterList.isEmpty()) {
+ testFilter = testFilter.and(testLangFilter(testLangFilterList));
+ }
+ impl = testFilter;
}
@Override
public boolean apply(@Nullable Target input) {
- return impl.apply(input);
+ return impl.test(input);
}
@Override
@@ -95,4 +113,60 @@ public final class TestFilter implements Predicate<Target> {
&& f.testTagFilterList.equals(testTagFilterList)
&& f.testLangFilterList.equals(testLangFilterList);
}
+
+ /**
+ * Returns a predicate to be used for test size filtering, i.e., that only accepts tests of the
+ * given size.
+ */
+ @VisibleForTesting
+ public static Predicate<Target> testSizeFilter(final Set<TestSize> allowedSizes) {
+ return target ->
+ target instanceof Rule && allowedSizes.contains(TestSize.getTestSize((Rule) target));
+ }
+
+ /**
+ * Returns a predicate to be used for test timeout filtering, i.e., that only accepts tests of the
+ * given timeout.
+ */
+ @VisibleForTesting
+ public static Predicate<Target> testTimeoutFilter(final Set<TestTimeout> allowedTimeouts) {
+ return target ->
+ target instanceof Rule
+ && allowedTimeouts.contains(TestTimeout.getTestTimeout((Rule) target));
+ }
+
+ /** Check languages specified in --test_lang_filters and warn if any of them are unknown. */
+ private static void checkLangFilters(
+ List<String> langFilterList, ExtendedEventHandler reporter, Set<String> allRuleNames) {
+ for (String lang : langFilterList) {
+ if (!allRuleNames.contains(lang + "_test")) {
+ reporter.handle(
+ Event.warn("Unknown language '" + lang + "' in --test_lang_filters option"));
+ }
+ }
+ }
+
+ /**
+ * Returns a predicate to be used for test language filtering, i.e., that only accepts tests of
+ * the specified languages.
+ */
+ private static Predicate<Target> testLangFilter(List<String> langFilterList) {
+ final Set<String> requiredLangs = new HashSet<>();
+ final Set<String> excludedLangs = new HashSet<>();
+
+ for (String lang : langFilterList) {
+ if (lang.startsWith("-")) {
+ lang = lang.substring(1);
+ excludedLangs.add(lang);
+ } else {
+ requiredLangs.add(lang);
+ }
+ }
+
+ return rule -> {
+ String ruleLang = TargetUtils.getRuleLanguage(rule);
+ return (requiredLangs.isEmpty() || requiredLangs.contains(ruleLang))
+ && !excludedLangs.contains(ruleLang);
+ };
+ }
}