Skip to content

Commit

Permalink
Create the logic in Plugins, which will manage redirectTo for Process…
Browse files Browse the repository at this point in the history
…Builder (#220)

* Create the logic in Plugins, which will manage redirectTo for ProcessBuilder

What's done:
* Added logic for redirectTo
Closes #122
  • Loading branch information
Cheshiriks authored Aug 31, 2021
1 parent 5b883c6 commit c22931f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class MockPlugin(baseDir: Path, testFiles: List<String> = 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<List<Path>>): Sequence<TestResult> = emptySequence()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
protected val fs: FileSystem,
private val useInternalRedirections: Boolean) {
private val useInternalRedirections: Boolean,
protected val redirectTo: Path?) {
/**
* Instance that is capable of executing processes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class FixAndWarnPlugin(
testConfig: TestConfig,
testFiles: List<String>,
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<FixAndWarnPluginConfig>().single().fix
private val warnPluginConfig: WarnPluginConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class FixPlugin(
testConfig: TestConfig,
testFiles: List<String>,
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)
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class WarnPlugin(
testConfig: TestConfig,
testFiles: List<String>,
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"

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c22931f

Please sign in to comment.