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:
@@ -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 ->
|
val atAuthorPattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL)
|
||||||
|
|
||||||
|
val tests = listOf(
|
||||||
|
TestData(
|
||||||
|
"%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s",
|
||||||
|
{ source ->
|
||||||
// substring check is an optimization
|
// substring check is an optimization
|
||||||
"@author" in source && pattern.matcher(source).find()
|
"@author" in source && atAuthorPattern.matcher(source).find()
|
||||||
}
|
}
|
||||||
|
),
|
||||||
assertTrue("%d source files contain @author javadoc tag. " +
|
TestData(
|
||||||
"Please remove them or exclude in this test:\n%s".format(found.size(), found.joinToString("\n")), found.isEmpty())
|
"%d source files use something from com.beust.jcommander.internal package.\n" +
|
||||||
}
|
|
||||||
|
|
||||||
public fun testNoJCommanderInternalImports() {
|
|
||||||
val found = filterSourceFiles { source ->
|
|
||||||
"com.beust.jcommander.internal" in source
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue("It seems that you've used something from com.beust.jcommander.internal package. " +
|
|
||||||
"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: $found", found.isEmpty())
|
"Please change references in these files to com.google.common.collect:\n%s",
|
||||||
|
{ source ->
|
||||||
|
"com.beust.jcommander.internal" in source
|
||||||
}
|
}
|
||||||
|
),
|
||||||
public fun testNoOrgJetbrainsJet() {
|
TestData(
|
||||||
val found = filterSourceFiles { source ->
|
"%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
|
"org.jetbrains.jet" in source
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
assertTrue("Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " +
|
for (sourceFile in FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File("."))) {
|
||||||
"Please consider changing the package in these files: $found", found.isEmpty())
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filterSourceFiles(predicate: (String) -> Boolean): List<File> {
|
if (tests.flatMap { it.result }.isNotEmpty()) {
|
||||||
return FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File(".")).filter { sourceFile ->
|
fail(StringBuilder {
|
||||||
EXCLUDED_FILES_AND_DIRS.none { FileUtil.isAncestor(it, sourceFile, false) } &&
|
for (test in tests) {
|
||||||
predicate(sourceFile.readText())
|
if (test.result.isNotEmpty()) {
|
||||||
|
append(test.message.format(test.result.size(), test.result.joinToString("\n")))
|
||||||
|
appendln()
|
||||||
|
appendln()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user