Skip to content

Commit f79cf6b

Browse files
authored
Add ability to selectively run spec tests via a -DexclusiveSpecTest flag to java (#1367)
1 parent a558c1e commit f79cf6b

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/spec/SpecTestCase.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.firestore.spec;
1616

17+
import static com.google.common.base.Strings.emptyToNull;
1718
import static com.google.firebase.firestore.TestUtil.waitFor;
1819
import static com.google.firebase.firestore.testutil.TestUtil.ARBITRARY_SEQUENCE_NUMBER;
1920
import static com.google.firebase.firestore.testutil.TestUtil.deleteMutation;
@@ -93,6 +94,7 @@
9394
import java.util.Map;
9495
import java.util.Set;
9596
import java.util.logging.Logger;
97+
import java.util.regex.Pattern;
9698
import java.util.stream.Stream;
9799
import javax.annotation.Nullable;
98100
import org.json.JSONArray;
@@ -132,6 +134,16 @@ public abstract class SpecTestCase implements RemoteStoreCallback {
132134
// this tag and they'll all be run (but all others won't).
133135
private static final String EXCLUSIVE_TAG = "exclusive";
134136

137+
// The name of a Java system property ({@link System#getProperty(String)}) whose value is a filter
138+
// that specifies which tests to execute. The value of this property is a regular expression that
139+
// is matched against the name of each test. Using this property is an alternative to setting the
140+
// {@link #EXCLUSIVE_TAG} tag, which requires modifying the JSON file. To use this property,
141+
// specify -DspecTestFilter=<Regex> to the Java runtime, replacing <Regex> with a regular
142+
// expression; a test will be executed if and only if its name matches this regular expression.
143+
// In this context, a test's "name" is the result of appending its "itName" to its "describeName",
144+
// separated by a space character.
145+
private static final String TEST_FILTER_PROPERTY = "specTestFilter";
146+
135147
// Tags on tests that should be excluded from execution, useful to allow the platforms to
136148
// temporarily diverge or for features that are designed to be platform specific (such as
137149
// 'multi-client').
@@ -1097,6 +1109,16 @@ public void testSpecTests() throws Exception {
10971109
parsedSpecFiles.add(new Pair<>(f.getName(), fileJSON));
10981110
}
10991111

1112+
String testNameFilterFromSystemProperty =
1113+
emptyToNull(System.getProperty(TEST_FILTER_PROPERTY));
1114+
Pattern testNameFilter;
1115+
if (testNameFilterFromSystemProperty == null) {
1116+
testNameFilter = null;
1117+
} else {
1118+
exclusiveMode = true;
1119+
testNameFilter = Pattern.compile(testNameFilterFromSystemProperty);
1120+
}
1121+
11001122
for (Pair<String, JSONObject> parsedSpecFile : parsedSpecFiles) {
11011123
String fileName = parsedSpecFile.first;
11021124
JSONObject fileJSON = parsedSpecFile.second;
@@ -1115,7 +1137,19 @@ public void testSpecTests() throws Exception {
11151137
JSONArray steps = testJSON.getJSONArray("steps");
11161138
Set<String> tags = getTestTags(testJSON);
11171139

1118-
boolean runTest = shouldRunTest(tags) && (!exclusiveMode || tags.contains(EXCLUSIVE_TAG));
1140+
boolean runTest;
1141+
if (!shouldRunTest(tags)) {
1142+
runTest = false;
1143+
} else if (!exclusiveMode) {
1144+
runTest = true;
1145+
} else if (tags.contains(EXCLUSIVE_TAG)) {
1146+
runTest = true;
1147+
} else if (testNameFilter != null) {
1148+
runTest = testNameFilter.matcher(name).find();
1149+
} else {
1150+
runTest = false;
1151+
}
1152+
11191153
boolean measureRuntime = tags.contains(BENCHMARK_TAG);
11201154
if (runTest) {
11211155
long start = System.currentTimeMillis();

0 commit comments

Comments
 (0)