Skip to content

Commit

Permalink
Support running mill under test in server mode (#100)
Browse files Browse the repository at this point in the history
This may only work for mill versions which support `-D` option.

* Fixes #97

There are other options to pass the temporary ivy repository to the mill version used in the tests. So, if this implementatin does not work for you, we have options... Let me know.

Pull request: #100
  • Loading branch information
lefou authored Apr 13, 2022
1 parent edc38f4 commit 5042de8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package de.tobiasroeser.mill.integrationtest

import java.nio.file.{CopyOption, LinkOption, StandardCopyOption}
import java.nio.file.attribute.PosixFilePermission

import scala.util.Try
import scala.util.control.NonFatal

import mainargs.Flag
import mill._
import mill.api.{Ctx, Result}
import mill.define.{Command, Sources, Target, Task, TaskModule}
import mill.scalalib._
import mill.scalalib.publish._
import os.ProcessOutput.Readlines
import os.{PathRedirect, ProcessOutput}

/**
Expand Down Expand Up @@ -147,15 +146,15 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
if (scala.util.Properties.isWin) (
testPath / "mill.bat",
s"""set JAVA_OPTS="-Divy.home=${ivyPath.toIO.getAbsolutePath()}"
|"${millExe.toIO.getAbsolutePath()}" -i --color false %*
|"${millExe.toIO.getAbsolutePath()}" %*
|""".stripMargin,
null
)
else (
testPath / "mill",
s"""#!/usr/bin/env sh
|export JAVA_OPTS="-Divy.home=${ivyPath.toIO.getAbsolutePath()}"
|exec ${millExe.toIO.getAbsolutePath()} -i --color false "$$@"
|exec ${millExe.toIO.getAbsolutePath()} "$$@"
|""".stripMargin,
os.PermSet(0) +
PosixFilePermission.OWNER_READ +
Expand All @@ -176,7 +175,7 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
else {
log.info(s"Invoking ${logLine}: ${invocation}")
val result = invocation match {
case invocation @ TestInvocation.Targets(targets, expectedExitCode, env) =>
case invocation @ TestInvocation.Targets(targets, expectedExitCode, env, noServer) =>
val outlog = os.temp(
dir = testPath,
prefix = "out-",
Expand All @@ -185,14 +184,24 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
)
val pathRedirect = os.PathRedirect(outlog)

val millOpts =
// --no-server is better, but not supported in older Mill versions
(if (noServer) Seq("-i") else Seq()) ++
Seq("--color", "false") ++
// -D is only supported since mill verison ???
(if (!noServer) Seq("-D", s"ivy.home=${ivyPath.toIO.getAbsolutePath()}") else Seq())

// run mill with test targets
// ENV=env mill -i testTargets
val result = os.proc(runScript, targets)
val result = os.proc(
runScript,
millOpts,
targets
)
.call(
cwd = testPath,
check = false,
stdout = pathRedirect,
// stderr = pathRedirect,
mergeErrIntoOut = true,
env = env
)
Expand All @@ -204,6 +213,23 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
TestResult.Failed
}

if (!noServer) {
log.debug(s"Stopping mill server in ${testPath} ...")
// clean possibly running server
try {
os.proc(runScript, millOpts, "shutdown")
.call(
cwd = testPath,
check = false,
stdout = Readlines(_ => ()),
mergeErrIntoOut = true,
env = env
)
} catch {
case NonFatal(_) => // ignore any errors
}
}

TestInvocationResult(invocation, res, result.out.lines, result.err.lines, Some(outlog))
}

Expand Down Expand Up @@ -363,7 +389,7 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
def pluginsUnderTest: Seq[PublishModule]

def transitivePluginsUnderTest: Seq[PublishModule] =
pluginsUnderTest.flatMap(_.transitiveModuleDeps).collect{
pluginsUnderTest.flatMap(_.transitiveModuleDeps).collect {
case m: PublishModule => m
}

Expand All @@ -375,7 +401,7 @@ trait MillIntegrationTestModule extends TaskModule with ExtraCoursierSupport wit
def temporaryIvyModules: Seq[PublishModule] = Seq()

def transitiveTemporaryIvyModules: Seq[PublishModule] =
temporaryIvyModules.flatMap(_.transitiveModuleDeps).collect{
temporaryIvyModules.flatMap(_.transitiveModuleDeps).collect {
case m: PublishModule => m
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ object TestInvocation {
final case class Targets(
targets: Seq[String],
expectedExitCode: Int = 0,
env: Map[String, String] = Map()
env: Map[String, String] = Map(),
noServer: Boolean = false
) extends TestInvocation {
override def toString: String =
getClass().getSimpleName() +
"(targets=" + targets +
",expectedExitCode=" + expectedExitCode +
",env=" + env
",env=" + env +
",noServer=" + noServer +
")"
}

Expand Down
3 changes: 2 additions & 1 deletion itest/src/01-simple/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ object itest extends Itest {
TestInvocation.Targets(Seq("verify")),
TestInvocation.Targets(Seq("-d", "fail"), 1),
TestInvocation.Targets(Seq("checkEnv"), 1),
TestInvocation.Targets(Seq("checkEnv"), env = Map("TEST_ENV" -> "SET"))
TestInvocation.Targets(Seq("checkEnv"), env = Map("TEST_ENV" -> "SET"), noServer = true),
TestInvocation.Targets(Seq("checkEnv"), env = Map("TEST_ENV" -> "SET"), noServer = false),
)
)
}
Expand Down

0 comments on commit 5042de8

Please sign in to comment.