-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce flags to allow filtering of targets by rule name #7
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,6 +381,29 @@ public static Predicate<Target> tagFilter(List<String> tagFilterList) { | |
}; | ||
} | ||
|
||
/** | ||
* Returns a predicate to be used for test rule name filtering, i.e., that only accepts tests that match | ||
* a required rule name and not an excluded rule name. | ||
*/ | ||
public static Predicate<Target> ruleFilter(List<String> ruleFilterList) { | ||
Pair<Collection<String>, Collection<String>> ruleLists = | ||
TestTargetUtils.sortTagsBySense(ruleFilterList); | ||
final Collection<String> requiredRules = ruleLists.first; | ||
final Collection<String> excludedRules = ruleLists.second; | ||
return input -> { | ||
if (requiredRules.isEmpty() && excludedRules.isEmpty()) { | ||
return true; | ||
} | ||
|
||
if (!(input instanceof Rule)) { | ||
return requiredRules.isEmpty(); | ||
} | ||
|
||
return TestTargetUtils.testMatchesRuleFilters( | ||
((Rule) input).getRuleClass(), requiredRules, excludedRules); | ||
Comment on lines
+402
to
+403
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
} | ||
|
||
/** Return {@link Location} for {@link Target} target, if it should not be null. */ | ||
@Nullable | ||
public static Location getLocationMaybe(Target target) { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,25 +46,29 @@ public static TestFilter forOptions(LoadingOptions options) { | |||||
ImmutableSet.copyOf(options.testSizeFilterSet), | ||||||
ImmutableSet.copyOf(options.testTimeoutFilterSet), | ||||||
ImmutableList.copyOf(options.testTagFilterList), | ||||||
ImmutableList.copyOf(options.testLangFilterList)); | ||||||
ImmutableList.copyOf(options.testLangFilterList), | ||||||
ImmutableList.copyOf(options.testRuleFilterList)); | ||||||
} | ||||||
|
||||||
private final ImmutableSet<TestSize> testSizeFilterSet; | ||||||
private final ImmutableSet<TestTimeout> testTimeoutFilterSet; | ||||||
private final ImmutableList<String> testTagFilterList; | ||||||
private final ImmutableList<String> testLangFilterList; | ||||||
private final ImmutableList<String> testRuleFilterList; | ||||||
private final Predicate<Target> impl; | ||||||
|
||||||
@VisibleForSerialization | ||||||
TestFilter( | ||||||
ImmutableSet<TestSize> testSizeFilterSet, | ||||||
ImmutableSet<TestTimeout> testTimeoutFilterSet, | ||||||
ImmutableList<String> testTagFilterList, | ||||||
ImmutableList<String> testLangFilterList) { | ||||||
ImmutableList<String> testLangFilterList, | ||||||
ImmutableList<String> testRuleFilterList) { | ||||||
this.testSizeFilterSet = testSizeFilterSet; | ||||||
this.testTimeoutFilterSet = testTimeoutFilterSet; | ||||||
this.testTagFilterList = testTagFilterList; | ||||||
this.testLangFilterList = testLangFilterList; | ||||||
this.testRuleFilterList = testRuleFilterList; | ||||||
Predicate<Target> testFilter = ALWAYS_TRUE; | ||||||
if (!testSizeFilterSet.isEmpty()) { | ||||||
testFilter = testFilter.and(testSizeFilter(testSizeFilterSet)); | ||||||
|
@@ -78,6 +82,9 @@ public static TestFilter forOptions(LoadingOptions options) { | |||||
if (!testLangFilterList.isEmpty()) { | ||||||
testFilter = testFilter.and(testLangFilter(testLangFilterList)); | ||||||
} | ||||||
if (!testRuleFilterList.isEmpty()) { | ||||||
testFilter = testFilter.and(testRuleFilter(testRuleFilterList)); | ||||||
} | ||||||
impl = testFilter; | ||||||
} | ||||||
|
||||||
|
@@ -89,7 +96,7 @@ public boolean apply(@Nullable Target input) { | |||||
@Override | ||||||
public int hashCode() { | ||||||
return Objects.hash(testSizeFilterSet, testTimeoutFilterSet, testTagFilterList, | ||||||
testLangFilterList); | ||||||
testLangFilterList, testRuleFilterList); | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
@@ -103,7 +110,8 @@ public boolean equals(Object o) { | |||||
return f.testSizeFilterSet.equals(testSizeFilterSet) | ||||||
&& f.testTimeoutFilterSet.equals(testTimeoutFilterSet) | ||||||
&& f.testTagFilterList.equals(testTagFilterList) | ||||||
&& f.testLangFilterList.equals(testLangFilterList); | ||||||
&& f.testLangFilterList.equals(testLangFilterList) | ||||||
&& f.testRuleFilterList.equals(testRuleFilterList); | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
@@ -113,6 +121,7 @@ public String toString() { | |||||
.add("testTimeoutFilterSet", testTimeoutFilterSet) | ||||||
.add("testTagFilterList", testTagFilterList) | ||||||
.add("testLangFilterList", testLangFilterList) | ||||||
.add("testRuleFilterList", testRuleFilterList) | ||||||
.toString(); | ||||||
} | ||||||
|
||||||
|
@@ -159,4 +168,28 @@ private static Predicate<Target> testLangFilter(List<String> langFilterList) { | |||||
&& !excludedLangs.contains(ruleLang); | ||||||
}; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns a predicate to be used for test rule filtering, i.e., that only accepts tests of | ||||||
* the specified rule names. | ||||||
*/ | ||||||
private static Predicate<Target> testRuleFilter(List<String> ruleFilterList) { | ||||||
final Set<String> requiredRules = new HashSet<>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the number of rule filters is expected to be large, consider using a more efficient data structure than
Suggested change
|
||||||
final Set<String> excludedRules = new HashSet<>(); | ||||||
|
||||||
for (String ruleFilter : ruleFilterList) { | ||||||
if (ruleFilter.startsWith("-")) { | ||||||
ruleFilter = ruleFilter.substring(1); | ||||||
excludedRules.add(ruleFilter); | ||||||
} else { | ||||||
requiredRules.add(ruleFilter); | ||||||
} | ||||||
} | ||||||
|
||||||
return rule -> { | ||||||
String ruleName = rule.getRuleClass(); | ||||||
return (requiredRules.isEmpty() || requiredRules.contains(ruleName)) | ||||||
&& !excludedRules.contains(ruleName); | ||||||
}; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a null check for
ruleFilterList
to prevent potentialNullPointerException
if the list is not initialized or passed as null.