Refactor and optimize JetCodeConformanceTest

Combine all tests that iterate over all files in the project. This should speed
up the test substantially, especially on slow file systems
This commit is contained in:
Alexander Udalov
2015-05-28 23:01:23 +03:00
parent 41d2e378f8
commit 9cd835a5f8
@@ -18,8 +18,8 @@ package org.jetbrains.kotlin.code
import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtil
import junit.framework.TestCase import junit.framework.TestCase
import org.junit.Assert.assertTrue
import java.io.File import java.io.File
import java.util.ArrayList
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.test.fail import kotlin.test.fail
@@ -55,43 +55,60 @@ public class JetCodeConformanceTest : TestCase() {
} }
} }
public fun testForAuthorJavadoc() { public fun testNoBadSubstringsInProjectCode() {
val pattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL) class TestData(val message: String, val filter: (String) -> Boolean) {
val result: MutableList<File> = ArrayList()
val found = filterSourceFiles { source ->
// substring check is an optimization
"@author" in source && pattern.matcher(source).find()
} }
assertTrue("%d source files contain @author javadoc tag. " + val atAuthorPattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL)
"Please remove them or exclude in this test:\n%s".format(found.size(), found.joinToString("\n")), found.isEmpty())
}
public fun testNoJCommanderInternalImports() { val tests = listOf(
val found = filterSourceFiles { source -> TestData(
"com.beust.jcommander.internal" in source "%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s",
{ source ->
// substring check is an optimization
"@author" in source && atAuthorPattern.matcher(source).find()
}
),
TestData(
"%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, " +
"because there's only an optional dependency on testng.jar.\n" +
"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",
{ source ->
"com.beust.jcommander.internal" in source
}
),
TestData(
"%d source files contain references to package org.jetbrains.jet.\n" +
"Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " +
"Please consider changing the package in these files:\n%s",
{ source ->
"org.jetbrains.jet" in source
}
)
)
for (sourceFile in FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File("."))) {
if (EXCLUDED_FILES_AND_DIRS.any { FileUtil.isAncestor(it, sourceFile, false) }) continue
val source = sourceFile.readText()
for (test in tests) {
if (test.filter(source)) test.result.add(sourceFile)
}
} }
assertTrue("It seems that you've used something from com.beust.jcommander.internal package. " + if (tests.flatMap { it.result }.isNotEmpty()) {
"This code won't work when there's no TestNG in the classpath of our IDEA plugin, " + fail(StringBuilder {
"because there's only an optional dependency on testng.jar.\n" + for (test in tests) {
"Most probably you meant to use Guava's Lists, Maps or Sets instead. " + if (test.result.isNotEmpty()) {
"Please change references in these files to com.google.common.collect: $found", found.isEmpty()) append(test.message.format(test.result.size(), test.result.joinToString("\n")))
} appendln()
appendln()
public fun testNoOrgJetbrainsJet() { }
val found = filterSourceFiles { source -> }
"org.jetbrains.jet" in source }.toString())
}
assertTrue("Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " +
"Please consider changing the package in these files: $found", found.isEmpty())
}
private fun filterSourceFiles(predicate: (String) -> Boolean): List<File> {
return FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File(".")).filter { sourceFile ->
EXCLUDED_FILES_AND_DIRS.none { FileUtil.isAncestor(it, sourceFile, false) } &&
predicate(sourceFile.readText())
} }
} }
} }