Skip to content

Add tests for the current behaviour of --cross #3589

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

Merged
merged 6 commits into from
Mar 26, 2025
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package scala.cli.integration

class CompileTestsDefault extends CompileTestDefinitions with CompileTests3StableDefinitions
with TestDefault
with TestDefault {
test(
s"compile --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
) {
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
simpleInputs
.add(os.rel / "project.scala" -> s"//> using scala ${crossVersions.mkString(" ")}")
.fromRoot { root =>
os.proc(TestUtil.cli, "compile", ".", "--cross", "--power", extraOptions).call(cwd = root)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1354,39 +1354,84 @@ abstract class PackageTestDefinitions extends ScalaCliSuite with TestScalaVersio
)
else Nil)
packageDescription = packageOpts.headOption.getOrElse("bootstrap")
}
} {
test(s"package with main method in test scope ($packageDescription)") {
val mainClass = "TestScopeMain"
val testScopeFileName = s"$mainClass.test.scala"
val message = "Hello"
val outputFile = mainClass + extension
TestInputs(
os.rel / "Messages.scala" -> s"""object Messages { val msg = "$message" }""",
os.rel / testScopeFileName -> s"""object $mainClass extends App { println(Messages.msg) }"""
).fromRoot { root =>
os.proc(
TestUtil.cli,
"--power",
"package",
"--test",
extraOptions,
".",
packageOpts
)
.call(cwd = root)
val outputFilePath = root / outputFile
expect(os.isFile(outputFilePath))
val output =
if (packageDescription == libraryArg)
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
else if (packageDescription == jsArg)
os.proc(node, outputFilePath).call(cwd = root).out.trim()
else {
expect(Files.isExecutable(outputFilePath.toNIO))
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
}
expect(output == message)
TestUtil.retryOnCi() {
val mainClass = "TestScopeMain"
val testScopeFileName = s"$mainClass.test.scala"
val message = "Hello"
val outputFile = mainClass + extension
TestInputs(
os.rel / "Messages.scala" -> s"""object Messages { val msg = "$message" }""",
os.rel / testScopeFileName -> s"""object $mainClass extends App { println(Messages.msg) }"""
).fromRoot { root =>
os.proc(
TestUtil.cli,
"--power",
"package",
"--test",
extraOptions,
".",
packageOpts
)
.call(cwd = root)
val outputFilePath = root / outputFile
expect(os.isFile(outputFilePath))
val output =
if (packageDescription == libraryArg)
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
else if (packageDescription == jsArg)
os.proc(node, outputFilePath).call(cwd = root).out.trim()
else {
expect(Files.isExecutable(outputFilePath.toNIO))
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
}
expect(output == message)
}
}
}

if (actualScalaVersion == Constants.scala3Next)
test(s"package ($packageDescription, --cross)") {
TestUtil.retryOnCi() {
val crossDirective =
s"//> using scala $actualScalaVersion ${Constants.scala213} ${Constants.scala212}"
val mainClass = "TestScopeMain"
val mainFile = s"$mainClass.scala"
val message = "Hello"
val outputFile = mainClass + extension
TestInputs(
os.rel / "Messages.scala" ->
s"""$crossDirective
|object Messages { val msg = "$message" }""".stripMargin,
os.rel / mainFile ->
s"""object $mainClass extends App { println(Messages.msg) }""".stripMargin
).fromRoot { root =>
os.proc(
TestUtil.cli,
"--power",
"package",
"--cross",
extraOptions,
".",
packageOpts
)
.call(cwd = root)
val outputFilePath = root / outputFile
expect(os.isFile(outputFilePath))
val output =
if (packageDescription == libraryArg)
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
else if (packageDescription == jsArg)
os.proc(node, outputFilePath).call(cwd = root).out.trim()
else {
expect(Files.isExecutable(outputFilePath.toNIO))
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
}
expect(output == message)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV

def testPublishVersion: String = "1.5.6"

private object PublishTestInputs {
protected object PublishTestInputs {
def testOrg: String = "test-local-org.sth"
def testName: String = "my-proj"
def projFile(message: String, exclude: Boolean = false): String =
s"""//> using scala $testedPublishedScalaVersion
def projFile(
message: String,
exclude: Boolean = false,
useTestScope: Boolean = false,
crossVersions: Option[Seq[String]] = None
): String =
s"""//> using scala ${crossVersions.map(
_.mkString(" ")
).getOrElse(testedPublishedScalaVersion)}
|${if (useTestScope) "//> using target.scope test" else ""}
|//> using dep com.lihaoyi::os-lib:0.11.3${Some(",exclude=com.lihaoyi%%geny").filter(_ =>
exclude
).getOrElse("")}
Expand All @@ -43,14 +51,18 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV
|""".stripMargin
}

lazy val projectFilePath: os.RelPath = os.rel / "src" / "project.scala"
lazy val projectConfPath: os.RelPath = os.rel / "src" / "publish-conf.scala"
def inputs(
message: String = "Hello",
includePublishVersion: Boolean = true,
excludeGeny: Boolean = false
excludeGeny: Boolean = false,
useTestScope: Boolean = false,
crossVersions: Option[Seq[String]] = None
): TestInputs =
TestInputs(
os.rel / "project.scala" -> projFile(message, excludeGeny),
os.rel / "publish-conf.scala" -> publishConfFile(includePublishVersion)
projectFilePath -> projFile(message, excludeGeny, useTestScope, crossVersions),
projectConfPath -> publishConfFile(includePublishVersion)
)
}

Expand Down Expand Up @@ -146,7 +158,7 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV
val output1 = output()
expect(output1 == "Hello")

os.write.over(root / "project.scala", PublishTestInputs.projFile("olleH"))
os.write.over(root / PublishTestInputs.projectFilePath, PublishTestInputs.projFile("olleH"))
publishLocal()
val output2 = output()
expect(output2 == "olleH")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
package scala.cli.integration

class PublishLocalTestsDefault extends PublishLocalTestDefinitions with TestDefault
import com.eed3si9n.expecty.Expecty.expect

class PublishLocalTestsDefault extends PublishLocalTestDefinitions with TestDefault {
test(
s"publish local --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
) {
val expectedMessage = "Hello"
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
PublishTestInputs.inputs(message = expectedMessage, crossVersions = Some(crossVersions))
.fromRoot { root =>
os.proc(
TestUtil.cli,
"--power",
"publish",
"local",
".",
extraOptions,
"--cross"
)
.call(cwd = root)
def publishedDep(scalaVersionSuffix: String) =
s"${PublishTestInputs.testOrg}:${PublishTestInputs.testName}_$scalaVersionSuffix:$testPublishVersion"
val r3 =
os.proc(TestUtil.cli, "run", "--dep", publishedDep("3"), extraOptions).call(cwd = root)
expect(r3.out.trim() == expectedMessage)
val r213 = os.proc(
TestUtil.cli,
"run",
"--dep",
publishedDep("2.13"),
"-S",
Constants.scala213,
extraOptions
).call(cwd = root)
expect(r213.out.trim() == expectedMessage)
val r212 = os.proc(
TestUtil.cli,
"run",
"--dep",
publishedDep("2.12"),
"-S",
Constants.scala212,
extraOptions
).call(cwd = root)
expect(r212.out.trim() == expectedMessage)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,42 @@ class RunTestsDefault extends RunTestDefinitions
}
}
}

for {
useTestScope <- Seq(true, false)
fileName = if (useTestScope) "example.test.scala" else "example.scala"
scopeOptions = if (useTestScope) Seq("--test") else Seq.empty
scopeDesc = if (useTestScope) "test" else "main"
expectedMessage = "Hello, World!"
platformOptions <- Seq(Seq("--native"), Seq("--js"), Nil)
platformDesc = platformOptions.headOption.getOrElse("JVM").stripPrefix("--")
}
test(
s"run --cross $platformDesc $actualScalaVersion, ${Constants.scala213} and ${Constants.scala212} ($scopeDesc scope)"
) {
TestUtil.retryOnCi() {
TestInputs {
os.rel / fileName ->
s"""//> using scala $actualScalaVersion ${Constants.scala213} ${Constants.scala212}
|object Main extends App {
| println("$expectedMessage")
|}
|""".stripMargin
}.fromRoot { root =>
val r =
os.proc(
TestUtil.cli,
"run",
".",
"--cross",
"--power",
extraOptions,
scopeOptions,
platformOptions
)
.call(cwd = root)
expect(r.out.trim() == expectedMessage)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class TestTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
if (actualScalaVersion.startsWith("3.")) Seq("--jvm", "11")
else Nil
protected lazy val baseExtraOptions: Seq[String] = TestUtil.extraOptions ++ jvmOptions
private lazy val extraOptions: Seq[String] = scalaVersionArgs ++ baseExtraOptions
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ baseExtraOptions

private val utestVersion = "0.8.3"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.eed3si9n.expecty.Expecty.expect

import java.io.File

import scala.cli.integration.Constants.munitVersion

class TestTestsDefault extends TestTestDefinitions with TestDefault {
test("Pure Java with Scala tests") {
val inputs = TestInputs(
Expand Down Expand Up @@ -40,4 +42,31 @@ class TestTestsDefault extends TestTestDefinitions with TestDefault {
.call(cwd = root, stdout = os.Inherit)
}
}

test(
s"successful test --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
) {
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
val expectedMessage = "Hello"
TestInputs(
os.rel / "Cross.test.scala" ->
s"""//> using dep org.scalameta::munit::$munitVersion
|class MyTests extends munit.FunSuite {
| test("foo") {
| assert(2 + 2 == 4)
| println("$expectedMessage")
| }
|}
|""".stripMargin,
os.rel / "project.scala" -> s"//> using scala ${crossVersions.mkString(" ")}"
).fromRoot { root =>
val output = os.proc(TestUtil.cli, "test", extraOptions, ".", "--cross", "--power")
.call(cwd = root).out.text()
def countOccurrences(a: String, b: String): Int =
if (b.isEmpty) 0 // Avoid infinite splitting
else a.sliding(b.length).count(_ == b)
expect(output.contains(expectedMessage))
expect(countOccurrences(output, expectedMessage) == crossVersions.length)
}
}
}