Skip to content

Commit 44649d2

Browse files
authored
Merge pull request #3589 from Gedochao/maintenance/cross-tests-and-fixes
Add tests for the current behaviour of `--cross`
2 parents 3a316a7 + 75a06ba commit 44649d2

File tree

7 files changed

+223
-41
lines changed

7 files changed

+223
-41
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
package scala.cli.integration
22

33
class CompileTestsDefault extends CompileTestDefinitions with CompileTests3StableDefinitions
4-
with TestDefault
4+
with TestDefault {
5+
test(
6+
s"compile --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
7+
) {
8+
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
9+
simpleInputs
10+
.add(os.rel / "project.scala" -> s"//> using scala ${crossVersions.mkString(" ")}")
11+
.fromRoot { root =>
12+
os.proc(TestUtil.cli, "compile", ".", "--cross", "--power", extraOptions).call(cwd = root)
13+
}
14+
}
15+
}

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

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,39 +1354,84 @@ abstract class PackageTestDefinitions extends ScalaCliSuite with TestScalaVersio
13541354
)
13551355
else Nil)
13561356
packageDescription = packageOpts.headOption.getOrElse("bootstrap")
1357-
}
1357+
} {
13581358
test(s"package with main method in test scope ($packageDescription)") {
1359-
val mainClass = "TestScopeMain"
1360-
val testScopeFileName = s"$mainClass.test.scala"
1361-
val message = "Hello"
1362-
val outputFile = mainClass + extension
1363-
TestInputs(
1364-
os.rel / "Messages.scala" -> s"""object Messages { val msg = "$message" }""",
1365-
os.rel / testScopeFileName -> s"""object $mainClass extends App { println(Messages.msg) }"""
1366-
).fromRoot { root =>
1367-
os.proc(
1368-
TestUtil.cli,
1369-
"--power",
1370-
"package",
1371-
"--test",
1372-
extraOptions,
1373-
".",
1374-
packageOpts
1375-
)
1376-
.call(cwd = root)
1377-
val outputFilePath = root / outputFile
1378-
expect(os.isFile(outputFilePath))
1379-
val output =
1380-
if (packageDescription == libraryArg)
1381-
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
1382-
else if (packageDescription == jsArg)
1383-
os.proc(node, outputFilePath).call(cwd = root).out.trim()
1384-
else {
1385-
expect(Files.isExecutable(outputFilePath.toNIO))
1386-
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
1387-
}
1388-
expect(output == message)
1359+
TestUtil.retryOnCi() {
1360+
val mainClass = "TestScopeMain"
1361+
val testScopeFileName = s"$mainClass.test.scala"
1362+
val message = "Hello"
1363+
val outputFile = mainClass + extension
1364+
TestInputs(
1365+
os.rel / "Messages.scala" -> s"""object Messages { val msg = "$message" }""",
1366+
os.rel / testScopeFileName -> s"""object $mainClass extends App { println(Messages.msg) }"""
1367+
).fromRoot { root =>
1368+
os.proc(
1369+
TestUtil.cli,
1370+
"--power",
1371+
"package",
1372+
"--test",
1373+
extraOptions,
1374+
".",
1375+
packageOpts
1376+
)
1377+
.call(cwd = root)
1378+
val outputFilePath = root / outputFile
1379+
expect(os.isFile(outputFilePath))
1380+
val output =
1381+
if (packageDescription == libraryArg)
1382+
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
1383+
else if (packageDescription == jsArg)
1384+
os.proc(node, outputFilePath).call(cwd = root).out.trim()
1385+
else {
1386+
expect(Files.isExecutable(outputFilePath.toNIO))
1387+
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
1388+
}
1389+
expect(output == message)
1390+
}
13891391
}
13901392
}
1393+
1394+
if (actualScalaVersion == Constants.scala3Next)
1395+
test(s"package ($packageDescription, --cross)") {
1396+
TestUtil.retryOnCi() {
1397+
val crossDirective =
1398+
s"//> using scala $actualScalaVersion ${Constants.scala213} ${Constants.scala212}"
1399+
val mainClass = "TestScopeMain"
1400+
val mainFile = s"$mainClass.scala"
1401+
val message = "Hello"
1402+
val outputFile = mainClass + extension
1403+
TestInputs(
1404+
os.rel / "Messages.scala" ->
1405+
s"""$crossDirective
1406+
|object Messages { val msg = "$message" }""".stripMargin,
1407+
os.rel / mainFile ->
1408+
s"""object $mainClass extends App { println(Messages.msg) }""".stripMargin
1409+
).fromRoot { root =>
1410+
os.proc(
1411+
TestUtil.cli,
1412+
"--power",
1413+
"package",
1414+
"--cross",
1415+
extraOptions,
1416+
".",
1417+
packageOpts
1418+
)
1419+
.call(cwd = root)
1420+
val outputFilePath = root / outputFile
1421+
expect(os.isFile(outputFilePath))
1422+
val output =
1423+
if (packageDescription == libraryArg)
1424+
os.proc(TestUtil.cli, "run", outputFilePath).call(cwd = root).out.trim()
1425+
else if (packageDescription == jsArg)
1426+
os.proc(node, outputFilePath).call(cwd = root).out.trim()
1427+
else {
1428+
expect(Files.isExecutable(outputFilePath.toNIO))
1429+
TestUtil.maybeUseBash(outputFilePath)(cwd = root).out.trim()
1430+
}
1431+
expect(output == message)
1432+
}
1433+
}
1434+
}
1435+
}
13911436
}
13921437
}

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV
1414

1515
def testPublishVersion: String = "1.5.6"
1616

17-
private object PublishTestInputs {
17+
protected object PublishTestInputs {
1818
def testOrg: String = "test-local-org.sth"
1919
def testName: String = "my-proj"
20-
def projFile(message: String, exclude: Boolean = false): String =
21-
s"""//> using scala $testedPublishedScalaVersion
20+
def projFile(
21+
message: String,
22+
exclude: Boolean = false,
23+
useTestScope: Boolean = false,
24+
crossVersions: Option[Seq[String]] = None
25+
): String =
26+
s"""//> using scala ${crossVersions.map(
27+
_.mkString(" ")
28+
).getOrElse(testedPublishedScalaVersion)}
29+
|${if (useTestScope) "//> using target.scope test" else ""}
2230
|//> using dep com.lihaoyi::os-lib:0.11.3${Some(",exclude=com.lihaoyi%%geny").filter(_ =>
2331
exclude
2432
).getOrElse("")}
@@ -43,14 +51,18 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV
4351
|""".stripMargin
4452
}
4553

54+
lazy val projectFilePath: os.RelPath = os.rel / "src" / "project.scala"
55+
lazy val projectConfPath: os.RelPath = os.rel / "src" / "publish-conf.scala"
4656
def inputs(
4757
message: String = "Hello",
4858
includePublishVersion: Boolean = true,
49-
excludeGeny: Boolean = false
59+
excludeGeny: Boolean = false,
60+
useTestScope: Boolean = false,
61+
crossVersions: Option[Seq[String]] = None
5062
): TestInputs =
5163
TestInputs(
52-
os.rel / "project.scala" -> projFile(message, excludeGeny),
53-
os.rel / "publish-conf.scala" -> publishConfFile(includePublishVersion)
64+
projectFilePath -> projFile(message, excludeGeny, useTestScope, crossVersions),
65+
projectConfPath -> publishConfFile(includePublishVersion)
5466
)
5567
}
5668

@@ -146,7 +158,7 @@ abstract class PublishLocalTestDefinitions extends ScalaCliSuite with TestScalaV
146158
val output1 = output()
147159
expect(output1 == "Hello")
148160

149-
os.write.over(root / "project.scala", PublishTestInputs.projFile("olleH"))
161+
os.write.over(root / PublishTestInputs.projectFilePath, PublishTestInputs.projFile("olleH"))
150162
publishLocal()
151163
val output2 = output()
152164
expect(output2 == "olleH")
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
11
package scala.cli.integration
22

3-
class PublishLocalTestsDefault extends PublishLocalTestDefinitions with TestDefault
3+
import com.eed3si9n.expecty.Expecty.expect
4+
5+
class PublishLocalTestsDefault extends PublishLocalTestDefinitions with TestDefault {
6+
test(
7+
s"publish local --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
8+
) {
9+
val expectedMessage = "Hello"
10+
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
11+
PublishTestInputs.inputs(message = expectedMessage, crossVersions = Some(crossVersions))
12+
.fromRoot { root =>
13+
os.proc(
14+
TestUtil.cli,
15+
"--power",
16+
"publish",
17+
"local",
18+
".",
19+
extraOptions,
20+
"--cross"
21+
)
22+
.call(cwd = root)
23+
def publishedDep(scalaVersionSuffix: String) =
24+
s"${PublishTestInputs.testOrg}:${PublishTestInputs.testName}_$scalaVersionSuffix:$testPublishVersion"
25+
val r3 =
26+
os.proc(TestUtil.cli, "run", "--dep", publishedDep("3"), extraOptions).call(cwd = root)
27+
expect(r3.out.trim() == expectedMessage)
28+
val r213 = os.proc(
29+
TestUtil.cli,
30+
"run",
31+
"--dep",
32+
publishedDep("2.13"),
33+
"-S",
34+
Constants.scala213,
35+
extraOptions
36+
).call(cwd = root)
37+
expect(r213.out.trim() == expectedMessage)
38+
val r212 = os.proc(
39+
TestUtil.cli,
40+
"run",
41+
"--dep",
42+
publishedDep("2.12"),
43+
"-S",
44+
Constants.scala212,
45+
extraOptions
46+
).call(cwd = root)
47+
expect(r212.out.trim() == expectedMessage)
48+
}
49+
}
50+
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,42 @@ class RunTestsDefault extends RunTestDefinitions
149149
}
150150
}
151151
}
152+
153+
for {
154+
useTestScope <- Seq(true, false)
155+
fileName = if (useTestScope) "example.test.scala" else "example.scala"
156+
scopeOptions = if (useTestScope) Seq("--test") else Seq.empty
157+
scopeDesc = if (useTestScope) "test" else "main"
158+
expectedMessage = "Hello, World!"
159+
platformOptions <- Seq(Seq("--native"), Seq("--js"), Nil)
160+
platformDesc = platformOptions.headOption.getOrElse("JVM").stripPrefix("--")
161+
}
162+
test(
163+
s"run --cross $platformDesc $actualScalaVersion, ${Constants.scala213} and ${Constants.scala212} ($scopeDesc scope)"
164+
) {
165+
TestUtil.retryOnCi() {
166+
TestInputs {
167+
os.rel / fileName ->
168+
s"""//> using scala $actualScalaVersion ${Constants.scala213} ${Constants.scala212}
169+
|object Main extends App {
170+
| println("$expectedMessage")
171+
|}
172+
|""".stripMargin
173+
}.fromRoot { root =>
174+
val r =
175+
os.proc(
176+
TestUtil.cli,
177+
"run",
178+
".",
179+
"--cross",
180+
"--power",
181+
extraOptions,
182+
scopeOptions,
183+
platformOptions
184+
)
185+
.call(cwd = root)
186+
expect(r.out.trim() == expectedMessage)
187+
}
188+
}
189+
}
152190
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class TestTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
1212
if (actualScalaVersion.startsWith("3.")) Seq("--jvm", "11")
1313
else Nil
1414
protected lazy val baseExtraOptions: Seq[String] = TestUtil.extraOptions ++ jvmOptions
15-
private lazy val extraOptions: Seq[String] = scalaVersionArgs ++ baseExtraOptions
15+
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ baseExtraOptions
1616

1717
private val utestVersion = "0.8.3"
1818

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.eed3si9n.expecty.Expecty.expect
44

55
import java.io.File
66

7+
import scala.cli.integration.Constants.munitVersion
8+
79
class TestTestsDefault extends TestTestDefinitions with TestDefault {
810
test("Pure Java with Scala tests") {
911
val inputs = TestInputs(
@@ -40,4 +42,31 @@ class TestTestsDefault extends TestTestDefinitions with TestDefault {
4042
.call(cwd = root, stdout = os.Inherit)
4143
}
4244
}
45+
46+
test(
47+
s"successful test --cross $actualScalaVersion with ${Constants.scala213} and ${Constants.scala212}"
48+
) {
49+
val crossVersions = Seq(actualScalaVersion, Constants.scala213, Constants.scala212)
50+
val expectedMessage = "Hello"
51+
TestInputs(
52+
os.rel / "Cross.test.scala" ->
53+
s"""//> using dep org.scalameta::munit::$munitVersion
54+
|class MyTests extends munit.FunSuite {
55+
| test("foo") {
56+
| assert(2 + 2 == 4)
57+
| println("$expectedMessage")
58+
| }
59+
|}
60+
|""".stripMargin,
61+
os.rel / "project.scala" -> s"//> using scala ${crossVersions.mkString(" ")}"
62+
).fromRoot { root =>
63+
val output = os.proc(TestUtil.cli, "test", extraOptions, ".", "--cross", "--power")
64+
.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)
68+
expect(output.contains(expectedMessage))
69+
expect(countOccurrences(output, expectedMessage) == crossVersions.length)
70+
}
71+
}
4372
}

0 commit comments

Comments
 (0)