Optimize testNoBadSubstringsInProjectCode test

Don't visit ignored directories
This commit is contained in:
Nikolay Krasko
2020-08-10 14:29:08 +03:00
parent 2655d9dab3
commit 8db588c7f0
@@ -167,59 +167,62 @@ class CodeConformanceTest : TestCase() {
val tests = listOf( val tests = listOf(
TestData( TestData(
"%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s", "%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s"
{ source -> ) { source ->
// substring check is an optimization // substring check is an optimization
"@author" in source && atAuthorPattern.matcher(source).find() && "@author" in source && atAuthorPattern.matcher(source).find() &&
"ASM: a very small and fast Java bytecode manipulation framework" !in source && "ASM: a very small and fast Java bytecode manipulation framework" !in source &&
"package org.jetbrains.kotlin.tools.projectWizard.settings.version.maven" !in source "package org.jetbrains.kotlin.tools.projectWizard.settings.version.maven" !in source
} },
),
TestData( TestData(
"%d source files use something from com.beust.jcommander.internal package.\n" + "%d source files use something from com.beust.jcommander.internal package.\n" +
"This code won't work when there's no TestNG in the classpath of our IDEA plugin, " + "This code won't work when there's no TestNG in the classpath of our IDEA plugin, " +
"because there's only an optional dependency on testng.jar.\n" + "because there's only an optional dependency on testng.jar.\n" +
"Most probably you meant to use Guava's Lists, Maps or Sets instead. " + "Most probably you meant to use Guava's Lists, Maps or Sets instead. " +
"Please change references in these files to com.google.common.collect:\n%s", "Please change references in these files to com.google.common.collect:\n%s"
{ source -> ) { source ->
"com.beust.jcommander.internal" in source "com.beust.jcommander.internal" in source
} },
),
TestData( TestData(
"%d source files contain references to package org.jetbrains.jet.\n" + "%d source files contain references to package org.jetbrains.jet.\n" +
"Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " + "Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " +
"Please consider changing the package in these files:\n%s", "Please consider changing the package in these files:\n%s"
{ source -> ) { source ->
"org.jetbrains.jet" in source "org.jetbrains.jet" in source
} },
),
TestData( TestData(
"%d source files contain references to package kotlin.reflect.jvm.internal.impl.\n" + "%d source files contain references to package kotlin.reflect.jvm.internal.impl.\n" +
"This package contains internal reflection implementation and is a result of a " + "This package contains internal reflection implementation and is a result of a " +
"post-processing of kotlin-reflect.jar by jarjar.\n" + "post-processing of kotlin-reflect.jar by jarjar.\n" +
"Most probably you meant to use classes from org.jetbrains.kotlin.**.\n" + "Most probably you meant to use classes from org.jetbrains.kotlin.**.\n" +
"Please change references in these files or exclude them in this test:\n%s", "Please change references in these files or exclude them in this test:\n%s"
{ source -> ) { source ->
"kotlin.reflect.jvm.internal.impl" in source "kotlin.reflect.jvm.internal.impl" in source
} },
),
TestData( TestData(
"%d source files contain references to package org.objectweb.asm.\n" + "%d source files contain references to package org.objectweb.asm.\n" +
"Package org.jetbrains.org.objectweb.asm should be used instead to avoid troubles with different asm versions in classpath. " + "Package org.jetbrains.org.objectweb.asm should be used instead to avoid troubles with different asm versions in classpath. " +
"Please consider changing the package in these files:\n%s", "Please consider changing the package in these files:\n%s"
{ source -> ) { source ->
" org.objectweb.asm" in source " org.objectweb.asm" in source
}) }
) )
for (sourceFile in FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File("."))) { val root = File(".")
if (NON_SOURCE_EXCLUDED_FILES_AND_DIRS.any { FileUtil.isAncestor(it, sourceFile, false) }) continue val nonSourceMatcher = FileMatcher(root, NON_SOURCE_EXCLUDED_FILES_AND_DIRS)
root.walkTopDown()
val source = sourceFile.readText() .onEnter { dir ->
for (test in tests) { !nonSourceMatcher.matchExact(dir) // Don't enter to ignored dirs
if (test.filter(source)) test.result.add(sourceFile) }
.filter { file -> !nonSourceMatcher.matchExact(file) } // filter ignored files
.filter { file -> SOURCES_FILE_PATTERN.matcher(file.name).matches() }
.filter { file -> file.isFile }
.forEach { sourceFile ->
val source = sourceFile.readText()
for (test in tests) {
if (test.filter(source)) test.result.add(sourceFile)
}
} }
}
if (tests.flatMap { it.result }.isNotEmpty()) { if (tests.flatMap { it.result }.isNotEmpty()) {
fail(buildString { fail(buildString {