From 5b1f96ba1be81f1dd6f64389b11a5bcedcc97397 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 11 Dec 2019 15:32:58 +0300 Subject: [PATCH] [TEST] Add ability to exclude some testdata with pattern in test generator --- .../kotlin/test/KotlinTestUtils.java | 63 ++++++++++++++++--- .../tests/generator/SimpleTestClassModel.java | 25 +++++--- .../tests/generator/SingleClassTestModel.java | 19 ++++-- .../tests/generator/TestGenerationDSL.kt | 8 ++- 4 files changed, 93 insertions(+), 22 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java index 6d64d1c3fb1..b6a78789b9a 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -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 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; } } diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java index e65a65e827d..5d65888ff7e 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SimpleTestClassModel.java @@ -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 ); } diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java index 0cd91251b12..b39ec23f6ed 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/SingleClassTestModel.java @@ -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); diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt index bb7454a22c8..0b1cd34c429 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/tests/generator/TestGenerationDSL.kt @@ -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 ) }