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

Fail if testOnly does not match any test case #3224

Merged
merged 2 commits into from
Jun 21, 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
22 changes: 14 additions & 8 deletions scalalib/src/mill/scalalib/TestModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ trait TestModule
* with "bar", with "arguments" as arguments passing to test framework.
*/
def testOnly(args: String*): Command[(String, Seq[TestResult])] = {
val splitAt = args.indexOf("--")
val (selector, testArgs) =
if (splitAt == -1) (args, Seq.empty)
else {
val (s, t) = args.splitAt(splitAt)
val (selector, testArgs) = args.indexOf("--") match {
case -1 => (args, Seq.empty)
case pos =>
val (s, t) = args.splitAt(pos)
(s, t.tail)
}
}
T.command {
testTask(T.task { testArgs }, T.task { selector })()
}
Expand Down Expand Up @@ -152,6 +151,8 @@ trait TestModule
forkArgs() -> Map()
}

val selectors = globSelectors()

val testArgs = TestArgs(
framework = testFramework(),
classpath = runClasspath().map(_.path),
Expand All @@ -161,7 +162,7 @@ trait TestModule
colored = T.log.colored,
testCp = testClasspath().map(_.path),
home = T.home,
globSelectors = globSelectors()
globSelectors = selectors
)

val testRunnerClasspathArg = zincWorker().scalalibClasspath()
Expand Down Expand Up @@ -192,7 +193,12 @@ trait TestModule
val (doneMsg, results) = {
upickle.default.read[(String, Seq[TestResult])](jsonOutput)
}
TestModule.handleResults(doneMsg, results, T.ctx(), testReportXml())
if (results.isEmpty && selectors.nonEmpty) {
// no tests ran but we expected some to run, as we applied a filter (e.g. via `testOnly`)
Result.Failure(
s"Test selector does not match any test: ${selectors.mkString(" ")}" + "\nRun discoveredTestClasses to see available tests"
)
} else TestModule.handleResults(doneMsg, results, T.ctx(), testReportXml())
} catch {
case e: Throwable =>
Result.Failure("Test reporting failed: " + e)
Expand Down
7 changes: 7 additions & 0 deletions scalalib/test/src/mill/scalalib/TestRunnerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ object TestRunnerTests extends TestSuite {
"multi" - workspaceTest(testrunner) { eval =>
testOnly(eval, Seq("*Bar*", "*bar*"), 2)
}
"noMatch" - workspaceTest(testrunner) { eval =>
val Left(Result.Failure(msg, _)) =
eval.apply(testrunner.utest.testOnly("noMatch", "noMatch*2"))
assert(
msg == "Test selector does not match any test: noMatch noMatch*2\nRun discoveredTestClasses to see available tests"
)
}
}
}

Expand Down