Skip to content

Commit

Permalink
Fix ScalaTest testOnly include option (com-lihaoyi#3557)
Browse files Browse the repository at this point in the history
Fixes com-lihaoyi#3440

The problem is mill defines all the test tasks with
`explicitlySpecified` to be `true`. This makes all the tests always be
included. [Related
code](https://github.com/scalatest/scalatest/blob/main/jvm/core/src/main/scala/org/scalatest/tools/Framework.scala#L267)
in Scalatest.
  • Loading branch information
wb14123 authored Sep 16, 2024
1 parent 78b9d62 commit b9b6433
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mill.scalalib

import org.scalatest.Tag
import org.scalatest.freespec.AnyFreeSpec

object TaggedTest extends Tag("tagged")

class ScalaTestSpec extends AnyFreeSpec {

"A Set" - {
Expand All @@ -15,6 +18,10 @@ class ScalaTestSpec extends AnyFreeSpec {
Set.empty.head
}
}

"should be tagged" taggedAs(TaggedTest) in {
assert(true)
}
}
}
}
36 changes: 33 additions & 3 deletions scalalib/test/src/mill/scalalib/TestRunnerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import mill.{Agg, T}
import os.Path
import sbt.testing.Status
import utest._
import utest.framework.TestPath

import java.io.{ByteArrayOutputStream, PrintStream}
import scala.xml.{Elem, NodeSeq, XML}
Expand Down Expand Up @@ -147,15 +146,46 @@ object TestRunnerTests extends TestSuite {
test("ScalaTest") {
test("test") - UnitTester(testrunner, resourcePath).scoped { eval =>
val Right(result) = eval(testrunner.scalatest.test())
assert(result.value._2.size == 2)
junitReportIn(eval.outPath, "scalatest").shouldHave(2 -> Status.Success)
assert(result.value._2.size == 3)
junitReportIn(eval.outPath, "scalatest").shouldHave(3 -> Status.Success)
}
test("discoveredTestClasses") - UnitTester(testrunner, resourcePath).scoped { eval =>
val Right(result) = eval.apply(testrunner.scalatest.discoveredTestClasses)
val expected = Seq("mill.scalalib.ScalaTestSpec")
assert(result.value == expected)
expected
}

test("testOnly") - {
def testOnly(eval: UnitTester, args: Seq[String], size: Int) = {
val Right(result) = eval.apply(testrunner.scalatest.testOnly(args: _*))
val testOnly = result.value.asInstanceOf[(String, Seq[mill.testrunner.TestResult])]
assert(
testOnly._2.size == size
)
}
test("all") - UnitTester(testrunner, resourcePath).scoped { eval =>
testOnly(eval, Seq("mill.scalalib.ScalaTestSpec"), 3)
}
test("include") - UnitTester(testrunner, resourcePath).scoped { eval =>
testOnly(eval, Seq("mill.scalalib.ScalaTestSpec", "--", "-n", "tagged"), 1)
}
test("exclude") - UnitTester(testrunner, resourcePath).scoped { eval =>
testOnly(eval, Seq("mill.scalalib.ScalaTestSpec", "--", "-l", "tagged"), 2)
}
test("includeAndExclude") - UnitTester(testrunner, resourcePath).scoped { eval =>
val Left(Result.Failure(msg, _)) =
eval.apply(testrunner.scalatest.testOnly(
"mill.scalalib.ScalaTestSpec",
"--",
"-n",
"tagged",
"-l",
"tagged"
))
assert(msg.contains("Test selector does not match any test"))
}
}
}

test("ZioTest") {
Expand Down
2 changes: 1 addition & 1 deletion testrunner/src/mill/testrunner/TestRunnerUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ import scala.jdk.CollectionConverters.IteratorHasAsScala
yield new TaskDef(
cls.getName.stripSuffix("$"),
fingerprint,
true,
false,
Array(new SuiteSelector)
)
)
Expand Down

0 comments on commit b9b6433

Please sign in to comment.