Skip to content

Commit 6d7a873

Browse files
committed
refactor tests to guarantee file deletion, fix leftover test output from ScriptingTests
1 parent 6fa07cf commit 6d7a873

File tree

3 files changed

+92
-60
lines changed

3 files changed

+92
-60
lines changed

compiler/test/dotty/tools/scripting/BashExitCodeTests.scala

Lines changed: 89 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,63 @@ import java.io.File
88
import java.nio.file.Files
99
import org.junit.Test
1010
import org.junit.Assert.assertEquals
11+
import org.junit.experimental.categories.Category
1112

1213
import ScriptTestEnv.*
1314

1415

1516
object BashExitCodeTests:
16-
def testFiles = scripts("/scripting/exit-code-tests")
17+
private def testFiles = scripts("/scripting/exit-code-tests")
1718

1819
/*
1920
* Compiles the class checking exit code
2021
*
21-
* @param testName name of the test containing the extension
22+
* @param testName name of the test
2223
* @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
2624
*/
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 =
2929
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)
3935
}
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)
4168

4269
/*
4370
* Runs the command and checks the exit code
@@ -51,30 +78,6 @@ object BashExitCodeTests:
5178
if verifyValid(validTest) then
5279
assertEquals(expectedExitCode, exitCode)
5380

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-
7881
/*
7982
* Checks if scripting test resources contains test with given `testName`
8083
* And then runs function `test`
@@ -87,20 +90,51 @@ object BashExitCodeTests:
8790
assert(file.isDefined)
8891
test(file.get)
8992

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]))
90124
class BashExitCodeTests:
91125
import BashExitCodeTests.*
92126

93127
@Test def verifyExitCodeOnCompileError: Unit =
94-
testCompilationExitCode("compileError.scala", 1, true)
128+
withTempDirectory(compileAndVerifyExitCode("compileError.scala", 1))
95129

96130
@Test def verifyExitCodeOnRuntimeError: Unit =
97-
testCompileAndRun("runtimeError.scala", 1)
131+
withTempDirectory(compileRunAndVerifyExitCode("runtimeError.scala", "runtimeError", 1))
98132

99133
@Test def verifyExitCode: Unit =
100-
testCompileAndRun("positiveTest.scala", 0)
134+
withTempDirectory(compileRunAndVerifyExitCode("positiveTest.scala", "positiveTest", 0))
101135

102136
@Test def verifyExitCodeOnScriptError: Unit =
103-
assertTestExists("scriptRuntimeError.sc") { file =>
137+
assertTestExists("scriptRuntimeError.sc"){ file =>
104138
testCommandExitCode(Seq(scalacPath, "-script", file.absPath), 1)
105139
}
106140

@@ -120,28 +154,24 @@ class BashExitCodeTests:
120154
}
121155

122156
@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+
}
127161

128162
@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+
}
133167

134168
@Test def verifyExitCodeOnDecompilationFailure: Unit =
135-
val tempFile = Files.createTempFile("temp-file", ".class").toFile
169+
withTempFile(file => testCommandExitCode(Seq(scalacPath, "-decompile", file.absPath), 1))
136170
testCommandExitCode(Seq(scalacPath, "-decompile", "non-existing-file.tasty"), 1)
137-
testCommandExitCode(Seq(scalacPath, "-decompile", tempFile.absPath), 1)
138-
Util.deleteFile(tempFile)
139171

140172
@Test def verifyExitCodeOnPrintTastyFailure: Unit =
141-
val tempFile = Files.createTempFile("temp-file", ".class").toFile
173+
withTempFile(file => testCommandExitCode(Seq(scalacPath, "-print-tasty", file.absPath), 1))
142174
testCommandExitCode(Seq(scalacPath, "-print-tasty", "non-existing-file.tasty"), 1)
143-
testCommandExitCode(Seq(scalacPath, "-print-tasty", tempFile.absPath), 1)
144-
Util.deleteFile(tempFile)
145175

146176
@Test def verifyExitCodeOnExpressionCompileError: Unit =
147177
testCommandExitCode(Seq(scalaPath, "-e", "'prinln(10*10)'"), 1)

compiler/test/dotty/tools/scripting/ScriptingTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ScriptingTests:
122122
/*
123123
* Compile touchFile.sc to create executable jar, verify jar execution succeeds.
124124
*/
125-
@Test def scriptingNoCompileJar =
125+
@Test def scriptingNoCompileJar: Unit =
126126
val scriptFile = touchFileScript
127127
showScriptUnderTest(scriptFile)
128128
val expectedJar = script2jar(scriptFile)
@@ -146,6 +146,8 @@ class ScriptingTests:
146146
if touchedFile.exists then
147147
printf("success: executable jar created file %s\n", touchedFile)
148148
assert( touchedFile.exists, s"expected to find file ${touchedFile}" )
149+
touchedFile.delete
150+
assert( !touchedFile.exists, s"unable to delete ${touchedFile}" )
149151

150152
///////////////////////////////////
151153
def touchFileScript = testFiles.find(_.getName == "touchFile.sc").get

touchedFile.out

Whitespace-only changes.

0 commit comments

Comments
 (0)