Skip to content

Commit

Permalink
Move validateAndSetDefaults to Plugin (#240)
Browse files Browse the repository at this point in the history
### What's done:
* Changed logic
  • Loading branch information
petertrr authored Sep 2, 2021
1 parent c1237b2 commit 060d173
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import okio.Path
* @property redirectTo a file where process output and errors should be redirected. If null, output will be returned as [ExecutionResult.stdout] and [ExecutionResult.stderr].
*/
abstract class Plugin(
open val testConfig: TestConfig,
val testConfig: TestConfig,
protected val testFiles: List<String>,
protected val fs: FileSystem,
private val useInternalRedirections: Boolean,
Expand All @@ -29,6 +29,9 @@ abstract class Plugin(
* Instance that is capable of executing processes
*/
val pb = ProcessBuilder(useInternalRedirections, fs)
init {
testConfig.validateAndSetDefaults()
}

/**
* Perform plugin's work.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class FixAndWarnPlugin(
)

override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
testConfig.validateAndSetDefaults()
// Need to update private fields after validation
initOrUpdateConfigs()
val testFilePattern = warnPluginConfig.resourceNamePattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ data class FixAndWarnPluginConfig(
}

override fun validateAndSetDefaults(): PluginConfig {
require(fix.resourceNameTest == warn.testName && fix.batchSize == warn.batchSize) {
val fixPluginConfig = fix.validateAndSetDefaults()
val warnPluginConfig = warn.validateAndSetDefaults()
require(fixPluginConfig.resourceNameTest == warnPluginConfig.testNameSuffix &&
fixPluginConfig.batchSize == warnPluginConfig.batchSize
) {
"""
Test files suffix names and batch sizes should be identical for [fix] and [warn] plugins.
But found [fix]: {${fix.resourceNameTest}, ${fix.batchSize}},
[warn]: {${warn.testName}, ${warn.batchSize}}
But found [fix]: {${fixPluginConfig.resourceNameTest}, ${fixPluginConfig.batchSize}},
[warn]: {${warnPluginConfig.testNameSuffix}, ${warnPluginConfig.batchSize}}
"""
}
return FixAndWarnPluginConfig(
fix.validateAndSetDefaults(),
warn.validateAndSetDefaults()
fixPluginConfig, warnPluginConfig
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class FixPlugin(

@Suppress("TOO_LONG_FUNCTION")
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
testConfig.validateAndSetDefaults()

val fixPluginConfig = testConfig.pluginConfigs.filterIsInstance<FixPluginConfig>().single()
val generalConfig = testConfig.pluginConfigs.filterIsInstance<GeneralConfig>().single()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class WarnPlugin(
private val unexpected = "Some warnings were unexpected"

override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> {
testConfig.validateAndSetDefaults()

val warnPluginConfig = testConfig.pluginConfigs.filterIsInstance<WarnPluginConfig>().single()
val generalConfig = testConfig.pluginConfigs.filterIsInstance<GeneralConfig>().single()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import kotlinx.serialization.UseSerializers
* @property batchSize it controls how many files execCmd will process at a time.
* @property batchSeparator separator for batch mode
* @property linePlaceholder placeholder for line number, which resolved as current line and support addition and subtraction
* @property wildCardInDirectoryMode mode that controls that we are targetting our tested tools on directories (not on files).
* @property wildCardInDirectoryMode mode that controls that we are targeting our tested tools on directories (not on files).
* This prefix will be added to the name of the directory, if you would like to use directory mode without any prefix simply use ""
*/
@Serializable
Expand Down Expand Up @@ -71,17 +71,12 @@ data class WarnPluginConfig(
override var configLocation: Path = "undefined_toml_location".toPath()

/**
* @property suffix name of the test file.
* regex for the name of the test files.
*/
val testName: String = testNameSuffix ?: "Test"

/**
* @property resourceNamePattern regex for the name of the test files.
*/
val resourceNamePattern: Regex = if (testName == "Test") {
Regex("""(.+)${(testName)}\.[\w\d]+""")
val resourceNamePattern: Regex = if (testNameSuffix == "Test") {
Regex("""(.+)${(testNameSuffix)}\.[\w\d]+""")
} else {
Regex("""(.+)${(testName)}""")
Regex("""(.+)${(testNameSuffix)}""")
}

@Suppress("ComplexMethod")
Expand Down Expand Up @@ -147,7 +142,7 @@ data class WarnPluginConfig(
newColumnCaptureGroupOut,
newMessageCaptureGroupOut,
exactWarningsMatch ?: true,
testName,
testNameSuffix ?: "Test",
linePlaceholder ?: "\$line",
wildCardInDirectoryMode,
)
Expand Down

0 comments on commit 060d173

Please sign in to comment.