Skip to content

Commit dc5cc72

Browse files
committed
Downgrade the test-runner module dependency when it's added to the classpath for Scala >= 3.0.0 and < current Scala 3 LTS version
1 parent f585762 commit dc5cc72

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

modules/integration/src/test/scala/scala/cli/integration/RunTestsDefault.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ class RunTestsDefault extends RunTestDefinitions
189189
}
190190

191191
for {
192-
scalaVersion <-
193-
Constants.legacyScala3Versions.sorted.reverse.distinctBy(_.split('.').take(2).mkString("."))
192+
scalaVersion <- TestUtil.legacyScalaVersionsOnePerMinor
194193
expectedMessage = "Hello, world!"
195194
expectedWarning =
196195
s"Defaulting to a legacy runner module version: ${Constants.runnerLegacyVersion}"

modules/integration/src/test/scala/scala/cli/integration/TestTestsDefault.scala

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package scala.cli.integration
33
import com.eed3si9n.expecty.Expecty.expect
44

55
import java.io.File
6-
76
import scala.cli.integration.Constants.munitVersion
7+
import scala.cli.integration.TestUtil.StringOps
88

99
class TestTestsDefault extends TestTestDefinitions with TestDefault {
1010
test("Pure Java with Scala tests") {
@@ -62,11 +62,39 @@ class TestTestsDefault extends TestTestDefinitions with TestDefault {
6262
).fromRoot { root =>
6363
val output = os.proc(TestUtil.cli, "test", extraOptions, ".", "--cross", "--power")
6464
.call(cwd = root).out.text()
65-
def countOccurrences(a: String, b: String): Int =
66-
if (b.isEmpty) 0 // Avoid infinite splitting
67-
else a.sliding(b.length).count(_ == b)
6865
expect(output.contains(expectedMessage))
69-
expect(countOccurrences(output, expectedMessage) == crossVersions.length)
66+
expect(output.countOccurrences(expectedMessage) == crossVersions.length)
7067
}
7168
}
69+
70+
for {
71+
scalaVersion <- TestUtil.legacyScalaVersionsOnePerMinor
72+
expectedMessage = "Hello, world!"
73+
expectedWarning =
74+
s"Defaulting to a legacy test-runner module version: ${Constants.runnerLegacyVersion}"
75+
}
76+
test(s"run a simple test with Scala $scalaVersion (legacy)") {
77+
TestInputs(os.rel / "example.test.scala" ->
78+
// using JUnit to work around TASTy and macro incompatibilities
79+
s"""//> using dep com.novocode:junit-interface:0.11
80+
|import org.junit.Test
81+
|
82+
|class MyTests {
83+
| @Test
84+
| def foo(): Unit = {
85+
| assert(2 + 2 == 4)
86+
| println("$expectedMessage")
87+
| }
88+
|}
89+
|""".stripMargin).fromRoot { root =>
90+
val res =
91+
os.proc(TestUtil.cli, "test", ".", "-S", scalaVersion, TestUtil.extraOptions)
92+
.call(cwd = root, stderr = os.Pipe)
93+
val out = res.out.trim()
94+
expect(out.contains(expectedMessage))
95+
val err = res.err.trim()
96+
expect(err.contains(expectedWarning))
97+
expect(err.countOccurrences(expectedWarning) == 1)
98+
}
99+
}
72100
}

modules/integration/src/test/scala/scala/cli/integration/TestUtil.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ object TestUtil {
2727
val cli: Seq[String] = cliCommand(cliPath)
2828
val ltsEqualsNext: Boolean = Constants.scala3Lts equals Constants.scala3Next
2929

30+
lazy val legacyScalaVersionsOnePerMinor: Seq[String] =
31+
Constants.legacyScala3Versions.sorted.reverse.distinctBy(_.split('.').take(2).mkString("."))
32+
3033
def cliCommand(cliPath: String): Seq[String] =
3134
if (isNativeCli)
3235
Seq(cliPath)
@@ -367,4 +370,10 @@ object TestUtil {
367370
Thread.sleep(200L)
368371
if (proc.isAlive()) proc.destroyForcibly()
369372
}
373+
374+
implicit class StringOps(a: String) {
375+
def countOccurrences(b: String): Int =
376+
if (b.isEmpty) 0 // Avoid infinite splitting
377+
else a.sliding(b.length).count(_ == b)
378+
}
370379
}

modules/options/src/main/scala/scala/build/Artifacts.scala

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,32 @@ object Artifacts {
130130
): Either[BuildException, Artifacts] = either {
131131
val dependencies = defaultDependencies ++ extraDependencies
132132

133+
val scalaVersion = (for {
134+
scalaArtifactsParams <- scalaArtifactsParamsOpt
135+
scalaParams = scalaArtifactsParams.params
136+
scalaVersion = scalaParams.scalaVersion
137+
} yield scalaVersion).getOrElse(defaultScalaVersion)
138+
139+
val shouldUseLegacyRunners =
140+
scalaVersion.startsWith("3") &&
141+
scalaVersion.coursierVersion < s"$scala3LtsPrefix.0".coursierVersion
142+
133143
val jvmTestRunnerDependencies =
134-
if (addJvmTestRunner)
135-
Seq(dep"$testRunnerOrganization::$testRunnerModuleName:$testRunnerVersion")
136-
else
137-
Nil
144+
if addJvmTestRunner then {
145+
val testRunnerVersion0 =
146+
if shouldUseLegacyRunners then {
147+
logger.message(
148+
s"""$warnPrefix Scala $scalaVersion is no longer supported by the test-runner module.
149+
|$warnPrefix Defaulting to a legacy test-runner module version: $runnerLegacyVersion.
150+
|$warnPrefix To use the latest test-runner, upgrade Scala to at least $scala3LtsPrefix."""
151+
.stripMargin
152+
)
153+
runnerLegacyVersion
154+
}
155+
else testRunnerVersion
156+
Seq(dep"$testRunnerOrganization::$testRunnerModuleName:$testRunnerVersion0")
157+
}
158+
else Nil
138159

139160
val jmhDependencies = addJmhDependencies.toSeq
140161
.map(version => dep"${Constants.jmhOrg}:${Constants.jmhGeneratorBytecodeModule}:$version")
@@ -408,15 +429,8 @@ object Artifacts {
408429
if runnerVersion.endsWith("SNAPSHOT") then
409430
Seq(coursier.Repositories.sonatype("snapshots"))
410431
else Nil
411-
val scalaVersion = (for {
412-
scalaArtifactsParams <- scalaArtifactsParamsOpt
413-
scalaParams = scalaArtifactsParams.params
414-
scalaVersion = scalaParams.scalaVersion
415-
} yield scalaVersion).getOrElse(defaultScalaVersion)
416432
val runnerVersion0 =
417-
if scalaVersion.startsWith("3") &&
418-
scalaVersion.coursierVersion < s"$scala3LtsPrefix.0".coursierVersion
419-
then {
433+
if shouldUseLegacyRunners then {
420434
logger.message(
421435
s"""$warnPrefix Scala $scalaVersion is no longer supported by the runner module.
422436
|$warnPrefix Defaulting to a legacy runner module version: $runnerLegacyVersion.

0 commit comments

Comments
 (0)