-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CoursierScalaTests.scala
173 lines (140 loc) · 7.03 KB
/
CoursierScalaTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dotty
package tools
package coursier
import java.io.File
import java.nio.file.{Path, Paths, Files}
import scala.sys.process._
import org.junit.Test
import org.junit.BeforeClass
import org.junit.Assert._
import scala.collection.mutable.ListBuffer
import java.net.URLClassLoader
import java.net.URL
class CoursierScalaTests:
private def scripts(path: String): Array[File] = {
val dir = new File(getClass.getResource(path).getPath)
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
dir.listFiles
}
extension (f: File) private def absPath =
f.getAbsolutePath.replace('\\', '/')
extension (str: String) private def dropExtension =
str.reverse.dropWhile(_ != '.').drop(1).reverse
// classpath tests are managed by scripting.ClasspathTests.scala
def testFiles = scripts("/scripting").filter { ! _.getName.startsWith("classpath") }
// Cannot run tests in parallel, more info here: https://stackoverflow.com/questions/6345660/java-executing-bash-script-error-26-text-file-busy
@Test def allTests =
def scriptArgs() =
val scriptPath = scripts("/scripting").find(_.getName == "showArgs.sc").get.absPath
val testScriptArgs = Seq("a", "b", "c", "-repl", "-run", "-script", "-debug")
val args = scriptPath +: testScriptArgs
val output = CoursierScalaTests.csScalaCmd(args*)
val expectedOutput = List(
"arg 0:[a]",
"arg 1:[b]",
"arg 2:[c]",
"arg 3:[-repl]",
"arg 4:[-run]",
"arg 5:[-script]",
"arg 6:[-debug]",
)
for (line, expect) <- output zip expectedOutput do
printf("expected: %-17s\nactual : %s\n", expect, line)
assertEquals(expectedOutput, output)
scriptArgs()
def scriptPath() =
val scriptPath = scripts("/scripting").find(_.getName == "scriptPath.sc").get.absPath
val args = scriptPath
val output = CoursierScalaTests.csScalaCmd(args)
assertTrue(output.mkString("\n").startsWith("script.path:"))
assertTrue(output.mkString("\n").endsWith("scriptPath.sc"))
scriptPath()
def scriptEnvDashJDashD() =
val scriptPath = scripts("/scripting").find(_.getName == "envtest.sc").get.absPath
val args = scriptPath
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", args)
assertEquals(output.mkString("\n"), "Hello World")
scriptEnvDashJDashD()
def version() =
val output = CoursierScalaTests.csScalaCmd("-version")
assertTrue(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
version()
def emptyArgsEqualsRepl() =
val output = CoursierScalaTests.csScalaCmd()
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
emptyArgsEqualsRepl()
def run() =
val output = CoursierScalaTests.csScalaCmd("-classpath", scripts("/run").head.getParentFile.getParent, "-run", "run.myfile")
assertEquals(output.mkString("\n"), "Hello")
run()
def runDashJDashD() =
val output = CoursierScalaTests.csScalaCmd("-J-Dkey=World", "-classpath", scripts("/run").head.getParentFile.getParent, "-run", "run.envtest")
assertEquals(output.mkString("\n"), "Hello World")
runDashJDashD()
def notOnlyOptionsEqualsRun() =
val output = CoursierScalaTests.csScalaCmd("-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
assertEquals(output.mkString("\n"), "Hello")
notOnlyOptionsEqualsRun()
def help() =
val output = CoursierScalaTests.csScalaCmd("-help")
assertTrue(output.mkString("\n").contains("Usage: scala <options> <source files>"))
help()
def jar() =
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
val output = CoursierScalaTests.csScalaCmd("-save", source.absPath)
assertEquals(output.mkString("\n"), "Hello")
assertTrue(source.getParentFile.listFiles.find(_.getName == "myfile.jar").isDefined)
jar()
def runThatJar() =
val source = new File(getClass.getResource("/run/myfile.jar").getPath)
val output = CoursierScalaTests.csScalaCmd(source.absPath)
assertEquals(output.mkString("\n"), "Hello")
runThatJar()
def compileFilesToJarAndRun() =
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
val prefix = source.getParent
val o1source = Paths.get(prefix, "automain.jar").toAbsolutePath.toString
val output1 = CoursierScalaTests.csScalaCompilerCmd("-d", o1source, source.absPath)
assertEquals(output1.mkString("\n"), "")
val o2source = Paths.get(prefix, "custommain.jar").toAbsolutePath.toString
val output2 = CoursierScalaTests.csScalaCompilerCmd("-d", o2source, "-Xmain-class", "run.myfile", source.absPath)
assertEquals(output2.mkString("\n"), "")
val output3 = CoursierScalaTests.csScalaCmd(o1source)
assertEquals(output3.mkString("\n"), "Hello")
val output4 = CoursierScalaTests.csScalaCmd(o2source)
assertEquals(output4.mkString("\n"), "Hello")
compileFilesToJarAndRun()
def replWithArgs() =
val output = CoursierScalaTests.csScalaCmd("-source", "3.0-migration")
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
replWithArgs()
def argumentFile() =
// verify that an arguments file is accepted
// verify that setting a user classpath does not remove compiler libraries from the classpath.
// arguments file contains "-classpath .", adding current directory to classpath.
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
val argsFile = new File(getClass.getResource("/run/myargs.txt").getPath)
val output = CoursierScalaTests.csScalaCmd(s"@$argsFile", source.absPath)
assertEquals(output.mkString("\n"), "Hello")
argumentFile()
object CoursierScalaTests:
def execCmd(command: String, options: String*): List[String] =
val cmd = (command :: options.toList).toSeq.mkString(" ")
val out = new ListBuffer[String]
cmd.!(ProcessLogger(out += _, out += _))
out.toList
def csScalaCmd(options: String*): List[String] =
csCmd("dotty.tools.MainGenericRunner", options*)
def csScalaCompilerCmd(options: String*): List[String] =
csCmd("dotty.tools.dotc.Main", options*)
private def csCmd(entry: String, options: String*): List[String] =
val (jOpts, args) = options.partition(_.startsWith("-J"))
val newOptions = args match
case Nil => args
case _ => "--" +: args
val newJOpts = jOpts.map(s => s"--java-opt ${s.stripPrefix("-J")}").mkString(" ")
execCmd("./cs", (s"""launch "org.scala-lang:scala3-compiler_3:${sys.env("DOTTY_BOOTSTRAPPED_VERSION")}" $newJOpts --main-class "$entry" --property "scala.usejavacp=true"""" +: newOptions)*)
/** Get coursier script */
@BeforeClass def setup(): Unit =
val ver = execCmd("uname").head.replace('L', 'l').replace('D', 'd')
execCmd("curl", s"-fLo cs https://git.io/coursier-cli-$ver") #&& execCmd("chmod", "+x cs")