From c22931fa8defe1cefb9cd1edc023dfb560a443f7 Mon Sep 17 00:00:00 2001 From: Vladislav Frolov <50615459+Cheshiriks@users.noreply.github.com> Date: Tue, 31 Aug 2021 11:46:17 +0300 Subject: [PATCH] Create the logic in Plugins, which will manage redirectTo for ProcessBuilder (#220) * Create the logic in Plugins, which will manage redirectTo for ProcessBuilder What's done: * Added logic for redirectTo Closes #122 --- .../kotlin/org/cqfn/save/plugin/MockPlugin.kt | 3 ++- .../commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt | 4 +++- .../kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt | 3 ++- .../org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt | 7 +++++-- .../kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt | 8 +++++--- .../kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt | 8 +++++--- 6 files changed, 22 insertions(+), 11 deletions(-) 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 0a0dbd93c..2ac46227a 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 @@ -17,7 +17,8 @@ class MockPlugin(baseDir: Path, testFiles: List = emptyList()) : Plugin( TestConfig((baseDir / "save.toml").also { fs.createFile(it) }, null, fs = fs), testFiles, fs, - useInternalRedirections = true + useInternalRedirections = true, + redirectTo = null ) { override fun handleFiles(files: Sequence>): Sequence = emptySequence() 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 1458d6f39..3e202b6a9 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 @@ -17,12 +17,14 @@ import okio.Path * @property testFiles a list of files (test resources or save.toml configs) * @property fs describes the current file system * @property useInternalRedirections whether to redirect stdout/stderr for internal purposes + * @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, protected val testFiles: List, protected val fs: FileSystem, - private val useInternalRedirections: Boolean) { + private val useInternalRedirections: Boolean, + protected val redirectTo: Path?) { /** * Instance that is capable of executing processes */ diff --git a/save-common/src/commonMain/kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt b/save-common/src/commonMain/kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt index ba9a481b2..d1d3a54a8 100644 --- a/save-common/src/commonMain/kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt +++ b/save-common/src/commonMain/kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt @@ -52,7 +52,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f * Execute [command] and wait for its completion. * * @param command executable command with arguments - * @param redirectTo a file where process output should be redirected. If null, output will be returned as [ExecutionResult.stdout]. + * @param redirectTo a file where process output and errors should be redirected. If null, output will be returned as [ExecutionResult.stdout] and [ExecutionResult.stderr]. * @return [ExecutionResult] built from process output * @throws ProcessExecutionException in case of impossibility of command execution */ @@ -107,6 +107,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f redirectTo?.let { fs.write(redirectTo) { write(stdout.joinToString("\n").encodeToByteArray()) + write(stderr.joinToString("\n").encodeToByteArray()) } } ?: logDebug("Execution output:\n$stdout") return ExecutionResult(status, stdout, stderr) 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 f13d2b513..44143b341 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 @@ -26,11 +26,14 @@ class FixAndWarnPlugin( testConfig: TestConfig, testFiles: List, fileSystem: FileSystem, - useInternalRedirections: Boolean = true) : Plugin( + useInternalRedirections: Boolean = true, + redirectTo: Path? = null, +) : Plugin( testConfig, testFiles, fileSystem, - useInternalRedirections) { + useInternalRedirections, + redirectTo) { private val fixPluginConfig: FixPluginConfig = testConfig.pluginConfigs.filterIsInstance().single().fix private val warnPluginConfig: WarnPluginConfig = 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 e8e852aa1..2dbb3b70a 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 @@ -29,12 +29,14 @@ class FixPlugin( testConfig: TestConfig, testFiles: List, fileSystem: FileSystem, - useInternalRedirections: Boolean = true + useInternalRedirections: Boolean = true, + redirectTo: Path? = null, ) : Plugin( testConfig, testFiles, fileSystem, - useInternalRedirections) { + useInternalRedirections, + redirectTo) { private val diffGenerator = DiffRowGenerator.create() .showInlineDiffs(true) .mergeOriginalRevised(false) @@ -58,7 +60,7 @@ class FixPlugin( val execCmd = "${(generalConfig.execCmd)} ${fixPluginConfig.execFlags} $testCopyNames" val executionResult = try { - pb.exec(execCmd, null) + pb.exec(execCmd, redirectTo) } catch (ex: ProcessExecutionException) { return@map chunk.map { TestResult( 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 59646e42f..edd82e3f6 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 @@ -29,12 +29,14 @@ class WarnPlugin( testConfig: TestConfig, testFiles: List, fileSystem: FileSystem, - useInternalRedirections: Boolean = true + useInternalRedirections: Boolean = true, + redirectTo: Path? = null, ) : Plugin( testConfig, testFiles, fileSystem, - useInternalRedirections) { + useInternalRedirections, + redirectTo) { private val expectedAndNotReceived = "Some warnings were expected but not received" private val unexpected = "Some warnings were unexpected" @@ -118,7 +120,7 @@ class WarnPlugin( val execCmd = "${generalConfig.execCmd} ${warnPluginConfig.execFlags} $fileNamesForExecCmd" val executionResult = try { - pb.exec("cd ${testConfig.getRootConfig().location.parent} && $execCmd", null) + pb.exec("cd ${testConfig.getRootConfig().location.parent} && $execCmd", redirectTo) } catch (ex: ProcessExecutionException) { return sequenceOf( TestResult(