-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ScriptingTests.scala
185 lines (155 loc) · 6.72 KB
/
ScriptingTests.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
173
174
175
176
177
178
179
180
181
182
183
184
185
package dotty
package tools
package scripting
import java.io.File
import java.nio.file.Path
import org.junit.Test
import vulpix.TestConfiguration
/** Runs all tests contained in `compiler/test-resources/scripting/` */
class ScriptingTests:
extension (str: String) def dropExtension =
str.reverse.dropWhile(_ != '.').drop(1).reverse
extension(f: File) def absPath =
f.getAbsolutePath.replace('\\', '/')
// classpath tests managed by scripting.ClasspathTests.scala
def testFiles = scripts("/scripting").filter { ! _.getName.toLowerCase.contains("classpath") }
def script2jar(scriptFile: File) =
val jarName = s"${scriptFile.getName.dropExtension}.jar"
File(scriptFile.getParent, jarName)
def showScriptUnderTest(scriptFile: File): Unit =
printf("===> test script name [%s]\n", scriptFile.getName)
val argss: Map[String, Array[String]] = (
for
argFile <- testFiles
if argFile.getName.endsWith(".args")
name = argFile.getName.dropExtension
scriptArgs = readLines(argFile).toArray
yield name -> scriptArgs).toMap
def scalaFilesWithArgs(extension: String) = (
for
scriptFile <- testFiles
if scriptFile.getName.endsWith(extension)
name = scriptFile.getName.dropExtension
scriptArgs = argss.getOrElse(name, Array.empty[String])
yield scriptFile -> scriptArgs).toList.sortBy { (file, args) => file.getName }
def callExecutableJar(script: File, jar: File, scriptArgs: Array[String] = Array.empty[String]) = {
import scala.sys.process._
val cmd = Array("java", s"-Dscript.path=${script.getName}", "-jar", jar.absPath)
++ scriptArgs
Process(cmd).lazyLines_!.foreach { println }
}
/*
* Call .scala scripts without -save option, verify no jar created
*/
@Test def scriptingDriverTests =
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".scala") do
showScriptUnderTest(scriptFile)
val unexpectedJar = script2jar(scriptFile)
unexpectedJar.delete
sys.props("script.path") = scriptFile.absPath
ScriptingDriver(
compilerArgs = Array(
"-classpath", TestConfiguration.basicClasspath
),
scriptFile = scriptFile,
scriptArgs = scriptArgs
).compileAndRun { (path:java.nio.file.Path, classpathEntries:Seq[Path], mainClass:String) =>
printf("mainClass from ScriptingDriver: %s\n", mainClass)
true // call compiled script main method
}
assert(! unexpectedJar.exists, s"not expecting jar file: ${unexpectedJar.absPath}")
/*
* Call .sc scripts without -save option, verify no jar created
*/
@Test def scriptingMainTests =
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
showScriptUnderTest(scriptFile)
val unexpectedJar = script2jar(scriptFile)
unexpectedJar.delete
sys.props("script.path") = scriptFile.absPath
val mainArgs: Array[String] = Array(
"-classpath", TestConfiguration.basicClasspath.toString,
"-script", scriptFile.toString,
) ++ scriptArgs
Main.main(mainArgs)
assert(! unexpectedJar.exists, s"not expecting jar file: ${unexpectedJar.absPath}")
/*
* Call .sc scripts with -save option, verify jar is created.
*/
@Test def scriptingJarTest =
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
showScriptUnderTest(scriptFile)
val expectedJar = script2jar(scriptFile)
expectedJar.delete
sys.props("script.path") = scriptFile.absPath
val mainArgs: Array[String] = Array(
"-classpath", TestConfiguration.basicClasspath.toString,
"-save",
"-script", scriptFile.toString,
) ++ scriptArgs
Main.main(mainArgs)
printf("===> test script jar name [%s]\n", expectedJar.getName)
assert(expectedJar.exists)
callExecutableJar(scriptFile, expectedJar, scriptArgs)
/*
* Verify that when ScriptingDriver callback returns true, main is called.
* Verify that when ScriptingDriver callback returns false, main is not called.
*/
@Test def scriptCompileOnlyTests =
val scriptFile = touchFileScript
showScriptUnderTest(scriptFile)
// verify main method not called when false is returned
printf("testing script compile, with no call to script main method.\n")
touchedFile.delete
assert(!touchedFile.exists, s"unable to delete ${touchedFile}")
ScriptingDriver(
compilerArgs = Array("-classpath", TestConfiguration.basicClasspath),
scriptFile = scriptFile,
scriptArgs = Array.empty[String]
).compileAndRun { (path:java.nio.file.Path, classpathEntries:Seq[Path], mainClass:String) =>
printf("success: no call to main method in mainClass: %s\n", mainClass)
false // no call to compiled script main method
}
touchedFile.delete
assert( !touchedFile.exists, s"unable to delete ${touchedFile}" )
// verify main method is called when true is returned
printf("testing script compile, with call to script main method.\n")
ScriptingDriver(
compilerArgs = Array("-classpath", TestConfiguration.basicClasspath),
scriptFile = scriptFile,
scriptArgs = Array.empty[String]
).compileAndRun { (path:java.nio.file.Path, classpathEntries:Seq[Path], mainClass:String) =>
printf("call main method in mainClass: %s\n", mainClass)
true // call compiled script main method, create touchedFile
}
if touchedFile.exists then
printf("success: script created file %s\n", touchedFile)
if touchedFile.exists then printf("success: created file %s\n", touchedFile)
assert( touchedFile.exists, s"expected to find file ${touchedFile}" )
/*
* Compile touchFile.sc to create executable jar, verify jar execution succeeds.
*/
@Test def scriptingNoCompileJar =
val scriptFile = touchFileScript
showScriptUnderTest(scriptFile)
val expectedJar = script2jar(scriptFile)
sys.props("script.path") = scriptFile.absPath
val mainArgs: Array[String] = Array(
"-classpath", TestConfiguration.basicClasspath.toString,
"-save",
"-script", scriptFile.toString,
"-compile-only"
)
expectedJar.delete
Main.main(mainArgs) // create executable jar
printf("===> test script jar name [%s]\n", expectedJar.getName)
assert(expectedJar.exists, s"unable to create executable jar [$expectedJar]")
touchedFile.delete
assert(!touchedFile.exists, s"unable to delete ${touchedFile}")
printf("calling executable jar %s\n", expectedJar)
callExecutableJar(scriptFile, expectedJar)
if touchedFile.exists then
printf("success: executable jar created file %s\n", touchedFile)
assert( touchedFile.exists, s"expected to find file ${touchedFile}" )
def touchFileScript = testFiles.find(_.getName == "touchFile.sc").get
def touchedFile = File("touchedFile.out")