Skip to content

Commit

Permalink
Removing includedTests option and adding functionality for excludedTe…
Browse files Browse the repository at this point in the history
…sts (#238)

Removing includedTests option and adding functionality for excludedTests

### What's done:
- added logic for excludedTests
- updating test resources
- removing includedTests option
  • Loading branch information
orchestr7 authored Sep 3, 2021
1 parent 060d173 commit 24279e9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 22 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,8 @@ description = "My suite description"
suiteName = DocsCheck, CaseCheck, NpeTests, e.t.c
# excluded tests in the suite (optional). Here you can provide names of excluded tests, separated by comma. By the default no tests are excluded.
# names can have 'Test' postifix or can be without a postfix - this does not matter. Save will detect tests for both cases
excludedTests = NameWithTestPostfix, NameWithoutPostfix, e.t.c
# tests included in the suite (optional). Here you can provide names of tests, that are included in this suite.
By the default all tests in the same directory with 'save.toml' file are included in the suite.
includedTests = NameWithTestPostfix, NameWithoutPostfix, e.t.c
# to exclude tests use relative path to the root of test project (to the root directory of `save.toml`)
excludedTests = ["warn/chapter1/GarbageTest.kt", "warn/otherDir/NewTest.kt"], e.t.c
```

## <a name="plugins"></a> Using plugins for specific test-scenarios
Expand Down
Empty file.
1 change: 1 addition & 0 deletions examples/kotlin-diktat/warn/chapter1/save.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
tags = ["tag1"]
description = "My suite description"
suiteName = "DocsCheck"
excludedTests = ["warn/chapter1/GarbageTest.kt"]

[warn]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.cqfn.save.core.utils.ProcessBuilder

import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath

/**
* Plugin that can be injected into SAVE during execution. Plugins accept contents of configuration file and then perform some work.
Expand Down Expand Up @@ -40,8 +41,8 @@ abstract class Plugin(
*/
fun execute(): Sequence<TestResult> {
clean()
// todo: pass individual groups of files to handleFiles? Or it will play bad with batch mode?
val testFilesSequence = discoverTestFiles(testConfig.directory)

return if (testFilesSequence.any()) {
logDebug("Discovered the following test resources: ${testFilesSequence.toList()}")
handleFiles(testFilesSequence)
Expand All @@ -66,7 +67,23 @@ abstract class Plugin(
* @return a sequence of files, grouped by test
*/
fun discoverTestFiles(root: Path): Sequence<List<Path>> {
val excludedTests =
testConfig
.pluginConfigs
.filterIsInstance<GeneralConfig>()
.singleOrNull()
?.excludedTests

if (!excludedTests.isNullOrEmpty()) {
logDebug("Excluded tests for [${testConfig.location}] : $excludedTests")
}

val rawTestFiles = rawDiscoverTestFiles(root.resourceDirectories())
// removing excluded test resources
.filterNot {
isExcludedTest(it, excludedTests)
}

return if (testFiles.isNotEmpty()) {
rawTestFiles.filter { resourcesGroup ->
// test can be specified by the name of one of it's files
Expand All @@ -79,6 +96,24 @@ abstract class Plugin(
}
}

private fun isExcludedTest(testFiles: List<Path>, 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)
.toString()
.replace('\\', '/')

// excluding tests that are included in the excluded list
return excludedTests
?.map { it.replace('\\', '/') }
?.contains(testFileRelative)
?: false
}

/**
* Discover groups of resource files which will be used to run tests.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ interface PluginConfig {
* @property description free text with a description
* @property suiteName name of test suite that can be visible from save-cloud
* @property excludedTests excluded tests from the run
* @property includedTests if specified - only these tests will be run
* @property expectedWarningsPattern - pattern with warnings that are expected from the test file
*/
@Serializable
Expand All @@ -65,7 +64,6 @@ data class GeneralConfig(
val description: String? = null,
val suiteName: String? = null,
val excludedTests: List<String>? = null,
val includedTests: List<String>? = null,
val expectedWarningsPattern: Regex? = null,
) : PluginConfig {
override val type = TestConfigSections.GENERAL
Expand All @@ -87,7 +85,6 @@ data class GeneralConfig(
this.description ?: other.description,
this.suiteName ?: other.suiteName,
this.excludedTests ?: other.excludedTests,
this.includedTests ?: other.includedTests,
this.expectedWarningsPattern ?: other.expectedWarningsPattern,
)
}
Expand All @@ -111,7 +108,6 @@ data class GeneralConfig(
description,
suiteName,
excludedTests ?: emptyList(),
includedTests ?: emptyList(),
expectedWarningsPattern ?: defaultInputPattern,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ class Save(
logDebug("=> Executing plugin: ${plugin::class.simpleName} for [${plugin.testConfig.location}]")
reporter.onPluginExecutionStart(plugin)
try {
val rootDir = plugin.testConfig.getRootConfig().location
val testRepositoryRootPath = plugin.testConfig.getRootConfig().location

plugin.execute()
.onEach { event ->
// calculate relative paths, because reporters don't need paths higher than root dir
val resourcesRelative =
event.resources.map { it.createRelativePathToTheRoot(rootDir).toPath() / it.name }
event.resources.map { it.createRelativePathToTheRoot(testRepositoryRootPath).toPath() / it.name }
reporter.onEvent(event.copy(resources = resourcesRelative))
}
.forEach(this::handleResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ internal val toml4 = nestedDir2 / "nestedDir3" / "nestedDir4" / "save.toml"

@Suppress("TOO_LONG_FUNCTION", "LOCAL_VARIABLE_EARLY_DECLARATION")
class MergeConfigsTest {
private val generalConfig1 = GeneralConfig("", listOf("Tag11", "Tag12"), "Description1", "suiteName1", listOf("excludedTests: test1"), listOf("includedTests: test2"))
private val generalConfig2 = GeneralConfig("", listOf("Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"), listOf("includedTests: test4"))
private val generalConfig1 = GeneralConfig("", listOf("Tag11", "Tag12"), "Description1", "suiteName1", listOf("excludedTests: test1"))
private val generalConfig2 = GeneralConfig("", listOf("Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"))
private val generalConfig3 = GeneralConfig("", listOf("Tag21", "Tag31", "Tag32"), "Description2", "suiteName3", listOf("excludedTests: test5", "includedTests: test6"))
private val generalConfig4 = GeneralConfig("", listOf("Tag11", "Tag21"), "Description2", "suiteName4", listOf("excludedTests: test7"), listOf("includedTests: test8"))
private val generalConfig4 = GeneralConfig("", listOf("Tag11", "Tag21"), "Description2", "suiteName4", listOf("excludedTests: test7"))
private val warningsOutputPattern1 = Regex(".*")
private val warningsOutputPattern2 = Regex("\\w+ - (\\d+)/(\\d+) - (.*)$")
private val warnConfig1 = WarnPluginConfig("execCmd1", warningsOutputPattern2,
Expand Down Expand Up @@ -58,7 +58,7 @@ class MergeConfigsTest {
assertEquals(1, config2.pluginConfigs.size)

val expectedGeneralConfig =
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"), listOf("includedTests: test4"))
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"))

val actualGeneralConfig = config2.pluginConfigs.filterIsInstance<GeneralConfig>().first()

Expand All @@ -76,7 +76,7 @@ class MergeConfigsTest {
assertEquals(2, config2.pluginConfigs.size)

val expectedGeneralConfig =
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"), listOf("includedTests: test4"))
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"))

val actualGeneralConfig = config2.pluginConfigs.filterIsInstance<GeneralConfig>().first()
val actualWarnConfig = config2.pluginConfigs.filterIsInstance<WarnPluginConfig>().first()
Expand Down Expand Up @@ -113,7 +113,7 @@ class MergeConfigsTest {
assertEquals(3, config2.pluginConfigs.size)

val expectedGeneralConfig =
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"), listOf("includedTests: test4"))
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21"), "Description2", "suiteName2", listOf("excludedTests: test3"))
val expectedWarnConfig = WarnPluginConfig("execCmd3", warningsOutputPattern2,
true, false, 1, ", ", 3, 3, 3, 3, 3, 3, 3, true)
val expectedFixConfig = FixPluginConfig("fixCmd2", 1, "Suffix")
Expand All @@ -139,7 +139,7 @@ class MergeConfigsTest {

assertEquals(3, config4.pluginConfigs.size)
val expectedGeneralConfig =
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21", "Tag31", "Tag32"), "Description2", "suiteName4", listOf("excludedTests: test7"), listOf("includedTests: test8"))
GeneralConfig("", listOf("Tag11", "Tag12", "Tag21", "Tag31", "Tag32"), "Description2", "suiteName4", listOf("excludedTests: test7"))
val expectedWarnConfig = WarnPluginConfig("execCmd4", warningsOutputPattern2,
true, false, 1, ", ", 4, 4, 4, 4, 4, 4, 4, true)
val expectedFixConfig = FixPluginConfig("fixCmd4", 1, "Suffix")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class ValidationTest {

val actualGeneralConfig1 = config.pluginConfigs.filterIsInstance<GeneralConfig>().first()
assertEquals(emptyList(), actualGeneralConfig1.excludedTests)
assertEquals(emptyList(), actualGeneralConfig1.includedTests)
}

@Test
Expand All @@ -41,7 +40,7 @@ class ValidationTest {
assertEquals(
"""
Error: Couldn't find `execCmd` in [general] section of `${generalConfig.configLocation}` config.
Current configuration: execCmd=null, tags=null, description=null, suiteName=null, excludedTests=null, includedTests=null, expectedWarningsPattern=null
Current configuration: execCmd=null, tags=null, description=null, suiteName=null, excludedTests=null, expectedWarningsPattern=null
Please provide it in this, or at least in one of the parent configs.
""".trimIndent(),
ex.message
Expand Down

0 comments on commit 24279e9

Please sign in to comment.