diff --git a/save-common-test/src/commonNonJsMain/kotlin/org/cqfn/save/plugin/MockPlugin.kt b/save-common-test/src/commonNonJsMain/kotlin/org/cqfn/save/plugin/MockPlugin.kt index 2ac46227a..617745755 100644 --- a/save-common-test/src/commonNonJsMain/kotlin/org/cqfn/save/plugin/MockPlugin.kt +++ b/save-common-test/src/commonNonJsMain/kotlin/org/cqfn/save/plugin/MockPlugin.kt @@ -20,9 +20,9 @@ class MockPlugin(baseDir: Path, testFiles: List = emptyList()) : Plugin( useInternalRedirections = true, redirectTo = null ) { - override fun handleFiles(files: Sequence>): Sequence = emptySequence() + override fun handleFiles(files: Sequence): Sequence = emptySequence() - override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence> = emptySequence() + override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence = emptySequence() override fun cleanupTempDir() = Unit } diff --git a/save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt b/save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt index 68e0054f5..950b4671b 100644 --- a/save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt +++ b/save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt @@ -57,7 +57,7 @@ abstract class Plugin( * @param files a sequence of file groups, corresponding to tests. * @return a sequence of [TestResult]s for each group of test resources */ - abstract fun handleFiles(files: Sequence>): Sequence + abstract fun handleFiles(files: Sequence): Sequence /** * Discover groups of resource files which will be used to run tests, applying additional filtering @@ -66,7 +66,7 @@ abstract class Plugin( * @param root root [Path], from where discovering should be started * @return a sequence of files, grouped by test */ - fun discoverTestFiles(root: Path): Sequence> { + fun discoverTestFiles(root: Path): Sequence { val excludedTests = testConfig .pluginConfigs @@ -85,25 +85,22 @@ abstract class Plugin( } return if (testFiles.isNotEmpty()) { - rawTestFiles.filter { resourcesGroup -> - // test can be specified by the name of one of it's files - resourcesGroup.any { path -> - testFiles.any { it in path.toString() } - } + rawTestFiles.filter { path -> + testFiles.any { it in path.test.toString() } } } else { rawTestFiles } } - private fun isExcludedTest(testFiles: List, excludedTests: List?): Boolean { + private fun isExcludedTest(testFiles: TestFiles, excludedTests: List?): Boolean { // common root of the test repository (not a location of a current test) val testRepositoryRoot = testConfig.getRootConfig().location // creating relative to root path from a test file // FixMe: https://github.com/cqfn/save/issues/241 here we are incorrectly using testFiles[0], as for example it is // "Expected" file for Fix plugin val testFileRelative = - (testFiles[0].createRelativePathToTheRoot(testRepositoryRoot).toPath() / testFiles[0].name) + (testFiles.test.createRelativePathToTheRoot(testRepositoryRoot).toPath() / testFiles.test.name) .toString() .replace('\\', '/') @@ -120,7 +117,7 @@ abstract class Plugin( * @param resourceDirectories a sequence of [Path]s, which contain this plugin's resources * @return a sequence of files, grouped by test */ - abstract fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence> + abstract fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence /** * Method for cleaning up a directory. @@ -188,4 +185,20 @@ abstract class Plugin( // this matches directories which contain their own SAVE config fs.metadata(file).isRegularFile || fs.list(file).none { it.isSaveTomlConfig() } } + + /** + * @property test test file + */ + @Suppress("USE_DATA_CLASS") + interface TestFiles { + /** + * path to test file + */ + val test: Path + } + + /** + * @property test test file + */ + data class Test(override val test: Path) : TestFiles } diff --git a/save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt b/save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt index 77a52f9e7..3be6d5c18 100644 --- a/save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt +++ b/save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt @@ -64,11 +64,10 @@ class FixAndWarnPlugin( fs, ) - override fun handleFiles(files: Sequence>): Sequence { + override fun handleFiles(files: Sequence): Sequence { // Need to update private fields after validation initOrUpdateConfigs() - val testFilePattern = warnPluginConfig.resourceNamePattern - val expectedFiles = files.filterTestResources(testFilePattern, match = false) + val expectedFiles = files.map { it as FixPlugin.FixTestFiles }.map { it.expected } // Remove (in place) warnings from test files before fix plugin execution val filesAndTheirWarningsMap = removeWarningsFromExpectedFiles(expectedFiles) @@ -99,33 +98,16 @@ class FixAndWarnPlugin( // TODO: then warn plugin should look at the fix plugin output for actual warnings, and not execute command one more time. // TODO: However it's required changes in warn plugin logic (it's should be able to compare expected and actual warnings from different places), // TODO: this probably could be obtained after https://github.com/cqfn/save/issues/164, - val warnTestResults = warnPlugin.handleFiles(expectedFilesWithPass.map { listOf(it) }) + val warnTestResults = warnPlugin.handleFiles(expectedFilesWithPass.map { Test(it) }) return fixTestResultsFailed.asSequence() + warnTestResults } - override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence> { + override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence { initOrUpdateConfigs() // Test files for fix and warn plugin should be the same, so this will be enough return fixPlugin.rawDiscoverTestFiles(resourceDirectories) } - /** - * Filter test resources - * - * @param suffix regex pattern of test resource - * @param match whether to keep elements which matches current pattern or to keep elements, which is not - * @return filtered list of files - */ - private fun Sequence>.filterTestResources(suffix: Regex, match: Boolean) = map { resources -> - resources.single { path -> - if (match) { - suffix.matchEntire(path.toString()) != null - } else { - suffix.matchEntire(path.toString()) == null - } - } - } - /** * Remove warnings from the given files, which satisfy pattern from plugin and save data about warnings, which were deleted * diff --git a/save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt b/save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt index 079913509..4c3edd68f 100644 --- a/save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt +++ b/save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt @@ -46,12 +46,12 @@ class FixPlugin( .build() @Suppress("TOO_LONG_FUNCTION") - override fun handleFiles(files: Sequence>): Sequence { + override fun handleFiles(files: Sequence): Sequence { val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance().single() val generalConfig = testConfig.pluginConfigs.filterIsInstance().single() - return files.chunked(fixPluginConfig.batchSize!!.toInt()).map { chunk -> - val pathMap = chunk.map { it.first() to it.last() } + return files.map { it as FixTestFiles }.chunked(fixPluginConfig.batchSize!!.toInt()).map { chunk -> + val pathMap = chunk.map { it.expected to it.test } val pathCopyMap = pathMap.map { (expected, test) -> expected to createTestFile(test, generalConfig) } val testCopyNames = pathCopyMap.joinToString(separator = fixPluginConfig.batchSeparator!!) { (_, testCopy) -> testCopy.toString() } @@ -88,7 +88,8 @@ class FixPlugin( ) ) } - }.flatten() + } + .flatten() } private fun checkStatus(expectedLines: List, fixedLines: List) = @@ -118,7 +119,7 @@ class FixPlugin( return pathCopy } - override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence> { + override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence { val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance().single() val regex = fixPluginConfig.resourceNamePattern val resourceNameTest = fixPluginConfig.resourceNameTest @@ -133,14 +134,13 @@ class FixPlugin( .filter { it.value.size > 1 && it.key != null } .mapValues { (name, group) -> require(group.size == 2) { "Files should be grouped in pairs, but for name $name these files have been discovered: $group" } - listOf( + Test( + group.first { it.name.contains("$resourceNameTest.") }, group.first { it.name.contains("$resourceNameExpected.") }, - group.first { it.name.contains("$resourceNameTest.") } ) } .values } - .filter { it.isNotEmpty() } } override fun cleanupTempDir() { @@ -171,4 +171,20 @@ class FixPlugin( } .toList() .joinToString { (type, lines) -> "$type: $lines lines" } + + /** + * @property expected expected file + */ + interface FixTestFiles : TestFiles { + /** + * path to expected file + */ + val expected: Path + } + + /** + * @property test test file + * @property expected expected file + */ + class Test(override val test: Path, override val expected: Path) : FixTestFiles } diff --git a/save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt b/save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt index b92f960f1..5ffe10c8a 100644 --- a/save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt +++ b/save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt @@ -40,7 +40,7 @@ class WarnPlugin( private val expectedAndNotReceived = "Some warnings were expected but not received" private val unexpected = "Some warnings were unexpected" - override fun handleFiles(files: Sequence>): Sequence { + override fun handleFiles(files: Sequence): Sequence { val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance().single() val generalConfig = testConfig.pluginConfigs.filterIsInstance().single() @@ -49,22 +49,22 @@ class WarnPlugin( // // In case, when user doesn't want to use directory mode, he needs simply not to pass [wildCardInDirectoryMode] and it will be null return warnPluginConfig.wildCardInDirectoryMode?.let { - handleTestFile(files.map { it.single() }.toList(), warnPluginConfig, generalConfig).asSequence() + handleTestFile(files.map { it.test }.toList(), warnPluginConfig, generalConfig).asSequence() } ?: run { files.chunked(warnPluginConfig.batchSize!!.toInt()).flatMap { chunk -> - handleTestFile(chunk.map { it.single() }, warnPluginConfig, generalConfig) + handleTestFile(chunk.map { it.test }, warnPluginConfig, generalConfig) } } } - override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence> { + override fun rawDiscoverTestFiles(resourceDirectories: Sequence): Sequence { val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance().single() val regex = warnPluginConfig.resourceNamePattern // returned sequence is a sequence of groups of size 1 return resourceDirectories.flatMap { directory -> fs.list(directory) .filter { regex.matches(it.name) } - .map { listOf(it) } + .map { Test(it) } } }