Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Mar 31, 2023
1 parent 7839169 commit 1a43868
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions example/web/1-hello-webapp/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ object app extends BuildModule with ScalaModule{
> curl http://localhost:8080
<!DOCTYPE html><html><body><h1>Hello World</h1><p>I am cow</p></body></html>
> cat stdout.log
webapp.WebApp.hello() called
> ./mill clean runBackground
*/
1 change: 1 addition & 0 deletions example/web/1-hello-webapp/src/WebApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import scalatags.Text.all._
object WebApp extends cask.MainRoutes{
@cask.get("/")
def hello() = {
println("webapp.WebApp.hello() called")
doctype("html")(
html(
body(
Expand Down
8 changes: 8 additions & 0 deletions integration/src/mill/integration/ExampleTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ object ExampleTestSuite extends IntegrationTestSuite{
IntegrationTestSuite.EvalResult(res.is2xx, res.text(), "")
)

case s"> cat $path" =>
Thread.sleep(500) // Need to give backgroundWrapper time to spin up
val res = os.read(os.Path(path, workspaceRoot))
validateEval(
expectedSnippets,
IntegrationTestSuite.EvalResult(true, res, "")
)

case s"> node $rest" =>
val res = os
.proc("node", rest.split(" "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ abstract class IntegrationTestSuite extends TestSuite{
try {
os.proc(millReleaseFileOpt.get, serverArgs, debugArgs, s).call(
cwd = wd,
stdin = "",
stdin = os.Inherit,
stdout = stdout,
stderr = stderr,
env = millTestSuiteEnv
Expand Down
21 changes: 12 additions & 9 deletions main/src/mill/modules/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ object Jvm extends CoursierSupport {

ctx.log.debug(s"Run subprocess with args: ${args.map(a => s"'${a}'").mkString(" ")}")

if (background) spawnSubprocess(args, envArgs, workingDir)
if (background) spawnSubprocess(args, envArgs, workingDir, background = true)
else runSubprocess(args, envArgs, workingDir)
}

/**
* Runs a generic subprocess and waits for it to terminate.
*/
def runSubprocess(commandArgs: Seq[String], envArgs: Map[String, String], workingDir: os.Path) = {
val process = spawnSubprocess(commandArgs, envArgs, workingDir)
val process = spawnSubprocess(commandArgs, envArgs, workingDir, background = false)
val shutdownHook = new Thread("subprocess-shutdown") {
override def run(): Unit = {
System.err.println("Host JVM shutdown. Forcefully destroying subprocess ...")
Expand Down Expand Up @@ -153,23 +153,26 @@ object Jvm extends CoursierSupport {
def spawnSubprocess(
commandArgs: Seq[String],
envArgs: Map[String, String],
workingDir: os.Path
workingDir: os.Path,
background: Boolean = false
): SubProcess = {
// If System.in is fake, then we pump output manually rather than relying
// on `os.Inherit`. That is because `os.Inherit` does not follow changes
// to System.in/System.out/System.err, so the subprocess's streams get sent
// to the parent process's origin outputs even if we want to direct them
// elsewhere

if (System.in.isInstanceOf[PipedInputStream]) {
val process = os.proc(commandArgs).spawn(
cwd = workingDir,
env = envArgs,
stdin = os.Pipe,
stdout = os.Pipe,
stderr = os.Pipe
stdin = if (!background) os.Pipe else "",
stdout = if (!background) os.Pipe else workingDir / "stdout.log",
stderr = if (!background) os.Pipe else workingDir / "stderr.log"
)

val sources = Seq(

(process.stdout, System.out, "spawnSubprocess.stdout", false, () => true),
(process.stderr, System.err, "spawnSubprocess.stderr", false, () => true),
(System.in, process.stdin, "spawnSubprocess.stdin", true, () => process.isAlive())
Expand All @@ -189,9 +192,9 @@ object Jvm extends CoursierSupport {
os.proc(commandArgs).spawn(
cwd = workingDir,
env = envArgs,
stdin = os.Inherit,
stdout = os.Inherit,
stderr = os.Inherit
stdin = if (!background) os.Inherit else "",
stdout = if (!background) os.Inherit else workingDir / "stdout.log",
stderr = if (!background) os.Inherit else workingDir / "stderr.log"
)
}
}
Expand Down

0 comments on commit 1a43868

Please sign in to comment.