Rewrite testThirdPartyCopyrights test

This commit is contained in:
Nikolay Krasko
2020-08-10 21:01:41 +03:00
parent bb0ea56d63
commit fad15b6627
@@ -16,7 +16,7 @@ import kotlin.collections.HashSet
class CodeConformanceTest : TestCase() { class CodeConformanceTest : TestCase() {
companion object { companion object {
private val JAVA_FILE_PATTERN = Pattern.compile(".+\\.java") private val JAVA_FILE_PATTERN = Pattern.compile(".+\\.java")
private val SOURCES_FILE_PATTERN = Pattern.compile("(.+\\.java|.+\\.kt|.+\\.js)") private val SOURCES_FILE_PATTERN = Pattern.compile(".+\\.(java|kt|js)")
private const val MAX_STEPS_COUNT = 100 private const val MAX_STEPS_COUNT = 100
private val nonSourcesMatcher = FileMatcher( private val nonSourcesMatcher = FileMatcher(
File("."), File("."),
@@ -61,7 +61,9 @@ class CodeConformanceTest : TestCase() {
) )
) )
private val COPYRIGHT_EXCLUDED_FILES_AND_DIRS = listOf( private val COPYRIGHT_EXCLUDED_FILES_AND_DIRS_MATCHER = FileMatcher(
File("."),
listOf(
"build", "build",
"buildSrc/prepare-deps/build", "buildSrc/prepare-deps/build",
"compiler/ir/serialization.js/build/fullRuntime", "compiler/ir/serialization.js/build/fullRuntime",
@@ -95,6 +97,7 @@ class CodeConformanceTest : TestCase() {
"libraries/tools/kotlin-test-nodejs-runner/node_modules", "libraries/tools/kotlin-test-nodejs-runner/node_modules",
"out" "out"
) )
)
} }
fun testParserCode() { fun testParserCode() {
@@ -223,22 +226,26 @@ class CodeConformanceTest : TestCase() {
fun testThirdPartyCopyrights() { fun testThirdPartyCopyrights() {
val filesWithUnlistedCopyrights = mutableListOf<String>() val filesWithUnlistedCopyrights = mutableListOf<String>()
val root = File(".").absoluteFile
val knownThirdPartyCode = loadKnownThirdPartyCodeList() val knownThirdPartyCode = loadKnownThirdPartyCodeList()
val copyrightRegex = Regex("""\bCopyright\b""") val copyrightRegex = Regex("""\bCopyright\b""")
for (sourceFile in FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, root)) { val root = COPYRIGHT_EXCLUDED_FILES_AND_DIRS_MATCHER.root
val relativePath = FileUtil.toSystemIndependentName(sourceFile.toRelativeString(root))
if (COPYRIGHT_EXCLUDED_FILES_AND_DIRS.any { relativePath.startsWith(it) } ||
knownThirdPartyCode.any { relativePath.startsWith(it) }) continue
COPYRIGHT_EXCLUDED_FILES_AND_DIRS_MATCHER.walkTopDown(SOURCES_FILE_PATTERN)
.filter { sourceFile ->
val relativePath = FileUtil.toSystemIndependentName(sourceFile.toRelativeString(root))
!knownThirdPartyCode.any { relativePath.startsWith(it) }
}
.forEach { sourceFile ->
sourceFile.useLines { lineSequence -> sourceFile.useLines { lineSequence ->
for (line in lineSequence) { for (line in lineSequence) {
if (copyrightRegex in line && "JetBrains" !in line) { if (copyrightRegex in line && "JetBrains" !in line) {
val relativePath = FileUtil.toSystemIndependentName(sourceFile.toRelativeString(root))
filesWithUnlistedCopyrights.add("$relativePath: $line") filesWithUnlistedCopyrights.add("$relativePath: $line")
} }
} }
} }
} }
if (filesWithUnlistedCopyrights.isNotEmpty()) { if (filesWithUnlistedCopyrights.isNotEmpty()) {
fail( fail(
"The following files contain third-party copyrights and no license information. " + "The following files contain third-party copyrights and no license information. " +
@@ -248,10 +255,10 @@ class CodeConformanceTest : TestCase() {
} }
private class FileMatcher(val root: File, paths: Collection<String>) { private class FileMatcher(val root: File, paths: Collection<String>) {
private val files = paths.filter { !it.startsWith("*/") }.map(::File) private val files = paths.map { File(it) }
private val names = files.mapTo(HashSet()) { it.name } private val names = files.mapTo(HashSet()) { it.name }
private val paths = files.mapTo(HashSet()) { it.systemIndependentPath } private val paths = files.mapTo(HashSet()) { it.systemIndependentPath }
private val relativePaths = files.filter { it.isDirectory }.mapTo(HashSet()) { it.systemIndependentPath + "/" } private val relativePaths = files.filterTo(ArrayList()) { it.isDirectory }.mapTo(HashSet()) { it.systemIndependentPath + "/" }
fun matchExact(file: File): Boolean { fun matchExact(file: File): Boolean {
return (file.name in names) && file.relativeTo(root).systemIndependentPath in paths return (file.name in names) && file.relativeTo(root).systemIndependentPath in paths