[TEST] Add ability to exclude some testdata with pattern in test generator

This commit is contained in:
Dmitriy Novozhilov
2019-12-11 15:32:58 +03:00
parent ae15fa7676
commit 5b1f96ba1b
4 changed files with 93 additions and 22 deletions
@@ -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( public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@@ -874,10 +885,11 @@ public class KotlinTestUtils {
); );
} }
public static void assertAllTestsPresentByMetadata( public static void assertAllTestsPresentByMetadataWithExcluded(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@NotNull Pattern filenamePattern, @NotNull Pattern filenamePattern,
@Nullable Pattern excludedPattern,
@NotNull TargetBackend targetBackend, @NotNull TargetBackend targetBackend,
boolean recursive, boolean recursive,
@NotNull String... excludeDirs @NotNull String... excludeDirs
@@ -891,17 +903,31 @@ public class KotlinTestUtils {
if (files != null) { if (files != null) {
for (File file : files) { for (File file : files) {
if (file.isDirectory()) { 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); assertTestClassPresentByMetadata(testCaseClass, file);
} }
} }
else if (filenamePattern.matcher(file.getName()).matches() && isCompatibleTarget(targetBackend, file)) { else {
assertFilePathPresent(file, rootFile, filePaths); 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( public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@@ -910,18 +936,38 @@ public class KotlinTestUtils {
assertAllTestsPresentInSingleGeneratedClass(testCaseClass, testDataDir, filenamePattern, TargetBackend.ANY); 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( public static void assertAllTestsPresentInSingleGeneratedClass(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull File testDataDir, @NotNull File testDataDir,
@NotNull Pattern filenamePattern, @NotNull Pattern filenamePattern,
@NotNull TargetBackend targetBackend @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)); File rootFile = new File(getTestsRoot(testCaseClass));
Set<String> filePaths = collectPathsMetadata(testCaseClass); Set<String> filePaths = collectPathsMetadata(testCaseClass);
FileUtil.processFilesRecursively(testDataDir, file -> { 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); assertFilePathPresent(file, rootFile, filePaths);
} }
@@ -960,17 +1006,18 @@ public class KotlinTestUtils {
return filePaths; 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(); File[] files = dir.listFiles();
assert files != null; assert files != null;
for (File file : files) { for (File file : files) {
if (file.isDirectory()) { if (file.isDirectory()) {
if (containsTestData(file, filenamePattern)) { if (containsTestData(file, filenamePattern, excludedPattern)) {
return true; return true;
} }
} }
else { 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; return true;
} }
} }
@@ -29,6 +29,8 @@ public class SimpleTestClassModel extends TestClassModel {
@NotNull @NotNull
private final Pattern filenamePattern; private final Pattern filenamePattern;
@Nullable @Nullable
private final Pattern excludePattern;
@Nullable
private final Boolean checkFilenameStartsLowerCase; private final Boolean checkFilenameStartsLowerCase;
@NotNull @NotNull
private final String doTestMethodName; private final String doTestMethodName;
@@ -56,6 +58,7 @@ public class SimpleTestClassModel extends TestClassModel {
boolean recursive, boolean recursive,
boolean excludeParentDirs, boolean excludeParentDirs,
@NotNull Pattern filenamePattern, @NotNull Pattern filenamePattern,
@Nullable Pattern excludedPattern,
@Nullable Boolean checkFilenameStartsLowerCase, @Nullable Boolean checkFilenameStartsLowerCase,
@NotNull String doTestMethodName, @NotNull String doTestMethodName,
@NotNull String testClassName, @NotNull String testClassName,
@@ -71,6 +74,7 @@ public class SimpleTestClassModel extends TestClassModel {
this.recursive = recursive; this.recursive = recursive;
this.excludeParentDirs = excludeParentDirs; this.excludeParentDirs = excludeParentDirs;
this.filenamePattern = filenamePattern; this.filenamePattern = filenamePattern;
this.excludePattern = excludedPattern;
this.doTestMethodName = doTestMethodName; this.doTestMethodName = doTestMethodName;
this.testClassName = testClassName; this.testClassName = testClassName;
this.targetBackend = targetBackend; this.targetBackend = targetBackend;
@@ -98,7 +102,7 @@ public class SimpleTestClassModel extends TestClassModel {
if (file.isDirectory() && dirHasFilesInside(file) && !excludeDirs.contains(file.getName())) { if (file.isDirectory() && dirHasFilesInside(file) && !excludeDirs.contains(file.getName())) {
String innerTestClassName = TestGeneratorUtil.fileNameToJavaIdentifier(file); String innerTestClassName = TestGeneratorUtil.fileNameToJavaIdentifier(file);
children.add(new SimpleTestClassModel( children.add(new SimpleTestClassModel(
file, true, excludeParentDirs, filenamePattern, checkFilenameStartsLowerCase, file, true, excludeParentDirs, filenamePattern, excludePattern, checkFilenameStartsLowerCase,
doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()), doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()),
skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep != null ? deep - 1 : null, annotations) 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)) { if (listFiles != null && (deep == null || deep == 0)) {
for (File file : listFiles) { 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)) { if (file.isDirectory() && excludeParentDirs && dirHasSubDirs(file)) {
continue; continue;
@@ -252,19 +257,25 @@ public class SimpleTestClassModel extends TestClassModel {
exclude.append("\""); exclude.append("\"");
} }
String excludedArgument;
if (excludePattern != null) {
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
} else {
excludedArgument = null;
}
String assertTestsPresentStr; String assertTestsPresentStr;
if (targetBackend == TargetBackend.ANY) { if (targetBackend == TargetBackend.ANY) {
assertTestsPresentStr = String.format( assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s%s);", "KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s%s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument, recursive, exclude
recursive, exclude
); );
} else { } else {
assertTestsPresentStr = String.format( 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()), 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 @NotNull
private final Pattern filenamePattern; private final Pattern filenamePattern;
@Nullable @Nullable
private final Pattern excludePattern;
@Nullable
private final Boolean checkFilenameStartsLowerCase; private final Boolean checkFilenameStartsLowerCase;
@NotNull @NotNull
private final String doTestMethodName; private final String doTestMethodName;
@@ -59,6 +61,7 @@ public class SingleClassTestModel extends TestClassModel {
public SingleClassTestModel( public SingleClassTestModel(
@NotNull File rootFile, @NotNull File rootFile,
@NotNull Pattern filenamePattern, @NotNull Pattern filenamePattern,
@Nullable Pattern excludePattern,
@Nullable Boolean checkFilenameStartsLowerCase, @Nullable Boolean checkFilenameStartsLowerCase,
@NotNull String doTestMethodName, @NotNull String doTestMethodName,
@NotNull String testClassName, @NotNull String testClassName,
@@ -70,6 +73,7 @@ public class SingleClassTestModel extends TestClassModel {
) { ) {
this.rootFile = rootFile; this.rootFile = rootFile;
this.filenamePattern = filenamePattern; this.filenamePattern = filenamePattern;
this.excludePattern = excludePattern;
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase; this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
this.doTestMethodName = doTestMethodName; this.doTestMethodName = doTestMethodName;
this.testClassName = testClassName; this.testClassName = testClassName;
@@ -157,16 +161,23 @@ public class SingleClassTestModel extends TestClassModel {
public void generateBody(@NotNull Printer p) { public void generateBody(@NotNull Printer p) {
String assertTestsPresentStr; String assertTestsPresentStr;
String excludedArgument;
if (excludePattern != null) {
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
} else {
excludedArgument = null;
}
if (targetBackend != TargetBackend.ANY) { if (targetBackend != TargetBackend.ANY) {
assertTestsPresentStr = String.format( 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()), KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
TargetBackend.class.getSimpleName(), targetBackend.toString() excludedArgument, TargetBackend.class.getSimpleName(), targetBackend.toString()
); );
} else { } else {
assertTestsPresentStr = String.format( assertTestsPresentStr = String.format(
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"));", "KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()) KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument
); );
} }
p.println(assertTestsPresentStr); p.println(assertTestsPresentStr);
@@ -52,6 +52,7 @@ class TestGroup(
excludeParentDirs: Boolean = false, excludeParentDirs: Boolean = false,
extension: String? = "kt", // null string means dir (name without dot) extension: String? = "kt", // null string means dir (name without dot)
pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$", pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$",
excludedPattern: String? = null,
testMethod: String = "doTest", testMethod: String = "doTest",
singleClass: Boolean = false, singleClass: Boolean = false,
testClassName: String? = null, testClassName: String? = null,
@@ -63,18 +64,19 @@ class TestGroup(
) { ) {
val rootFile = File("$testDataRoot/$relativeRootPath") val rootFile = File("$testDataRoot/$relativeRootPath")
val compiledPattern = Pattern.compile(pattern) val compiledPattern = Pattern.compile(pattern)
val compiledExcludedPattern = excludedPattern?.let { Pattern.compile(it) }
val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile) val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile)
testModels.add( testModels.add(
if (singleClass) { if (singleClass) {
if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet") if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet")
SingleClassTestModel( SingleClassTestModel(
rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend, rootFile, compiledPattern, compiledExcludedPattern, filenameStartsLowerCase, testMethod, className,
skipIgnored, testRunnerMethodName, additionalRunnerArguments, annotations targetBackend, skipIgnored, testRunnerMethodName, additionalRunnerArguments, annotations
) )
} else { } else {
SimpleTestClassModel( SimpleTestClassModel(
rootFile, recursive, excludeParentDirs, rootFile, recursive, excludeParentDirs,
compiledPattern, filenameStartsLowerCase, testMethod, className, compiledPattern, compiledExcludedPattern, filenameStartsLowerCase, testMethod, className,
targetBackend, excludeDirs, skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep, annotations targetBackend, excludeDirs, skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep, annotations
) )
} }