Skip to content

Commit 04af902

Browse files
committed
Handle save scala option, move coursier tests to isolated configuration, apply requested changes
1 parent 216b70b commit 04af902

File tree

10 files changed

+92
-233
lines changed

10 files changed

+92
-233
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ jobs:
111111

112112
- name: Cmd Tests
113113
run: |
114-
./project/scripts/sbt ";scala3-bootstrapped/compile; scala3-bootstrapped/publishLocal; scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;configureIDE ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test"
114+
./project/scripts/sbt ";scala3-bootstrapped/compile; scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;configureIDE ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test; scala3-compiler-bootstrapped/scala3CompilerCoursierTest:test"
115115
./project/scripts/bootstrapCmdTests
116116
117117
- name: MiMa

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ community-build/dotty-community-build-deps
9999

100100
# Coursier
101101
cs
102+
103+
# Coursier test product
104+
compiler/test-coursier/run/myfile.jar

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.net.URLClassLoader
88
import sys.process._
99
import java.io.File
1010
import java.lang.Thread
11+
import scala.annotation.internal.sharable
1112

1213
enum ExecuteMode:
1314
case Guess
@@ -20,10 +21,12 @@ case class Settings(
2021
classPath: List[String] = List.empty,
2122
executeMode: ExecuteMode = ExecuteMode.Guess,
2223
exitCode: Int = 0,
24+
javaArgs: List[String] = List.empty,
25+
scalaArgs: List[String] = List.empty,
2326
residualArgs: List[String] = List.empty,
2427
scriptArgs: List[String] = List.empty,
2528
targetScript: String = "",
26-
areWithCompiler: Boolean = false,
29+
save: Boolean = false,
2730
) {
2831
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
2932
case ExecuteMode.Guess =>
@@ -33,6 +36,12 @@ case class Settings(
3336
this.copy(exitCode = 1)
3437
end withExecuteMode
3538

39+
def withScalaArgs(args: String*): Settings =
40+
this.copy(scalaArgs = scalaArgs.appendedAll(args.toList))
41+
42+
def withJavaArgs(args: String*): Settings =
43+
this.copy(javaArgs = javaArgs.appendedAll(args.toList))
44+
3645
def withResidualArgs(args: String*): Settings =
3746
this.copy(residualArgs = residualArgs.appendedAll(args.toList))
3847

@@ -47,23 +56,23 @@ case class Settings(
4756
this.copy(exitCode = 2)
4857
end withTargetScript
4958

50-
def withCompiler: Settings =
51-
this.copy(areWithCompiler = true)
59+
def withSave: Settings =
60+
this.copy(save = true)
5261
}
5362

5463
object MainGenericRunner {
5564

56-
final val classpathSeparator = ":"
65+
val classpathSeparator = File.pathSeparator
66+
67+
@sharable val javaOption = raw"""-J(.*)""".r
5768

5869
@tailrec
5970
def process(args: List[String], settings: Settings): Settings = args match
6071
case Nil =>
6172
settings
62-
case "-repl" :: tail =>
63-
process(tail, settings.withExecuteMode(ExecuteMode.Repl))
6473
case "-run" :: tail =>
6574
process(tail, settings.withExecuteMode(ExecuteMode.Run))
66-
case ("-cp" | "-classpath" | "--classpath") :: cp :: tail =>
75+
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
6776
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
6877
case ("-version" | "--version") :: _ =>
6978
settings.copy(
@@ -78,8 +87,10 @@ object MainGenericRunner {
7887
residualArgs = settings.residualArgs :+ "-verbose"
7988
)
8089
)
81-
case "-with-compiler" :: tail =>
82-
process(tail, settings.withCompiler)
90+
case "-save" :: tail =>
91+
process(tail, settings.withSave)
92+
case (o @ javaOption(striped)) :: tail =>
93+
process(tail, settings.withJavaArgs(striped).withScalaArgs(o))
8394
case arg :: tail =>
8495
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
8596
if arg.endsWith(".scala") || arg.endsWith(".sc") || (line.nonEmpty && raw"#!.*scala".r.matches(line.get)) then
@@ -93,7 +104,8 @@ object MainGenericRunner {
93104
def main(args: Array[String]): Unit =
94105
val settings = process(args.toList, Settings())
95106
if settings.exitCode != 0 then System.exit(settings.exitCode)
96-
settings.executeMode match
107+
108+
def run(mode: ExecuteMode): Unit = mode match
97109
case ExecuteMode.Repl =>
98110
val properArgs =
99111
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
@@ -104,20 +116,23 @@ object MainGenericRunner {
104116
val newClasspath = settings.classPath ++ getClasspath :+ "."
105117
List("-classpath", newClasspath.mkString(classpathSeparator)).filter(Function.const(newClasspath.nonEmpty))
106118
++ settings.residualArgs
107-
s"java ${properArgs.mkString(" ")}".! // For now we collect classpath that coursier provides for convenience
119+
s"java ${settings.javaArgs.mkString(" ")} ${properArgs.mkString(" ")}".! // For now we collect classpath that coursier provides for convenience
108120
case ExecuteMode.Script =>
109121
val properArgs =
110122
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
111123
++ settings.residualArgs
124+
++ (if settings.save then List("-save") else Nil)
112125
++ List("-script", settings.targetScript)
126+
++ settings.scalaArgs
113127
++ settings.scriptArgs
114128
scripting.Main.main(properArgs.toArray)
115129
case ExecuteMode.Guess =>
116-
val properArgs =
117-
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
118-
++ settings.residualArgs
119-
repl.Main.main(properArgs.toArray)
130+
if args.toList.forall(_.startsWith("-")) then // all are options
131+
run(ExecuteMode.Repl)
132+
else
133+
run(ExecuteMode.Run)
120134

135+
run(settings.executeMode)
121136

122137
private def getClasspath(cl: ClassLoader): Array[String] = cl match
123138
case null => Array()

compiler/test/dotty/tools/coursier/CoursierScalaTests.scala renamed to compiler/test-coursier/dotty/tools/coursier/CoursierScalaTests.scala

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package dotty
22
package tools
3-
package scripting
3+
package coursier
44

55
import java.io.File
66
import java.nio.file.{Path, Paths, Files}
77
import scala.sys.process._
8-
98
import org.junit.Test
109
import org.junit.BeforeClass
11-
12-
import vulpix.TestConfiguration
13-
14-
import dotty.tools.absPath
10+
import org.junit.Assert._
1511
import scala.collection.mutable.ListBuffer
1612

13+
import java.net.URLClassLoader
14+
import java.net.URL
15+
1716
class CoursierScalaTests:
1817

18+
private def scripts(path: String): Array[File] = {
19+
val dir = new File(getClass.getResource(path).getPath)
20+
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
21+
dir.listFiles
22+
}
23+
24+
extension (f: File) private def absPath =
25+
f.getAbsolutePath.replace('\\', '/')
26+
27+
extension (str: String) private def dropExtension =
28+
str.reverse.dropWhile(_ != '.').drop(1).reverse
29+
1930
// classpath tests are managed by scripting.ClasspathTests.scala
2031
def testFiles = scripts("/scripting").filter { ! _.getName.startsWith("classpath") }
2132

@@ -38,29 +49,36 @@ class CoursierScalaTests:
3849
)
3950
for (line, expect) <- output zip expectedOutput do
4051
printf("expected: %-17s\nactual : %s\n", expect, line)
41-
assert(output == expectedOutput)
52+
assertEquals(expectedOutput, output)
4253
scriptArgs()
4354

4455
def version() =
4556
val output = CoursierScalaTests.csCmd("-version")
46-
assert(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
57+
assertTrue(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
4758
version()
4859

4960
def emptyArgsEqualsRepl() =
5061
val output = CoursierScalaTests.csCmd()
51-
assert(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
62+
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
5263
emptyArgsEqualsRepl()
5364

54-
def repl() =
55-
val output = CoursierScalaTests.csCmd("-repl")
56-
assert(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
57-
repl()
58-
5965
def run() =
60-
val output = CoursierScalaTests.csCmd("-run", "-classpath", scripts("/run").head.getParent, "myfile")
61-
assert(output.mkString("\n") == "Hello")
66+
val output = CoursierScalaTests.csCmd("-run", "-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
67+
assertEquals(output.mkString("\n"), "Hello")
6268
run()
6369

70+
def notOnlyOptionsEqualsRun() =
71+
val output = CoursierScalaTests.csCmd("-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
72+
assertEquals(output.mkString("\n"), "Hello")
73+
notOnlyOptionsEqualsRun()
74+
75+
def jar() =
76+
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
77+
val output = CoursierScalaTests.csCmd("-save", source.absPath)
78+
assertEquals(output.mkString("\n"), "Hello")
79+
assertTrue(source.getParentFile.listFiles.find(_.getName == "myfile.jar").isDefined)
80+
jar()
81+
6482
object CoursierScalaTests:
6583

6684
def execCmd(command: String, options: String*): List[String] =
@@ -69,7 +87,6 @@ object CoursierScalaTests:
6987
cmd.!(ProcessLogger(out += _, out += _))
7088
out.toList
7189

72-
7390
def csCmd(options: String*): List[String] =
7491
val newOptions = options match
7592
case Nil => options
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
package run
2+
13
object myfile extends App:
24
println("Hello")
-2.16 KB
Binary file not shown.
-664 Bytes
Binary file not shown.
-514 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)