@@ -14,6 +14,9 @@ class ScriptingTests:
1414 extension (str : String ) def dropExtension =
1515 str.reverse.dropWhile(_ != '.' ).drop(1 ).reverse
1616
17+ extension(f : File ) def absPath =
18+ f.getAbsolutePath.replace('\\ ' ,'/' )
19+
1720 def testFiles = scripts(" /scripting" )
1821
1922 def script2jar (scriptFile : File ) =
@@ -23,7 +26,6 @@ class ScriptingTests:
2326 def showScriptUnderTest (scriptFile : File ): Unit =
2427 printf(" ===> test script name [%s]\n " ,scriptFile.getName)
2528
26-
2729 val argss : Map [String , Array [String ]] = (
2830 for
2931 argFile <- testFiles
@@ -40,8 +42,17 @@ class ScriptingTests:
4042 scriptArgs = argss.getOrElse(name, Array .empty[String ])
4143 yield scriptFile -> scriptArgs).toList.sortBy { (file,args) => file.getName }
4244
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+ }
4451
52+ /*
53+ * Call .scala scripts without -save option, verify no jar created
54+ */
55+ @ Test def scriptingDriverTests =
4556 for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .scala" ) do
4657 showScriptUnderTest(scriptFile)
4758 val unexpectedJar = script2jar(scriptFile)
@@ -56,9 +67,13 @@ class ScriptingTests:
5667 scriptArgs = scriptArgs
5768 ).compileAndRun { (path: java.nio.file.Path ,classpath: String , mainClass: String ) =>
5869 printf(" mainClass from ScriptingDriver: %s\n " ,mainClass)
70+ true // call compiled script main method
5971 }
6072 assert(! unexpectedJar.exists, s " not expecting jar file: ${unexpectedJar.absPath}" )
6173
74+ /*
75+ * Call .sc scripts without -save option, verify no jar created
76+ */
6277 @ Test def scriptingMainTests =
6378 for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .sc" ) do
6479 showScriptUnderTest(scriptFile)
@@ -74,6 +89,9 @@ class ScriptingTests:
7489 Main .main(mainArgs)
7590 assert(! unexpectedJar.exists, s " not expecting jar file: ${unexpectedJar.absPath}" )
7691
92+ /*
93+ * Call .sc scripts with -save option, verify jar is created.
94+ */
7795 @ Test def scriptingJarTest =
7896 for (scriptFile,scriptArgs) <- scalaFilesWithArgs(" .sc" ) do
7997 showScriptUnderTest(scriptFile)
@@ -92,11 +110,74 @@ class ScriptingTests:
92110 printf(" ===> test script jar name [%s]\n " ,expectedJar.getName)
93111 assert(expectedJar.exists)
94112
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