Skip to content

Commit c22931f

Browse files
authored
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
1 parent 5b883c6 commit c22931f

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

save-common-test/src/commonNonJsMain/kotlin/org/cqfn/save/plugin/MockPlugin.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class MockPlugin(baseDir: Path, testFiles: List<String> = emptyList()) : Plugin(
1717
TestConfig((baseDir / "save.toml").also { fs.createFile(it) }, null, fs = fs),
1818
testFiles,
1919
fs,
20-
useInternalRedirections = true
20+
useInternalRedirections = true,
21+
redirectTo = null
2122
) {
2223
override fun handleFiles(files: Sequence<List<Path>>): Sequence<TestResult> = emptySequence()
2324

save-common/src/commonMain/kotlin/org/cqfn/save/core/plugin/Plugin.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import okio.Path
1717
* @property testFiles a list of files (test resources or save.toml configs)
1818
* @property fs describes the current file system
1919
* @property useInternalRedirections whether to redirect stdout/stderr for internal purposes
20+
* @property redirectTo a file where process output and errors should be redirected. If null, output will be returned as [ExecutionResult.stdout] and [ExecutionResult.stderr].
2021
*/
2122
abstract class Plugin(
2223
open val testConfig: TestConfig,
2324
protected val testFiles: List<String>,
2425
protected val fs: FileSystem,
25-
private val useInternalRedirections: Boolean) {
26+
private val useInternalRedirections: Boolean,
27+
protected val redirectTo: Path?) {
2628
/**
2729
* Instance that is capable of executing processes
2830
*/

save-common/src/commonMain/kotlin/org/cqfn/save/core/utils/ProcessBuilder.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f
5252
* Execute [command] and wait for its completion.
5353
*
5454
* @param command executable command with arguments
55-
* @param redirectTo a file where process output should be redirected. If null, output will be returned as [ExecutionResult.stdout].
55+
* @param redirectTo a file where process output and errors should be redirected. If null, output will be returned as [ExecutionResult.stdout] and [ExecutionResult.stderr].
5656
* @return [ExecutionResult] built from process output
5757
* @throws ProcessExecutionException in case of impossibility of command execution
5858
*/
@@ -107,6 +107,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f
107107
redirectTo?.let {
108108
fs.write(redirectTo) {
109109
write(stdout.joinToString("\n").encodeToByteArray())
110+
write(stderr.joinToString("\n").encodeToByteArray())
110111
}
111112
} ?: logDebug("Execution output:\n$stdout")
112113
return ExecutionResult(status, stdout, stderr)

save-plugins/fix-and-warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fixandwarn/FixAndWarnPlugin.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ class FixAndWarnPlugin(
2626
testConfig: TestConfig,
2727
testFiles: List<String>,
2828
fileSystem: FileSystem,
29-
useInternalRedirections: Boolean = true) : Plugin(
29+
useInternalRedirections: Boolean = true,
30+
redirectTo: Path? = null,
31+
) : Plugin(
3032
testConfig,
3133
testFiles,
3234
fileSystem,
33-
useInternalRedirections) {
35+
useInternalRedirections,
36+
redirectTo) {
3437
private val fixPluginConfig: FixPluginConfig =
3538
testConfig.pluginConfigs.filterIsInstance<FixAndWarnPluginConfig>().single().fix
3639
private val warnPluginConfig: WarnPluginConfig =

save-plugins/fix-plugin/src/commonMain/kotlin/org/cqfn/save/plugins/fix/FixPlugin.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class FixPlugin(
2929
testConfig: TestConfig,
3030
testFiles: List<String>,
3131
fileSystem: FileSystem,
32-
useInternalRedirections: Boolean = true
32+
useInternalRedirections: Boolean = true,
33+
redirectTo: Path? = null,
3334
) : Plugin(
3435
testConfig,
3536
testFiles,
3637
fileSystem,
37-
useInternalRedirections) {
38+
useInternalRedirections,
39+
redirectTo) {
3840
private val diffGenerator = DiffRowGenerator.create()
3941
.showInlineDiffs(true)
4042
.mergeOriginalRevised(false)
@@ -58,7 +60,7 @@ class FixPlugin(
5860

5961
val execCmd = "${(generalConfig.execCmd)} ${fixPluginConfig.execFlags} $testCopyNames"
6062
val executionResult = try {
61-
pb.exec(execCmd, null)
63+
pb.exec(execCmd, redirectTo)
6264
} catch (ex: ProcessExecutionException) {
6365
return@map chunk.map {
6466
TestResult(

save-plugins/warn-plugin/src/commonMain/kotlin/org/cqfn/save/plugin/warn/WarnPlugin.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class WarnPlugin(
2929
testConfig: TestConfig,
3030
testFiles: List<String>,
3131
fileSystem: FileSystem,
32-
useInternalRedirections: Boolean = true
32+
useInternalRedirections: Boolean = true,
33+
redirectTo: Path? = null,
3334
) : Plugin(
3435
testConfig,
3536
testFiles,
3637
fileSystem,
37-
useInternalRedirections) {
38+
useInternalRedirections,
39+
redirectTo) {
3840
private val expectedAndNotReceived = "Some warnings were expected but not received"
3941
private val unexpected = "Some warnings were unexpected"
4042

@@ -118,7 +120,7 @@ class WarnPlugin(
118120
val execCmd = "${generalConfig.execCmd} ${warnPluginConfig.execFlags} $fileNamesForExecCmd"
119121

120122
val executionResult = try {
121-
pb.exec("cd ${testConfig.getRootConfig().location.parent} && $execCmd", null)
123+
pb.exec("cd ${testConfig.getRootConfig().location.parent} && $execCmd", redirectTo)
122124
} catch (ex: ProcessExecutionException) {
123125
return sequenceOf(
124126
TestResult(

0 commit comments

Comments
 (0)