@@ -8,36 +8,63 @@ import java.io.File
8
8
import java .nio .file .Files
9
9
import org .junit .Test
10
10
import org .junit .Assert .assertEquals
11
+ import org .junit .experimental .categories .Category
11
12
12
13
import ScriptTestEnv .*
13
14
14
15
15
16
object BashExitCodeTests :
16
- def testFiles = scripts(" /scripting/exit-code-tests" )
17
+ private def testFiles = scripts(" /scripting/exit-code-tests" )
17
18
18
19
/*
19
20
* Compiles the class checking exit code
20
21
*
21
- * @param testName name of the test containing the extension
22
+ * @param testName name of the test
22
23
* @param expectedExitCode expected exit code from the output
23
- * @param deleteTempDir if temporary directory created for compilation output should delete in this scope
24
- *
25
- * @return Created temporary directory
26
24
*/
27
- private def testCompilationExitCode (testName : String , expectedExitCode : Int , deleteTempDir : Boolean = false ): File =
28
- val temporaryDir = Files .createTempDirectory(testName)
25
+ private def compileAndVerifyExitCode (
26
+ testName : String ,
27
+ expectedExitCode : Int ,
28
+ )(using temporaryDir : File ): Unit =
29
29
assertTestExists(testName) { testFile =>
30
- try {
31
- val testFilePath = testFile.absPath
32
- val commandline = (Seq (scalacPath, " -d" , temporaryDir, testFilePath)).mkString(" " )
33
- val (validTest, exitCode, _, _) = bashCommand(commandline)
34
- if verifyValid(validTest) then
35
- assertEquals(expectedExitCode, exitCode)
36
- } finally {
37
- if deleteTempDir then Util .deleteFile(temporaryDir.toFile)
38
- }
30
+ val testFilePath = testFile.absPath
31
+ val commandline = (Seq (scalacPath, " -d" , temporaryDir, testFilePath)).mkString(" " )
32
+ val (validTest, exitCode, _, _) = bashCommand(commandline)
33
+ if verifyValid(validTest) then
34
+ assertEquals(expectedExitCode, exitCode)
39
35
}
40
- temporaryDir.toFile
36
+
37
+ /*
38
+ * Runs compiled code checking the exit code
39
+ *
40
+ * @param className name of compiled class
41
+ * @param runExitCode expected exit code from the runner
42
+ */
43
+ private def runClassAndVerifyExitCode (
44
+ className : String ,
45
+ expectedExitCode : Int
46
+ )(using temporaryDir : File ): Unit =
47
+ val testClassFile = temporaryDir.files.find(_.getName == s " $className.class " )
48
+ assert(testClassFile.isDefined)
49
+ val commandline = (Seq (scalaPath, " -classpath" , temporaryDir.getAbsolutePath, className)).mkString(" " )
50
+ val (validTest, exitCode, o, e) = bashCommand(commandline)
51
+ if verifyValid(validTest) then
52
+ assertEquals(expectedExitCode, exitCode)
53
+
54
+ /*
55
+ * Compiles and then runs code verifying runner status code
56
+ *
57
+ * @param testName name of the test
58
+ * @param className name of compiled class
59
+ * @param expectedRunExitCode expected exit code from the runner
60
+ */
61
+ private def compileRunAndVerifyExitCode (
62
+ testName : String ,
63
+ className : String ,
64
+ expectedRunExitCode : Int ,
65
+ )(using File ): Unit =
66
+ compileAndVerifyExitCode(testName, 0 )
67
+ runClassAndVerifyExitCode(className, expectedRunExitCode)
41
68
42
69
/*
43
70
* Runs the command and checks the exit code
@@ -51,30 +78,6 @@ object BashExitCodeTests:
51
78
if verifyValid(validTest) then
52
79
assertEquals(expectedExitCode, exitCode)
53
80
54
-
55
- /* Compiles and then runs the code checking the exit code
56
- *
57
- * @param testFileName name of the test
58
- * @param runExitCode expected exit code from the runner output
59
- */
60
- private def testCompileAndRun (testFileName : String , runExitCode : Int ): Unit =
61
- val outputDirectory = testCompilationExitCode(testFileName, 0 )
62
-
63
- def testRuntimeExitCode (className : String , expectedExitCode : Int ): Unit =
64
- val testClassFile = outputDirectory.files.find(_.getName == s " $className.class " )
65
- assert(testClassFile.isDefined)
66
- val commandline = (Seq (scalaPath, " -classpath" , outputDirectory.getAbsolutePath, className)).mkString(" " )
67
- val (validTest, exitCode, o, e) = bashCommand(commandline)
68
- if verifyValid(validTest) then
69
- assertEquals(expectedExitCode, exitCode)
70
-
71
- try {
72
- val generatedClassName = testFileName.split('.' ).head
73
- testRuntimeExitCode(generatedClassName, runExitCode)
74
- } finally {
75
- Util .deleteFile(outputDirectory)
76
- }
77
-
78
81
/*
79
82
* Checks if scripting test resources contains test with given `testName`
80
83
* And then runs function `test`
@@ -87,20 +90,51 @@ object BashExitCodeTests:
87
90
assert(file.isDefined)
88
91
test(file.get)
89
92
93
+ /*
94
+ * Runs test for created temporary file
95
+ * and ensures it deletion after function execution
96
+ *
97
+ * @param test check to be run on found test file
98
+ */
99
+ private def withTempFile (test : File => Unit ) =
100
+ val tempFile = Files .createTempFile(" temp-file" , " .class" ).toFile
101
+ try {
102
+ test(tempFile)
103
+ } finally {
104
+ Util .deleteFile(tempFile)
105
+ }
106
+
107
+ /*
108
+ * Runs test with implicit temporary directory
109
+ * and ensures it deletion after the function execution
110
+ *
111
+ * @param test test to be run with given temporary directory
112
+ */
113
+ private def withTempDirectory (test : File ?=> Unit ) =
114
+ given file : File = Files .createTempDirectory(" exit-code-tests" ).toFile
115
+ try { test } finally { Util .deleteFile(file) }
116
+
117
+ /*
118
+ * Returns path to the generated tasty file for given directory and classname
119
+ */
120
+ private def getGeneratedTastyPath (className : String )(using temporaryDir : File ): String =
121
+ temporaryDir.toPath.resolve(s " $className.tasty " ).toString
122
+
123
+ @ Category (Array (classOf [BootstrappedOnlyTests ]))
90
124
class BashExitCodeTests :
91
125
import BashExitCodeTests .*
92
126
93
127
@ Test def verifyExitCodeOnCompileError : Unit =
94
- testCompilationExitCode( " compileError.scala" , 1 , true )
128
+ withTempDirectory(compileAndVerifyExitCode( " compileError.scala" , 1 ) )
95
129
96
130
@ Test def verifyExitCodeOnRuntimeError : Unit =
97
- testCompileAndRun( " runtimeError.scala" , 1 )
131
+ withTempDirectory(compileRunAndVerifyExitCode( " runtimeError.scala" , " runtimeError " , 1 ) )
98
132
99
133
@ Test def verifyExitCode : Unit =
100
- testCompileAndRun( " positiveTest.scala" , 0 )
134
+ withTempDirectory(compileRunAndVerifyExitCode( " positiveTest.scala" , " positiveTest " , 0 ) )
101
135
102
136
@ Test def verifyExitCodeOnScriptError : Unit =
103
- assertTestExists(" scriptRuntimeError.sc" ) { file =>
137
+ assertTestExists(" scriptRuntimeError.sc" ){ file =>
104
138
testCommandExitCode(Seq (scalacPath, " -script" , file.absPath), 1 )
105
139
}
106
140
@@ -120,28 +154,24 @@ class BashExitCodeTests:
120
154
}
121
155
122
156
@ Test def verifyExitCodeOnDecompilation : Unit =
123
- val testName = " positiveTest "
124
- val outputDirectory = testCompilationExitCode( s " $testName .scala " , 0 )
125
- val tastyFilePath = outputDirectory.toPath.resolve( s " $testName .tasty " ).toString
126
- testCommandExitCode( Seq (scalacPath, " -decompile " , tastyFilePath), 0 )
157
+ withTempDirectory {
158
+ compileAndVerifyExitCode( " positiveTest .scala" , 0 )
159
+ testCommandExitCode( Seq (scalacPath, " -decompile " , getGeneratedTastyPath( " positiveTest " )), 0 )
160
+ }
127
161
128
162
@ Test def verifyExitCodeOnPrintTasty : Unit =
129
- val testName = " positiveTest "
130
- val outputDirectory = testCompilationExitCode( s " $testName .scala " , 0 )
131
- val tastyFilePath = outputDirectory.toPath.resolve( s " $testName .tasty " ).toString
132
- testCommandExitCode( Seq (scalacPath, " -print-tasty " , tastyFilePath), 0 )
163
+ withTempDirectory {
164
+ compileAndVerifyExitCode( " positiveTest .scala" , 0 )
165
+ testCommandExitCode( Seq (scalacPath, " -print-tasty " , getGeneratedTastyPath( " positiveTest " )), 0 )
166
+ }
133
167
134
168
@ Test def verifyExitCodeOnDecompilationFailure : Unit =
135
- val tempFile = Files .createTempFile( " temp-file " , " .class " ).toFile
169
+ withTempFile(file => testCommandExitCode( Seq (scalacPath, " -decompile " , file.absPath), 1 ))
136
170
testCommandExitCode(Seq (scalacPath, " -decompile" , " non-existing-file.tasty" ), 1 )
137
- testCommandExitCode(Seq (scalacPath, " -decompile" , tempFile.absPath), 1 )
138
- Util .deleteFile(tempFile)
139
171
140
172
@ Test def verifyExitCodeOnPrintTastyFailure : Unit =
141
- val tempFile = Files .createTempFile( " temp-file " , " .class " ).toFile
173
+ withTempFile(file => testCommandExitCode( Seq (scalacPath, " -print-tasty " , file.absPath), 1 ))
142
174
testCommandExitCode(Seq (scalacPath, " -print-tasty" , " non-existing-file.tasty" ), 1 )
143
- testCommandExitCode(Seq (scalacPath, " -print-tasty" , tempFile.absPath), 1 )
144
- Util .deleteFile(tempFile)
145
175
146
176
@ Test def verifyExitCodeOnExpressionCompileError : Unit =
147
177
testCommandExitCode(Seq (scalaPath, " -e" , " 'prinln(10*10)'" ), 1 )
0 commit comments