Skip to content

Commit

Permalink
Add TestModule.discoveredTestClasses target to ease test inspection (
Browse files Browse the repository at this point in the history
…#3191)

Example usage:

```
> mill show __.discoverTestClasses
{
  "foo.test.discoverTestClasses": [
    "foo.tests.FooTests",
    "foo.tests.FoobarTests"
  ]
  "bar.test.discoverTestClasses": [
    "foo.tests.BarTests"
  ]
}
```

Fix #3169

Pull request: #3191
  • Loading branch information
lefou authored May 29, 2024
1 parent 166c6d2 commit 4916d61
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
21 changes: 20 additions & 1 deletion scalalib/src/mill/scalalib/TestModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mill.scalalib
import mill.api.{Ctx, PathRef, Result}
import mill.define.{Command, Task, TaskModule}
import mill.scalalib.bsp.{BspBuildTarget, BspModule}
import mill.testrunner.{Framework, TestArgs, TestResult, TestRunner}
import mill.testrunner.{Framework, TestArgs, TestResult, TestRunner, TestRunnerUtils}
import mill.util.Jvm
import mill.{Agg, T}
import sbt.testing.Status
Expand Down Expand Up @@ -46,6 +46,25 @@ trait TestModule
*/
def testFramework: T[String]

def discoveredTestClasses: T[Seq[String]] = T {
val classes = Jvm.inprocess(
runClasspath().map(_.path),
classLoaderOverrideSbtTesting = true,
isolated = true,
closeContextClassLoaderWhenDone = true,
cl => {
val framework = Framework.framework(testFramework())(cl)
val classes = TestRunnerUtils.discoverTests(cl, framework, testClasspath().map(_.path))
classes.toSeq.map(_._1.getName())
.map {
case s if s.endsWith("$") => s.dropRight(1)
case s => s
}
}
)
classes.sorted
}

/**
* Discovers and runs the module's tests in a subprocess, reporting the
* results to the console.
Expand Down
30 changes: 26 additions & 4 deletions scalalib/test/src/mill/scalalib/TestRunnerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,27 @@ object TestRunnerTests extends TestSuite {
}

override def tests: Tests = Tests {
"TestRunner" - {
"utest" - {
"test case lookup" - workspaceTest(testrunner) { eval =>
test("TestRunner") - {
test("utest") - {
test("test case lookup") - workspaceTest(testrunner) { eval =>
val Right((result, _)) = eval.apply(testrunner.utest.test())
val test = result.asInstanceOf[(String, Seq[mill.testrunner.TestResult])]
assert(
test._2.size == 3
)
junitReportIn(eval.outPath, "utest").shouldHave(3 -> Status.Success)
}
"testOnly" - {
test("discoveredTestClasses") - workspaceTest(testrunner) { eval =>
val Right((res, _)) = eval.apply(testrunner.utest.discoveredTestClasses)
val expected = Seq(
"mill.scalalib.BarTests",
"mill.scalalib.FooTests",
"mill.scalalib.FoobarTests"
)
assert(res == expected)
expected
}
test("testOnly") - {
def testOnly(eval: TestEvaluator, args: Seq[String], size: Int) = {
val Right((result1, _)) = eval.apply(testrunner.utest.testOnly(args: _*))
val testOnly = result1.asInstanceOf[(String, Seq[mill.testrunner.TestResult])]
Expand Down Expand Up @@ -144,6 +154,12 @@ object TestRunnerTests extends TestSuite {
junitReportIn(eval.outPath, "scalatest").shouldHave(2 -> Status.Success)
}
}
test("discoveredTestClasses") - workspaceTest(testrunner) { eval =>
val Right((res, _)) = eval.apply(testrunner.scalatest.discoveredTestClasses)
val expected = Seq("mill.scalalib.ScalaTestSpec")
assert(res == expected)
expected
}
}

"ZioTest" - {
Expand All @@ -154,6 +170,12 @@ object TestRunnerTests extends TestSuite {
junitReportIn(eval.outPath, "ziotest").shouldHave(1 -> Status.Success)
}
}
test("discoveredTestClasses") - workspaceTest(testrunner) { eval =>
val Right((res, _)) = eval.apply(testrunner.ziotest.discoveredTestClasses)
val expected = Seq("mill.scalalib.ZioTestSpec")
assert(res == expected)
expected
}
}
}
}
Expand Down

0 comments on commit 4916d61

Please sign in to comment.