Skip to content

Commit d5427b0

Browse files
committed
Respect default Scala version override in project exports
1 parent 9d09b78 commit d5427b0

File tree

8 files changed

+143
-48
lines changed

8 files changed

+143
-48
lines changed

modules/cli/src/main/scala/scala/cli/exportCmd/MillProjectDescriptor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import scala.build.internal.Runner.frameworkName
1313
import scala.build.options.{BuildOptions, Platform, ScalaJsOptions, ScalaNativeOptions, Scope}
1414
import scala.build.testrunner.AsmTestRunner
1515
import scala.build.{Logger, Sources}
16+
import scala.cli.ScalaCli
1617
import scala.cli.util.SeqHelpers.*
1718

1819
final case class MillProjectDescriptor(
@@ -39,7 +40,7 @@ final case class MillProjectDescriptor(
3940

4041
val sv = options.scalaOptions.scalaVersion
4142
.flatMap(_.versionOpt) // FIXME If versionOpt is empty, the project is pure Java
42-
.getOrElse(Constants.defaultScalaVersion)
43+
.getOrElse(ScalaCli.getDefaultScalaVersion)
4344

4445
if (pureJava)
4546
MillProject()

modules/cli/src/main/scala/scala/cli/exportCmd/SbtProjectDescriptor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import scala.build.options.{
2121
}
2222
import scala.build.testrunner.AsmTestRunner
2323
import scala.build.{Logger, Positioned, Sources}
24+
import scala.cli.ScalaCli
2425

2526
final case class SbtProjectDescriptor(
2627
sbtVersion: String,
@@ -122,7 +123,7 @@ final case class SbtProjectDescriptor(
122123
val scalaVerSetting = {
123124
val sv = options.scalaOptions.scalaVersion
124125
.flatMap(_.versionOpt) // FIXME If versionOpt is empty, the project is pure Java
125-
.getOrElse(Constants.defaultScalaVersion)
126+
.getOrElse(ScalaCli.getDefaultScalaVersion)
126127
s"""scalaVersion := "$sv""""
127128
}
128129

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
package scala.cli.integration
22

33
import com.eed3si9n.expecty.Expecty.expect
4+
import os.RelPath
45

56
import java.nio.charset.Charset
67

7-
import scala.util.Properties
8-
98
abstract class ExportMillTestDefinitions extends ScalaCliSuite
109
with TestScalaVersionArgs
11-
with ExportCommonTestDefinitions { _: TestScalaVersion =>
12-
protected def launcher: os.RelPath =
13-
if (Properties.isWin) os.rel / "mill.bat"
14-
else os.rel / "mill"
15-
16-
implicit class MillTestInputs(inputs: TestInputs) {
17-
def withMillJvmOpts: TestInputs = inputs.add(
18-
os.rel / ".mill-jvm-opts" ->
19-
"""-Xmx512m
20-
|-Xms128m
21-
|""".stripMargin
22-
)
23-
}
24-
10+
with ExportCommonTestDefinitions
11+
with MillTestHelper { _: TestScalaVersion =>
2512
override val prepareTestInputs: TestInputs => TestInputs = _.withMillJvmOpts
2613

14+
override val outputDir: RelPath = millOutputDir
2715
override def exportCommand(args: String*): os.proc =
2816
os.proc(
2917
TestUtil.cli,
@@ -37,18 +25,17 @@ abstract class ExportMillTestDefinitions extends ScalaCliSuite
3725
)
3826

3927
override def buildToolCommand(root: os.Path, args: String*): os.proc =
40-
os.proc(root / outputDir / launcher, args)
28+
millCommand(root, args*)
4129

42-
protected val defaultProjectName = "project"
43-
override val runMainArgs: Seq[String] = Seq(s"$defaultProjectName.run")
30+
override val runMainArgs: Seq[String] = Seq(s"$millDefaultProjectName.run")
4431

45-
override val runTestsArgs: Seq[String] = Seq(s"$defaultProjectName.test")
32+
override val runTestsArgs: Seq[String] = Seq(s"$millDefaultProjectName.test")
4633

4734
def jvmTestScalacOptions(): Unit =
4835
ExportTestProjects.jvmTest(actualScalaVersion).withMillJvmOpts.fromRoot { root =>
4936
exportCommand(".").call(cwd = root, stdout = os.Inherit)
5037
val res =
51-
buildToolCommand(root, "--disable-ticker", "show", s"$defaultProjectName.scalacOptions")
38+
buildToolCommand(root, "--disable-ticker", "show", s"$millDefaultProjectName.scalacOptions")
5239
.call(cwd = root / outputDir)
5340
val output = res.out.text(Charset.defaultCharset())
5441
expect(output.filterNot(_.isWhitespace) == "[\"-deprecation\"]")
@@ -64,7 +51,7 @@ abstract class ExportMillTestDefinitions extends ScalaCliSuite
6451
root,
6552
"--disable-ticker",
6653
"show",
67-
s"$defaultProjectName.scalacPluginIvyDeps"
54+
s"$millDefaultProjectName.scalacPluginIvyDeps"
6855
)
6956
.call(cwd = root / outputDir)
7057
val output = res.out.text(Charset.defaultCharset())
@@ -73,7 +60,8 @@ abstract class ExportMillTestDefinitions extends ScalaCliSuite
7360
}
7461
locally {
7562
// test
76-
val res = buildToolCommand(root, s"$defaultProjectName.test").call(cwd = root / outputDir)
63+
val res =
64+
buildToolCommand(root, s"$millDefaultProjectName.test").call(cwd = root / outputDir)
7765
val output = res.out.text(Charset.defaultCharset())
7866
expect(output.contains("1 succeeded"))
7967
}

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
package scala.cli.integration
22

33
abstract class ExportSbtTestDefinitions extends ScalaCliSuite
4-
with TestScalaVersionArgs with ExportCommonTestDefinitions { _: TestScalaVersion =>
5-
private lazy val sbtLaunchJar = {
6-
val res =
7-
os.proc(TestUtil.cs, "fetch", "--intransitive", "org.scala-sbt:sbt-launch:1.5.5").call()
8-
val rawPath = res.out.trim()
9-
val path = os.Path(rawPath, os.pwd)
10-
if (os.isFile(path)) path
11-
else sys.error(s"Something went wrong (invalid sbt launch JAR path '$rawPath')")
12-
}
13-
14-
protected lazy val sbt: os.Shellable =
15-
Seq[os.Shellable](
16-
"java",
17-
"-Xmx512m",
18-
"-Xms128m",
19-
"-Djline.terminal=jline.UnsupportedTerminal",
20-
"-Dsbt.log.noformat=true",
21-
"-jar",
22-
sbtLaunchJar
23-
)
24-
4+
with TestScalaVersionArgs with ExportCommonTestDefinitions with SbtTestHelper {
5+
_: TestScalaVersion =>
256
override def exportCommand(args: String*): os.proc =
267
os.proc(
278
TestUtil.cli,
@@ -34,7 +15,7 @@ abstract class ExportSbtTestDefinitions extends ScalaCliSuite
3415
args
3516
)
3617

37-
override def buildToolCommand(root: os.Path, args: String*): os.proc = os.proc(sbt, args)
18+
override def buildToolCommand(root: os.Path, args: String*): os.proc = sbtCommand(args*)
3819

3920
override val runMainArgs: Seq[String] = Seq("run")
4021
override val runTestsArgs: Seq[String] = Seq("test")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package scala.cli.integration
2+
3+
import scala.util.Properties
4+
5+
trait MillTestHelper {
6+
7+
protected def millLauncher: os.RelPath =
8+
if (Properties.isWin) os.rel / "mill.bat"
9+
else os.rel / "mill"
10+
11+
protected val millJvmOptsFileName: String = ".mill-jvm-opts"
12+
protected val millJvmOptsContent: String = """-Xmx512m
13+
|-Xms128m
14+
|""".stripMargin
15+
16+
protected val millDefaultProjectName = "project"
17+
18+
implicit class MillTestInputs(inputs: TestInputs) {
19+
def withMillJvmOpts: TestInputs = inputs.add(os.rel / millJvmOptsFileName -> millJvmOptsContent)
20+
}
21+
22+
protected val millOutputDir: os.RelPath = os.rel / "output-project"
23+
24+
protected def millCommand(root: os.Path, args: String*): os.proc =
25+
os.proc(root / millOutputDir / millLauncher, args)
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.cli.integration
2+
3+
trait SbtTestHelper {
4+
protected lazy val sbtLaunchJar: os.Path = {
5+
val res =
6+
os.proc(TestUtil.cs, "fetch", "--intransitive", "org.scala-sbt:sbt-launch:1.5.5").call()
7+
val rawPath = res.out.trim()
8+
val path = os.Path(rawPath, os.pwd)
9+
if (os.isFile(path)) path
10+
else sys.error(s"Something went wrong (invalid sbt launch JAR path '$rawPath')")
11+
}
12+
13+
protected lazy val sbt: os.Shellable =
14+
Seq[os.Shellable](
15+
"java",
16+
"-Xmx512m",
17+
"-Xms128m",
18+
"-Djline.terminal=jline.UnsupportedTerminal",
19+
"-Dsbt.log.noformat=true",
20+
"-jar",
21+
sbtLaunchJar
22+
)
23+
24+
protected def sbtCommand(args: String*): os.proc = os.proc(sbt, args)
25+
}

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import os.CommandResult
55

66
import scala.util.Properties
77

8-
class SipScalaTests extends ScalaCliSuite {
8+
class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper {
99
implicit class StringEnrichment(s: String) {
1010
def containsExperimentalWarningOf(featureNameAndType: String): Boolean =
1111
s.contains(s"The $featureNameAndType is experimental") ||
@@ -635,4 +635,76 @@ class SipScalaTests extends ScalaCliSuite {
635635
expect(r.out.trim() == sv)
636636
}
637637
}
638+
639+
test(s"default Scala version override launcher option is respected by the SBT export") {
640+
val input = "printVersion.sc"
641+
val code = """println(s"Default version: ${scala.util.Properties.versionNumberString}")"""
642+
val outputDir = "sbt-project"
643+
TestInputs(os.rel / input -> code).fromRoot { root =>
644+
val defaultSv = Constants.scala213
645+
val expectedMessage = s"Default version: $defaultSv"
646+
val launcherOpt = "--cli-default-scala-version"
647+
val exportRes = os.proc(
648+
TestUtil.cli,
649+
launcherOpt,
650+
defaultSv,
651+
"export",
652+
input,
653+
"--sbt",
654+
"--power",
655+
"-o",
656+
outputDir
657+
).call(cwd = root)
658+
expect(exportRes.exitCode == 0)
659+
val sbtRes = sbtCommand("run").call(cwd = root / outputDir)
660+
val output = sbtRes.out.trim()
661+
expect(output.contains(expectedMessage))
662+
}
663+
}
664+
665+
test(s"default Scala version override launcher option is respected by the Mill export") {
666+
val input = "printVersion.sc"
667+
val code = """println(s"Default version: ${scala.util.Properties.versionNumberString}")"""
668+
val outputDir = millOutputDir
669+
TestInputs(os.rel / input -> code).fromRoot { root =>
670+
val defaultSv = Constants.scala213
671+
val expectedMessage = s"Default version: $defaultSv"
672+
val launcherOpt = "--cli-default-scala-version"
673+
val exportRes = os.proc(
674+
TestUtil.cli,
675+
launcherOpt,
676+
defaultSv,
677+
"export",
678+
input,
679+
"--mill",
680+
"--power",
681+
"-o",
682+
outputDir
683+
).call(cwd = root)
684+
expect(exportRes.exitCode == 0)
685+
val millRes = millCommand(root, s"$millDefaultProjectName.run").call(cwd = root / outputDir)
686+
val output = millRes.out.trim()
687+
expect(output.contains(expectedMessage))
688+
}
689+
}
690+
691+
test(s"default Scala version override launcher option is respected by the json export") {
692+
val input = "printVersion.sc"
693+
val code = """println(s"Default version: ${scala.util.Properties.versionNumberString}")"""
694+
TestInputs(os.rel / input -> code).fromRoot { root =>
695+
val defaultSv = Constants.scala213
696+
val launcherOpt = "--cli-default-scala-version"
697+
val exportRes = os.proc(
698+
TestUtil.cli,
699+
launcherOpt,
700+
defaultSv,
701+
"export",
702+
input,
703+
"--json",
704+
"--power"
705+
).call(cwd = root)
706+
expect(exportRes.exitCode == 0)
707+
expect(exportRes.out.trim().contains(s""""scalaVersion": "$defaultSv""""))
708+
}
709+
}
638710
}

modules/options/src/main/scala/scala/build/info/BuildInfo.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ object BuildInfo {
128128
private def scalaVersionSettings(options: BuildOptions): BuildInfo = {
129129
val sv = options.scalaParams.toOption.flatten
130130
.map(_.scalaVersion)
131+
.orElse(options.scalaOptions.defaultScalaVersion)
131132
.getOrElse(Constants.defaultScalaVersion)
132133

133134
BuildInfo(scalaVersion = Some(sv))

0 commit comments

Comments
 (0)