mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-23 00:21:29 +00:00
[TEST] Add ability to exclude some testdata with pattern in test generator
This commit is contained in:
@@ -857,6 +857,17 @@ public class KotlinTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentByMetadataWithExcluded(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludedPattern,
|
||||
boolean recursive,
|
||||
@NotNull String... excludeDirs
|
||||
) {
|
||||
assertAllTestsPresentByMetadataWithExcluded(testCaseClass, testDataDir, filenamePattern, excludedPattern, TargetBackend.ANY, recursive, excludeDirs);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentByMetadata(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@@ -874,10 +885,11 @@ public class KotlinTestUtils {
|
||||
);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentByMetadata(
|
||||
public static void assertAllTestsPresentByMetadataWithExcluded(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludedPattern,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean recursive,
|
||||
@NotNull String... excludeDirs
|
||||
@@ -891,17 +903,31 @@ public class KotlinTestUtils {
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
if (recursive && containsTestData(file, filenamePattern) && !exclude.contains(file.getName())) {
|
||||
if (recursive && containsTestData(file, filenamePattern, excludedPattern) && !exclude.contains(file.getName())) {
|
||||
assertTestClassPresentByMetadata(testCaseClass, file);
|
||||
}
|
||||
}
|
||||
else if (filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
|
||||
assertFilePathPresent(file, rootFile, filePaths);
|
||||
else {
|
||||
boolean excluded = excludedPattern != null && excludedPattern.matcher(file.getName()).matches();
|
||||
if (!excluded && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
|
||||
assertFilePathPresent(file, rootFile, filePaths);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentByMetadata(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean recursive,
|
||||
@NotNull String... excludeDirs
|
||||
) {
|
||||
assertAllTestsPresentByMetadataWithExcluded(testCaseClass, testDataDir, filenamePattern, null, targetBackend, recursive, excludeDirs);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentInSingleGeneratedClass(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@@ -910,18 +936,38 @@ public class KotlinTestUtils {
|
||||
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, TargetBackend.ANY);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentInSingleGeneratedClassWithExcluded(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludePattern
|
||||
) {
|
||||
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, excludePattern, TargetBackend.ANY);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentInSingleGeneratedClass(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@NotNull TargetBackend targetBackend
|
||||
) {
|
||||
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, null, targetBackend);
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentInSingleGeneratedClass(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludePattern,
|
||||
@NotNull TargetBackend targetBackend
|
||||
) {
|
||||
File rootFile = new File(getTestsRoot(testCaseClass));
|
||||
|
||||
Set<String> filePaths = collectPathsMetadata(testCaseClass);
|
||||
|
||||
FileUtil.processFilesRecursively(testDataDir, file -> {
|
||||
if (file.isFile() && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
|
||||
boolean excluded = excludePattern != null && excludePattern.matcher(file.getName()).matches();
|
||||
if (file.isFile() && !excluded && filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) {
|
||||
assertFilePathPresent(file, rootFile, filePaths);
|
||||
}
|
||||
|
||||
@@ -960,17 +1006,18 @@ public class KotlinTestUtils {
|
||||
return filePaths;
|
||||
}
|
||||
|
||||
private static boolean containsTestData(File dir, Pattern filenamePattern) {
|
||||
private static boolean containsTestData(File dir, Pattern filenamePattern, @Nullable Pattern excludedPattern) {
|
||||
File[] files = dir.listFiles();
|
||||
assert files != null;
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
if (containsTestData(file, filenamePattern)) {
|
||||
if (containsTestData(file, filenamePattern, excludedPattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (filenamePattern.matcher(file.getName()).matches()) {
|
||||
boolean excluded = excludedPattern != null && excludedPattern.matcher(file.getName()).matches();
|
||||
if (! excluded && filenamePattern.matcher(file.getName()).matches()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@Nullable
|
||||
private final Pattern excludePattern;
|
||||
@Nullable
|
||||
private final Boolean checkFilenameStartsLowerCase;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@@ -56,6 +58,7 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
boolean recursive,
|
||||
boolean excludeParentDirs,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludedPattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@@ -71,6 +74,7 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
this.recursive = recursive;
|
||||
this.excludeParentDirs = excludeParentDirs;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.excludePattern = excludedPattern;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
this.targetBackend = targetBackend;
|
||||
@@ -98,7 +102,7 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
if (file.isDirectory() && dirHasFilesInside(file) && !excludeDirs.contains(file.getName())) {
|
||||
String innerTestClassName = TestGeneratorUtil.fileNameToJavaIdentifier(file);
|
||||
children.add(new SimpleTestClassModel(
|
||||
file, true, excludeParentDirs, filenamePattern, checkFilenameStartsLowerCase,
|
||||
file, true, excludeParentDirs, filenamePattern, excludePattern, checkFilenameStartsLowerCase,
|
||||
doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()),
|
||||
skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep != null ? deep - 1 : null, annotations)
|
||||
);
|
||||
@@ -173,7 +177,8 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
|
||||
if (listFiles != null && (deep == null || deep == 0)) {
|
||||
for (File file : listFiles) {
|
||||
if (filenamePattern.matcher(file.getName()).matches()) {
|
||||
boolean excluded = excludePattern != null && excludePattern.matcher(file.getName()).matches();
|
||||
if (filenamePattern.matcher(file.getName()).matches() && !excluded) {
|
||||
|
||||
if (file.isDirectory() && excludeParentDirs && dirHasSubDirs(file)) {
|
||||
continue;
|
||||
@@ -252,19 +257,25 @@ public class SimpleTestClassModel extends TestClassModel {
|
||||
exclude.append("\"");
|
||||
}
|
||||
|
||||
String excludedArgument;
|
||||
if (excludePattern != null) {
|
||||
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
|
||||
} else {
|
||||
excludedArgument = null;
|
||||
}
|
||||
|
||||
String assertTestsPresentStr;
|
||||
|
||||
if (targetBackend == TargetBackend.ANY) {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
recursive, exclude
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument, recursive, exclude
|
||||
);
|
||||
} else {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);",
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
|
||||
excludedArgument, TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ public class SingleClassTestModel extends TestClassModel {
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@Nullable
|
||||
private final Pattern excludePattern;
|
||||
@Nullable
|
||||
private final Boolean checkFilenameStartsLowerCase;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@@ -59,6 +61,7 @@ public class SingleClassTestModel extends TestClassModel {
|
||||
public SingleClassTestModel(
|
||||
@NotNull File rootFile,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@@ -70,6 +73,7 @@ public class SingleClassTestModel extends TestClassModel {
|
||||
) {
|
||||
this.rootFile = rootFile;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.excludePattern = excludePattern;
|
||||
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
@@ -157,16 +161,23 @@ public class SingleClassTestModel extends TestClassModel {
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
String assertTestsPresentStr;
|
||||
|
||||
String excludedArgument;
|
||||
if (excludePattern != null) {
|
||||
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
|
||||
} else {
|
||||
excludedArgument = null;
|
||||
}
|
||||
|
||||
if (targetBackend != TargetBackend.ANY) {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);",
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
TargetBackend.class.getSimpleName(), targetBackend.toString()
|
||||
excludedArgument, TargetBackend.class.getSimpleName(), targetBackend.toString()
|
||||
);
|
||||
} else {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"));",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern())
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument
|
||||
);
|
||||
}
|
||||
p.println(assertTestsPresentStr);
|
||||
|
||||
@@ -52,6 +52,7 @@ class TestGroup(
|
||||
excludeParentDirs: Boolean = false,
|
||||
extension: String? = "kt", // null string means dir (name without dot)
|
||||
pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$",
|
||||
excludedPattern: String? = null,
|
||||
testMethod: String = "doTest",
|
||||
singleClass: Boolean = false,
|
||||
testClassName: String? = null,
|
||||
@@ -63,18 +64,19 @@ class TestGroup(
|
||||
) {
|
||||
val rootFile = File("$testDataRoot/$relativeRootPath")
|
||||
val compiledPattern = Pattern.compile(pattern)
|
||||
val compiledExcludedPattern = excludedPattern?.let { Pattern.compile(it) }
|
||||
val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile)
|
||||
testModels.add(
|
||||
if (singleClass) {
|
||||
if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet")
|
||||
SingleClassTestModel(
|
||||
rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend,
|
||||
skipIgnored, testRunnerMethodName, additionalRunnerArguments, annotations
|
||||
rootFile, compiledPattern, compiledExcludedPattern, filenameStartsLowerCase, testMethod, className,
|
||||
targetBackend, skipIgnored, testRunnerMethodName, additionalRunnerArguments, annotations
|
||||
)
|
||||
} else {
|
||||
SimpleTestClassModel(
|
||||
rootFile, recursive, excludeParentDirs,
|
||||
compiledPattern, filenameStartsLowerCase, testMethod, className,
|
||||
compiledPattern, compiledExcludedPattern, filenameStartsLowerCase, testMethod, className,
|
||||
targetBackend, excludeDirs, skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep, annotations
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user