Skip to content

Commit 623ff3f

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 9dbd7ff commit 623ff3f

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.eed3si9n.expecty.Expecty.expect
55
import java.io.File
66

77
import scala.cli.integration.Constants.munitVersion
8+
import scala.cli.integration.TestUtil.StringOps
89

910
class TestTestsDefault extends TestTestDefinitions with TestDefault {
1011
test("Pure Java with Scala tests") {
@@ -62,11 +63,39 @@ class TestTestsDefault extends TestTestDefinitions with TestDefault {
6263
).fromRoot { root =>
6364
val output = os.proc(TestUtil.cli, "test", extraOptions, ".", "--cross", "--power")
6465
.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)
6866
expect(output.contains(expectedMessage))
69-
expect(countOccurrences(output, expectedMessage) == crossVersions.length)
67+
expect(output.countOccurrences(expectedMessage) == crossVersions.length)
7068
}
7169
}
70+
71+
for {
72+
scalaVersion <- TestUtil.legacyScalaVersionsOnePerMinor
73+
expectedMessage = "Hello, world!"
74+
expectedWarning =
75+
s"Defaulting to a legacy test-runner module version: ${Constants.runnerLegacyVersion}"
76+
}
77+
test(s"run a simple test with Scala $scalaVersion (legacy)") {
78+
TestInputs(os.rel / "example.test.scala" ->
79+
// using JUnit to work around TASTy and macro incompatibilities
80+
s"""//> using dep com.novocode:junit-interface:0.11
81+
|import org.junit.Test
82+
|
83+
|class MyTests {
84+
| @Test
85+
| def foo(): Unit = {
86+
| assert(2 + 2 == 4)
87+
| println("$expectedMessage")
88+
| }
89+
|}
90+
|""".stripMargin).fromRoot { root =>
91+
val res =
92+
os.proc(TestUtil.cli, "test", ".", "-S", scalaVersion, TestUtil.extraOptions)
93+
.call(cwd = root, stderr = os.Pipe)
94+
val out = res.out.trim()
95+
expect(out.contains(expectedMessage))
96+
val err = res.err.trim()
97+
expect(err.contains(expectedWarning))
98+
expect(err.countOccurrences(expectedWarning) == 1)
99+
}
100+
}
72101
}

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)