Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirection of stdout stream to stderr when using show #2689

Merged
merged 11 commits into from Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,9 @@
package mill.integration

import mill.main.client.Util
import utest._

import scala.collection.mutable
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.duration.SECONDS
Expand Down Expand Up @@ -31,127 +34,146 @@ object WatchSourceInputTests extends IntegrationTestSuite {
}
}

def testWatchSource(show: Boolean) = {
val showArgs = if (show) Seq("show") else Nil

val evalResult = Future { evalTimeoutStdout(maxDuration, "--watch", showArgs, "qux") }
val expectedPrints = collection.mutable.Buffer.empty[String]
val expectedShows = collection.mutable.Buffer.empty[String]

awaitCompletionMarker("initialized0")
awaitCompletionMarker("quxRan0")
expectedPrints.append(
"Setting up build.sc",
"Running qux foo contents initial-foo1 initial-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents initial-foo1 initial-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "foo1.txt", "edited-foo1")
awaitCompletionMarker("quxRan1")
expectedPrints.append(
"Running qux foo contents edited-foo1 initial-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 initial-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "foo2.txt", "edited-foo2")
awaitCompletionMarker("quxRan2")
expectedPrints.append(
"Running qux foo contents edited-foo1 edited-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "bar.txt", "edited-bar")
awaitCompletionMarker("quxRan3")
expectedPrints.append(
"Running qux foo contents edited-foo1 edited-foo2",
"Running qux bar contents edited-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents edited-bar"
)

os.write.append(wsRoot / "build.sc", "\ndef unrelated = true")
awaitCompletionMarker("initialized1")
expectedPrints.append(
"Setting up build.sc"
// These targets do not re-evaluate, because the change to the build
// file was unrelated to them and does not affect their transitive callgraph
// "Running qux foo contents edited-foo1 edited-foo2",
// "Running qux bar contents edited-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents edited-bar"
)

os.write.over(wsRoot / "watchValue.txt", "exit")
awaitCompletionMarker("initialized2")
expectedPrints.append("Setting up build.sc")

val res = Await.result(evalResult, Duration.apply(maxDuration, SECONDS))

val (shows, prints) = res.out.linesIterator.toVector.partition(_.startsWith("\""))

assert(prints == expectedPrints)
def testBase(show: Boolean)(f: (
mutable.Buffer[String],
mutable.Buffer[String],
mutable.Buffer[String]
) => IntegrationTestSuite.EvalResult): Unit = {
val expectedOut = mutable.Buffer.empty[String]
// Most of these are normal `println`s, so they go to `stdout` by
// default unless you use `show` in which case they go to `stderr`.
val expectedErr = if (show) mutable.Buffer.empty[String] else expectedOut
val expectedShows = mutable.Buffer.empty[String]
val res = f(expectedOut, expectedErr, expectedShows)
val (shows, out) = res.out.linesIterator.toVector.partition(_.startsWith("\""))
val err = res.err.linesIterator.toVector
.filter(!_.contains("Watching for changes"))
.filter(!_.contains("[info] compiling"))
.filter(!_.contains("[info] done compiling"))

assert(out == expectedOut)

// If show is not enabled, we don't expect any of our custom prints to go to stderr
if (show) assert(err == expectedErr)
else assert(err.isEmpty)

if (show) assert(shows == expectedShows.map('"' + _ + '"'))
}

def testWatchSource(show: Boolean) =
testBase(show) { (expectedOut, expectedErr, expectedShows) =>
val showArgs = if (show) Seq("show") else Nil

val evalResult = Future { evalTimeoutStdout(maxDuration, "--watch", showArgs, "qux") }

awaitCompletionMarker("initialized0")
awaitCompletionMarker("quxRan0")
expectedOut.append(
"Setting up build.sc"
)
expectedErr.append(
"Running qux foo contents initial-foo1 initial-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents initial-foo1 initial-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "foo1.txt", "edited-foo1")
awaitCompletionMarker("quxRan1")
expectedErr.append(
"Running qux foo contents edited-foo1 initial-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 initial-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "foo2.txt", "edited-foo2")
awaitCompletionMarker("quxRan2")
expectedErr.append(
"Running qux foo contents edited-foo1 edited-foo2",
"Running qux bar contents initial-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents initial-bar"
)

os.write.over(wsRoot / "bar.txt", "edited-bar")
awaitCompletionMarker("quxRan3")
expectedErr.append(
"Running qux foo contents edited-foo1 edited-foo2",
"Running qux bar contents edited-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents edited-bar"
)

os.write.append(wsRoot / "build.sc", "\ndef unrelated = true")
awaitCompletionMarker("initialized1")
expectedOut.append(
"Setting up build.sc"
// These targets do not re-evaluate, because the change to the build
// file was unrelated to them and does not affect their transitive callgraph
// "Running qux foo contents edited-foo1 edited-foo2",
// "Running qux bar contents edited-bar"
)
expectedShows.append(
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents edited-bar"
)

os.write.over(wsRoot / "watchValue.txt", "exit")
awaitCompletionMarker("initialized2")
expectedOut.append("Setting up build.sc")

Await.result(evalResult, Duration.apply(maxDuration, SECONDS))
}

test("sources") {

// Make sure we clean up the workspace between retries
test("noshow") - retry(3) { initWorkspace(); testWatchSource(false) }
test("show") - retry(3) { initWorkspace(); testWatchSource(true) }
test("noshow") - retry(3) { if (!Util.isWindows) { initWorkspace(); testWatchSource(false) } }
test("show") - retry(3) { if (!Util.isWindows) { initWorkspace(); testWatchSource(true) } }
}

def testWatchInput(show: Boolean) = {
val showArgs = if (show) Seq("show") else Nil

val evalResult = Future { evalTimeoutStdout(maxDuration, "--watch", showArgs, "lol") }
val expectedPrints = collection.mutable.Buffer.empty[String]
val expectedShows = collection.mutable.Buffer.empty[String]

awaitCompletionMarker("initialized0")
awaitCompletionMarker("lolRan0")
expectedPrints.append(
"Setting up build.sc",
"Running lol baz contents initial-baz"
)
expectedShows.append("Running lol baz contents initial-baz")

os.write.over(wsRoot / "baz.txt", "edited-baz")
awaitCompletionMarker("lolRan1")
expectedPrints.append("Running lol baz contents edited-baz")
expectedShows.append("Running lol baz contents edited-baz")

os.write.over(wsRoot / "watchValue.txt", "edited-watchValue")
awaitCompletionMarker("initialized1")
expectedPrints.append("Setting up build.sc")
expectedShows.append("Running lol baz contents edited-baz")

os.write.over(wsRoot / "watchValue.txt", "exit")
awaitCompletionMarker("initialized2")
expectedPrints.append("Setting up build.sc")

val res = Await.result(evalResult, Duration.apply(maxDuration, SECONDS))

val (shows, prints) = res.out.linesIterator.toVector.partition(_.startsWith("\""))
assert(prints == expectedPrints)
if (show) assert(shows == expectedShows.map('"' + _ + '"'))
}
def testWatchInput(show: Boolean) =
testBase(show) { (expectedOut, expectedErr, expectedShows) =>
val showArgs = if (show) Seq("show") else Nil

val evalResult = Future { evalTimeoutStdout(maxDuration, "--watch", showArgs, "lol") }

awaitCompletionMarker("initialized0")
awaitCompletionMarker("lolRan0")
expectedOut.append(
"Setting up build.sc"
)
expectedErr.append(
"Running lol baz contents initial-baz"
)
expectedShows.append("Running lol baz contents initial-baz")

os.write.over(wsRoot / "baz.txt", "edited-baz")
awaitCompletionMarker("lolRan1")
expectedErr.append("Running lol baz contents edited-baz")
expectedShows.append("Running lol baz contents edited-baz")

os.write.over(wsRoot / "watchValue.txt", "edited-watchValue")
awaitCompletionMarker("initialized1")
expectedOut.append("Setting up build.sc")
expectedShows.append("Running lol baz contents edited-baz")

os.write.over(wsRoot / "watchValue.txt", "exit")
awaitCompletionMarker("initialized2")
expectedOut.append("Setting up build.sc")

Await.result(evalResult, Duration.apply(maxDuration, SECONDS))
}

test("input") {

// Make sure we clean up the workspace between retries
test("noshow") - retry(3) { initWorkspace(); testWatchInput(false) }
test("show") - retry(3) { initWorkspace(); testWatchInput(true) }
test("noshow") - retry(3) { if (!Util.isWindows) { initWorkspace(); testWatchInput(false) } }
test("show") - retry(3) { if (!Util.isWindows) { initWorkspace(); testWatchInput(true) } }
}
}
}
10 changes: 4 additions & 6 deletions main/src/mill/main/MainModule.scala
Expand Up @@ -32,14 +32,12 @@ object MainModule {
log: Logger,
watch0: Watchable => Unit
)(f: Seq[(Any, Option[(RunScript.TaskName, ujson.Value)])] => ujson.Value) = {

RunScript.evaluateTasksNamed(
// When using `show`, redirect all stdout of the evaluated tasks so the
// printed JSON is the only thing printed to stdout.
evaluator.withBaseLogger(
// When using `show`, redirect all stdout of the evaluated tasks so the
// printed JSON is the only thing printed to stdout.
evaluator.baseLogger match {
case p: PrintLogger => p.withOutStream(p.errorStream)
case l => l
}
evaluator.baseLogger.withOutStream(evaluator.baseLogger.errorStream)
),
targets,
Separated
Expand Down
59 changes: 53 additions & 6 deletions main/test/src/mill/main/MainModuleTests.scala
Expand Up @@ -6,11 +6,25 @@ import mill.define.{Cross, Discover, Module, Task}
import mill.util.{TestEvaluator, TestUtil}
import utest.{TestSuite, Tests, assert, test}

import java.io.{ByteArrayOutputStream, PrintStream}

object MainModuleTests extends TestSuite {

object mainModule extends TestUtil.BaseModule with MainModule {
def hello = T { Seq("hello", "world") }
def hello2 = T { Map("1" -> "hello", "2" -> "world") }
def hello = T {
System.out.println("Hello System Stdout")
System.err.println("Hello System Stderr")
Console.out.println("Hello Console Stdout")
Console.err.println("Hello Console Stderr")
Seq("hello", "world")
}
def hello2 = T {
System.out.println("Hello2 System Stdout")
System.err.println("Hello2 System Stderr")
Console.out.println("Hello2 Console Stdout")
Console.err.println("Hello2 Console Stderr")
Map("1" -> "hello", "2" -> "world")
}
def helloCommand(x: Int, y: Task[String]) = T.command { (x, y(), hello()) }
override lazy val millDiscover: Discover[this.type] = Discover[this.type]
}
Expand Down Expand Up @@ -81,7 +95,13 @@ object MainModuleTests extends TestSuite {
}

test("show") {
val evaluator = new TestEvaluator(mainModule)
val outStream = new ByteArrayOutputStream()
val errStream = new ByteArrayOutputStream()
val evaluator = new TestEvaluator(
mainModule,
outStream = new PrintStream(outStream, true),
errStream = new PrintStream(errStream, true)
)
test("single") {
val results =
evaluator.evaluator.evaluate(Agg(mainModule.show(evaluator.evaluator, "hello")))
Expand All @@ -90,7 +110,20 @@ object MainModuleTests extends TestSuite {

val Result.Success(Val(value)) = results.rawValues.head

assert(value == ujson.Arr.from(Seq("hello", "world")))
val shown = ujson.read(outStream.toByteArray)
val expected = ujson.Arr.from(Seq("hello", "world"))
assert(value == expected)
assert(shown == expected)

// Make sure both stdout and stderr are redirected by `show`
// to stderr so that only the JSON file value goes to stdout
val strippedErr =
fansi.Str(errStream.toString, errorMode = fansi.ErrorMode.Sanitize).plainText

assert(strippedErr.contains("Hello System Stdout"))
assert(strippedErr.contains("Hello System Stderr"))
assert(strippedErr.contains("Hello Console Stdout"))
assert(strippedErr.contains("Hello Console Stderr"))
}
test("multi") {
val results =
Expand All @@ -105,10 +138,24 @@ object MainModuleTests extends TestSuite {

val Result.Success(Val(value)) = results.rawValues.head

assert(value == ujson.Obj(
val shown = ujson.read(outStream.toByteArray)

val expected = ujson.Obj(
"hello" -> ujson.Arr("hello", "world"),
"hello2" -> ujson.Obj("1" -> "hello", "2" -> "world")
))
)
assert(value == expected)
assert(shown == expected)

// Make sure both stdout and stderr are redirected by `show`
// to stderr so that only the JSON file value goes to stdout
val strippedErr =
fansi.Str(errStream.toString, errorMode = fansi.ErrorMode.Sanitize).plainText

assert(strippedErr.contains("Hello2 System Stdout"))
assert(strippedErr.contains("Hello2 System Stderr"))
assert(strippedErr.contains("Hello2 Console Stdout"))
assert(strippedErr.contains("Hello2 Console Stderr"))
}

test("command") {
Expand Down
2 changes: 2 additions & 0 deletions main/test/src/mill/util/TestEvaluator.scala
Expand Up @@ -22,6 +22,7 @@ class TestEvaluator(
failFast: Boolean = false,
threads: Option[Int] = Some(1),
outStream: PrintStream = System.out,
errStream: PrintStream = System.err,
inStream: InputStream = DummyInputStream,
debugEnabled: Boolean = false,
extraPathEnd: Seq[String] = Seq.empty,
Expand All @@ -32,6 +33,7 @@ class TestEvaluator(
failFast,
threads,
outStream,
errStream,
inStream,
debugEnabled,
extraPathEnd,
Expand Down