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

Add TestModule.discoveredTestClasses target to ease test inspection #3191

Merged
merged 1 commit into from
May 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading