Skip to content

Commit b0845e2

Browse files
committed
[tests] disable colors in the sbt process to avoid ascii sequences in the output that we parse, reuse more utilities between test code (part 2)
1 parent cd866f7 commit b0845e2

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

packaging/src/test/scala-2.12/org/jetbrains/sbtidea/packaging/RegenerateProjectsStructureTestData.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.jetbrains.sbtidea.packaging.structure.sbtImpl.SbtPackagedProjectNodeI
99
import org.jetbrains.sbtidea.packaging.testUtils.CirceEncodersDecoders.*
1010
import org.jetbrains.sbtidea.packaging.testUtils.{JsonUtils, RevisionReference, TestDataDir}
1111
import org.jetbrains.sbtidea.testUtils.IoUtils.*
12+
import org.jetbrains.sbtidea.testUtils.SbtProjectFilesUtils.IoMode.PrintAndCollectOutput
1213
import org.jetbrains.sbtidea.testUtils.{CurrentEnvironmentUtils, SbtProjectFilesUtils}
1314
import sbt.fileToRichFile
1415

@@ -241,8 +242,7 @@ object RegenerateProjectsStructureTestData {
241242
val outputLines = SbtProjectFilesUtils.runSbtProcess(
242243
Seq(s"dumpStructureToFile $baseTargetStructuresDir"),
243244
repoDir,
244-
inheritIO = false,
245-
printAndCollectOutput = true,
245+
ioMode = PrintAndCollectOutput,
246246
//ensure we reuse downloaded artifacts between tests if they need the same artifacts
247247
vmOptions = vmOptions,
248248
// Ensure the sbt process uses the same JVM that is used in the current app to ensure correct serialization

testUtils/src/test/scala/org/jetbrains/sbtidea/testUtils/CurrentEnvironmentUtils.scala

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.jetbrains.sbtidea.testUtils
22

3+
import org.jetbrains.sbtidea.testUtils.SbtProjectFilesUtils.IoMode.PrintAndCollectOutput
4+
35
import java.io.File
4-
import scala.util.Using
56

67
object CurrentEnvironmentUtils {
78

@@ -15,23 +16,11 @@ object CurrentEnvironmentUtils {
1516
def publishCurrentSbtIdeaPluginToLocalRepoAndGetVersions: String = {
1617
println("Publishing sbt-idea-plugin to local repository and getting it's version")
1718

18-
val process = new ProcessBuilder("sbt", "compile ; publishLocal ; show core / version")
19-
.directory(CurrentWorkingDir)
20-
.redirectErrorStream(true)
21-
.start()
22-
23-
val outputLines = Using.resource(scala.io.Source.fromInputStream(process.getInputStream)) { source =>
24-
source.getLines().map { line =>
25-
println(line)
26-
line
27-
}.toArray.toSeq
28-
}
29-
30-
process.waitFor()
31-
32-
val exitCode = process.exitValue()
33-
if (exitCode != 0)
34-
throw new RuntimeException(s"Failed to execute sbt command to detect current sbt-idea-plugin version (exit code: $exitCode)")
19+
val outputLines = SbtProjectFilesUtils.runSbtProcess(
20+
Seq("compile ; publishLocal ; show core / version"),
21+
CurrentWorkingDir,
22+
ioMode = PrintAndCollectOutput,
23+
).outputLines.get
3524

3625
val infoLines = outputLines
3726
.map(_.trim)
@@ -44,12 +33,12 @@ object CurrentEnvironmentUtils {
4433
val DebugInfo = if (infoLines.nonEmpty)
4534
s"""Last $TaleLastLinesNumber info lines:
4635
| ## ${infoLines.takeRight(TaleLastLinesNumber).mkString("\n ## ")}"""
47-
else if (outputLines.nonEmpty) {
36+
else if (outputLines.nonEmpty)
4837
s"""Last $TaleLastLinesNumber output lines:
4938
| ## ${outputLines.takeRight(TaleLastLinesNumber).mkString("\n ## ")}"""
50-
} else {
39+
else
5140
"!!! No output lines !!!"
52-
}
41+
5342
throw new RuntimeException(
5443
s"""Failed to retrieve plugin version from the sbt process output.
5544
|$DebugInfo""".stripMargin)

testUtils/src/test/scala/org/jetbrains/sbtidea/testUtils/SbtProjectFilesUtils.scala

+12-9
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ object SbtProjectFilesUtils {
5858
def runSbtProcess(
5959
sbtArguments: Seq[String],
6060
workingDir: File,
61-
inheritIO: Boolean = true,
62-
printAndCollectOutput: Boolean = false,
61+
ioMode: IoMode = IoMode.Inherit,
6362
vmOptions: Seq[String] = Seq.empty,
6463
envVars: Map[String, String] = Map.empty,
6564
): ProcessRunResult = {
@@ -69,28 +68,32 @@ object SbtProjectFilesUtils {
6968
runProcess(
7069
// Disable colors to avoid escape sequences in the output
7170
// This is needed to parse the output of the test reliably
72-
"sbt" +: "-no-colors" +: sbtArguments,
71+
Seq("sbt", "-no-colors") ++ sbtArguments,
7372
workingDir,
74-
inheritIO = inheritIO,
75-
printAndCollectOutput = printAndCollectOutput,
73+
ioMode = ioMode,
7674
envVars = envVarsUpdated,
7775
)
7876
}
7977

8078
case class ProcessRunResult(outputLines: Option[Seq[String]])
8179

80+
sealed trait IoMode
81+
object IoMode {
82+
object Inherit extends IoMode
83+
object PrintAndCollectOutput extends IoMode
84+
}
85+
8286
def runProcess(
8387
command: Seq[String],
8488
workingDir: File,
85-
inheritIO: Boolean = true,
86-
printAndCollectOutput: Boolean = false,
89+
ioMode: IoMode = IoMode.Inherit,
8790
envVars: Map[String, String] = Map.empty
8891
): ProcessRunResult = {
8992
val pb = new ProcessBuilder(command *)
9093
pb.directory(workingDir)
9194
pb.redirectErrorStream(true)
9295

93-
if (inheritIO) {
96+
if (ioMode == IoMode.Inherit) {
9497
pb.inheritIO()
9598
}
9699

@@ -100,7 +103,7 @@ object SbtProjectFilesUtils {
100103

101104
val process = pb.start()
102105

103-
val outputLines: Option[Seq[String]] = if (printAndCollectOutput) {
106+
val outputLines: Option[Seq[String]] = if (ioMode == IoMode.PrintAndCollectOutput) {
104107
Some(Using.resource(Source.fromInputStream(process.getInputStream)) { source =>
105108
source.getLines.map { line =>
106109
println(line)

0 commit comments

Comments
 (0)