Skip to content

Commit

Permalink
Change test storage from List to class in Fix plugin
Browse files Browse the repository at this point in the history
What's done:
* Change test storage from List to class in plugin
Closes #221
  • Loading branch information
Cheshiriks committed Sep 3, 2021
1 parent 24279e9 commit 755c2bf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class MockPlugin(baseDir: Path, testFiles: List<String> = emptyList()) : Plugin(
useInternalRedirections = true,
redirectTo = null
) {
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> = emptySequence()
override fun handleFiles(files: Sequence<TestFiles>): Sequence<TestResult> = emptySequence()

override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<List<Path>> = emptySequence()
override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<TestFiles> = emptySequence()

override fun cleanupTempDir() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Path>>): Sequence<TestResult>
abstract fun handleFiles(files: Sequence<TestFiles>): Sequence<TestResult>

/**
* Discover groups of resource files which will be used to run tests, applying additional filtering
Expand All @@ -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<List<Path>> {
fun discoverTestFiles(root: Path): Sequence<TestFiles> {
val excludedTests =
testConfig
.pluginConfigs
Expand All @@ -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<Path>, excludedTests: List<String>?): Boolean {
private fun isExcludedTest(testFiles: TestFiles, excludedTests: List<String>?): 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('\\', '/')

Expand All @@ -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<Path>): Sequence<List<Path>>
abstract fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<TestFiles>

/**
* Method for cleaning up a directory.
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class FixAndWarnPlugin(
fs,
)

override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
override fun handleFiles(files: Sequence<TestFiles>): Sequence<TestResult> {
// 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)
Expand Down Expand Up @@ -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<Path>): Sequence<List<Path>> {
override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<TestFiles> {
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<List<Path>>.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 <warn> plugin and save data about warnings, which were deleted
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class FixPlugin(
.build()

@Suppress("TOO_LONG_FUNCTION")
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
override fun handleFiles(files: Sequence<TestFiles>): Sequence<TestResult> {
val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance<FixPluginConfig>().single()
val generalConfig = testConfig.pluginConfigs.filterIsInstance<GeneralConfig>().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() }
Expand Down Expand Up @@ -88,7 +88,8 @@ class FixPlugin(
)
)
}
}.flatten()
}
.flatten()
}

private fun checkStatus(expectedLines: List<String>, fixedLines: List<String>) =
Expand Down Expand Up @@ -118,7 +119,7 @@ class FixPlugin(
return pathCopy
}

override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<List<Path>> {
override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<TestFiles> {
val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance<FixPluginConfig>().single()
val regex = fixPluginConfig.resourceNamePattern
val resourceNameTest = fixPluginConfig.resourceNameTest
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Path>>): Sequence<TestResult> {
override fun handleFiles(files: Sequence<TestFiles>): Sequence<TestResult> {
val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance<WarnPluginConfig>().single()
val generalConfig = testConfig.pluginConfigs.filterIsInstance<GeneralConfig>().single()

Expand All @@ -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<Path>): Sequence<List<Path>> {
override fun rawDiscoverTestFiles(resourceDirectories: Sequence<Path>): Sequence<TestFiles> {
val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance<WarnPluginConfig>().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) }
}
}

Expand Down

0 comments on commit 755c2bf

Please sign in to comment.