@@ -14,6 +14,9 @@ class ScriptingTests:
14
14
extension (str : String ) def dropExtension =
15
15
str.reverse.dropWhile(_ != '.' ).drop(1 ).reverse
16
16
17
+ extension(f : File ) def absPath =
18
+ f.getAbsolutePath.replace('\\ ' ,'/' )
19
+
17
20
def testFiles = scripts(" /scripting" )
18
21
19
22
def script2jar (scriptFile : File ) =
@@ -23,7 +26,6 @@ class ScriptingTests:
23
26
def showScriptUnderTest (scriptFile : File ): Unit =
24
27
printf(" ===> test script name [%s]\n " ,scriptFile.getName)
25
28
26
-
27
29
val argss : Map [String , Array [String ]] = (
28
30
for
29
31
argFile <- testFiles
@@ -40,8 +42,17 @@ class ScriptingTests:
40
42
scriptArgs = argss.getOrElse(name, Array .empty[String ])
41
43
yield scriptFile -> scriptArgs).toList.sortBy { (file,args) => file.getName }
42
44
43
- @ Test def scriptingDriverTests =
45
+ def callExecutableJar (script : File ,jar : File , scriptArgs : Array [String ] = Array .empty[String ]) = {
46
+ import scala .sys .process ._
47
+ val cmd = Array (" java" ,s " -Dscript.path= ${script.getName}" ," -jar" ,jar.absPath)
48
+ ++ scriptArgs
49
+ Process (cmd).lazyLines_!.foreach { println }
50
+ }
44
51
52
+ /*
53
+ * Call .scala scripts without -save option, verify no jar created
54
+ */
55
+ @ Test def scriptingDriverTests =
45
56
for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .scala" ) do
46
57
showScriptUnderTest(scriptFile)
47
58
val unexpectedJar = script2jar(scriptFile)
@@ -56,9 +67,13 @@ class ScriptingTests:
56
67
scriptArgs = scriptArgs
57
68
).compileAndRun { (path: java.nio.file.Path ,classpath: String , mainClass: String ) =>
58
69
printf(" mainClass from ScriptingDriver: %s\n " ,mainClass)
70
+ true // call compiled script main method
59
71
}
60
72
assert(! unexpectedJar.exists, s " not expecting jar file: ${unexpectedJar.absPath}" )
61
73
74
+ /*
75
+ * Call .sc scripts without -save option, verify no jar created
76
+ */
62
77
@ Test def scriptingMainTests =
63
78
for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .sc" ) do
64
79
showScriptUnderTest(scriptFile)
@@ -74,6 +89,9 @@ class ScriptingTests:
74
89
Main .main(mainArgs)
75
90
assert(! unexpectedJar.exists, s " not expecting jar file: ${unexpectedJar.absPath}" )
76
91
92
+ /*
93
+ * Call .sc scripts with -save option, verify jar is created.
94
+ */
77
95
@ Test def scriptingJarTest =
78
96
for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .sc" ) do
79
97
showScriptUnderTest(scriptFile)
@@ -92,11 +110,74 @@ class ScriptingTests:
92
110
printf(" ===> test script jar name [%s]\n " ,expectedJar.getName)
93
111
assert(expectedJar.exists)
94
112
95
- import scala .sys .process ._
96
- val cmd = Array (" java" ,s " -Dscript.path= ${scriptFile.getName}" ," -jar" ,expectedJar.absPath)
97
- ++ scriptArgs
98
- Process (cmd).lazyLines_!.foreach { println }
99
-
100
- extension(f : File ){
101
- def absPath = f.getAbsolutePath.replace('\\ ' ,'/' )
102
- }
113
+ callExecutableJar(scriptFile, expectedJar, scriptArgs)
114
+
115
+ /*
116
+ * Verify that when ScriptingDriver callback returns true, main is called.
117
+ * Verify that when ScriptingDriver callback returns false, main is not called.
118
+ */
119
+ @ Test def scriptCompileOnlyTests =
120
+ val scriptFile = touchFileScript
121
+ showScriptUnderTest(scriptFile)
122
+
123
+ // verify main method not called when false is returned
124
+ printf(" testing script compile, with no call to script main method.\n " )
125
+ touchedFile.delete
126
+ assert(! touchedFile.exists, s " unable to delete ${touchedFile}" )
127
+ ScriptingDriver (
128
+ compilerArgs = Array (" -classpath" , TestConfiguration .basicClasspath),
129
+ scriptFile = scriptFile,
130
+ scriptArgs = Array .empty[String ]
131
+ ).compileAndRun { (path: java.nio.file.Path ,classpath: String , mainClass: String ) =>
132
+ printf(" success: no call to main method in mainClass: %s\n " ,mainClass)
133
+ false // no call to compiled script main method
134
+ }
135
+ touchedFile.delete
136
+ assert( ! touchedFile.exists, s " unable to delete ${touchedFile}" )
137
+
138
+ // verify main method is called when true is returned
139
+ printf(" testing script compile, with call to script main method.\n " )
140
+ ScriptingDriver (
141
+ compilerArgs = Array (" -classpath" , TestConfiguration .basicClasspath),
142
+ scriptFile = scriptFile,
143
+ scriptArgs = Array .empty[String ]
144
+ ).compileAndRun { (path: java.nio.file.Path ,classpath: String , mainClass: String ) =>
145
+ printf(" call main method in mainClass: %s\n " ,mainClass)
146
+ true // call compiled script main method, create touchedFile
147
+ }
148
+
149
+ if touchedFile.exists then
150
+ printf(" success: script created file %s\n " ,touchedFile)
151
+ if touchedFile.exists then printf(" success: created file %s\n " ,touchedFile)
152
+ assert( touchedFile.exists, s " expected to find file ${touchedFile}" )
153
+
154
+ /*
155
+ * Compile touchFile.sc to create executable jar, verify jar execution succeeds.
156
+ */
157
+ @ Test def scriptingNoCompileJar =
158
+ val scriptFile = touchFileScript
159
+ showScriptUnderTest(scriptFile)
160
+ val expectedJar = script2jar(scriptFile)
161
+ sys.props(" script.path" ) = scriptFile.absPath
162
+ val mainArgs : Array [String ] = Array (
163
+ " -classpath" , TestConfiguration .basicClasspath.toString,
164
+ " -save" ,
165
+ " -script" , scriptFile.toString,
166
+ " -compile-only"
167
+ )
168
+
169
+ expectedJar.delete
170
+ Main .main(mainArgs) // create executable jar
171
+ printf(" ===> test script jar name [%s]\n " ,expectedJar.getName)
172
+ assert(expectedJar.exists,s " unable to create executable jar [ $expectedJar] " )
173
+
174
+ touchedFile.delete
175
+ assert(! touchedFile.exists,s " unable to delete ${touchedFile}" )
176
+ printf(" calling executable jar %s\n " ,expectedJar)
177
+ callExecutableJar(scriptFile, expectedJar)
178
+ if touchedFile.exists then
179
+ printf(" success: executable jar created file %s\n " ,touchedFile)
180
+ assert( touchedFile.exists, s " expected to find file ${touchedFile}" )
181
+
182
+ def touchFileScript = testFiles.find(_.getName == " touchFile.sc" ).get
183
+ def touchedFile = File (" touchedFile.out" )
0 commit comments