|
| 1 | +package dotty |
| 2 | +package tools |
| 3 | +package scripting |
| 4 | + |
| 5 | +import java.io.File |
| 6 | +import java.nio.file.Files |
| 7 | +import org.junit.Test |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | + |
| 10 | +import ScriptTestEnv.* |
| 11 | + |
| 12 | + |
| 13 | +object BashExitCodeTests: |
| 14 | + def testFiles = scripts("/scripting") |
| 15 | + |
| 16 | + /* |
| 17 | + * Compiles the class checking exit code |
| 18 | + * |
| 19 | + * @param testName name of the test containing the extension |
| 20 | + * @param expectedExitCode expected exit code from the output |
| 21 | + * @param deleteTempDir if temporary directory created for compilation output should delete in this scope |
| 22 | + * |
| 23 | + * @return Created temporary directory |
| 24 | + */ |
| 25 | + private def testCompilationExitCode(testName: String, expectedExitCode: Int, deleteTempDir: Boolean = false): File = |
| 26 | + val temporaryDir = Files.createTempDirectory(testName) |
| 27 | + assertTestExists(testName) { testFile => |
| 28 | + try { |
| 29 | + val testFilePath = testFile.absPath |
| 30 | + val commandline = (Seq(scalacPath, "-d", temporaryDir, testFilePath)).mkString(" ") |
| 31 | + val (validTest, exitCode, _, _) = bashCommand(commandline) |
| 32 | + if verifyValid(validTest) then |
| 33 | + assertEquals(expectedExitCode, exitCode) |
| 34 | + } finally { |
| 35 | + if deleteTempDir then Util.deleteFile(temporaryDir.toFile) |
| 36 | + } |
| 37 | + } |
| 38 | + temporaryDir.toFile |
| 39 | + |
| 40 | + /* |
| 41 | + * Runs the command and checks the exit code |
| 42 | + * |
| 43 | + * @param args arguments for command line |
| 44 | + * @param expectedExitCode expected exit code from the output |
| 45 | + */ |
| 46 | + private def testCommandExitCode(args: Seq[String], expectedExitCode: Int): Unit = |
| 47 | + val commandline = args.mkString(" ") |
| 48 | + val (validTest, exitCode, output, erroutput) = bashCommand(commandline) |
| 49 | + if verifyValid(validTest) then |
| 50 | + assertEquals(expectedExitCode, exitCode) |
| 51 | + |
| 52 | + |
| 53 | + /* Compiles and then runs the code checking the exit code |
| 54 | + * |
| 55 | + * @param testFileName name of the test |
| 56 | + * @param runExitCode expected exit code from the runner output |
| 57 | + */ |
| 58 | + private def testCompileAndRun(testFileName: String, runExitCode: Int): Unit = |
| 59 | + val outputDirectory = testCompilationExitCode(testFileName, 0) |
| 60 | + |
| 61 | + def testRuntimeExitCode(className: String, expectedExitCode: Int): Unit = |
| 62 | + val testClassFile = outputDirectory.files.find(_.getName == s"$className.class") |
| 63 | + assert(testClassFile.isDefined) |
| 64 | + val commandline = (Seq(scalaPath, "-classpath", outputDirectory.getAbsolutePath, className)).mkString(" ") |
| 65 | + val (validTest, exitCode, o, e) = bashCommand(commandline) |
| 66 | + if verifyValid(validTest) then |
| 67 | + assertEquals(expectedExitCode, exitCode) |
| 68 | + |
| 69 | + try { |
| 70 | + val generatedClassName = testFileName.split('.').head |
| 71 | + testRuntimeExitCode(generatedClassName, runExitCode) |
| 72 | + } finally { |
| 73 | + Util.deleteFile(outputDirectory) |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + * Checks if scripting test resources contains test with given `testName` |
| 78 | + * And then runs function `test` |
| 79 | + * |
| 80 | + * @param testName name of the test containing the extension |
| 81 | + * @param test check to be run on found test file |
| 82 | + */ |
| 83 | + private def assertTestExists(testName: String)(test: File => Unit) = |
| 84 | + val file = testFiles.find(_.getName == testName) |
| 85 | + assert(file.isDefined) |
| 86 | + test(file.get) |
| 87 | + |
| 88 | +class BashExitCodeTests: |
| 89 | + import BashExitCodeTests.* |
| 90 | + |
| 91 | + @Test def verifyExitCodeOnCompileError: Unit = |
| 92 | + testCompilationExitCode("compileError.scala", 1, true) |
| 93 | + |
| 94 | + @Test def verifyExitCodeOnRuntimeError: Unit = |
| 95 | + testCompileAndRun("runtimeError.scala", 1) |
| 96 | + |
| 97 | + @Test def verifyExitCode: Unit = |
| 98 | + testCompileAndRun("positiveTest.scala", 0) |
| 99 | + |
| 100 | + @Test def verifyExitCodeOnScriptError: Unit = |
| 101 | + assertTestExists("scriptRuntimeError.sc") { file => |
| 102 | + testCommandExitCode(Seq(scalacPath, "-script", file.absPath), 1) |
| 103 | + } |
| 104 | + |
| 105 | + @Test def verifyExitCodeOnScriptErrorCompiler: Unit = |
| 106 | + assertTestExists("scriptRuntimeError.sc") { file => |
| 107 | + testCommandExitCode(Seq(scalacPath, "-script", file.absPath), 1) |
| 108 | + } |
| 109 | + |
| 110 | + @Test def verifyExitCodeOnScript: Unit = |
| 111 | + assertTestExists("scriptPositive.sc") { file => |
| 112 | + testCommandExitCode(Seq(scalaPath, file.absPath), 0) |
| 113 | + } |
| 114 | + |
| 115 | + @Test def verifyExitCodeOnScriptCompiler: Unit = |
| 116 | + assertTestExists("scriptPositive.sc") { file => |
| 117 | + testCommandExitCode(Seq(scalacPath, "-script", file.absPath), 0) |
| 118 | + } |
| 119 | + |
| 120 | + @Test def verifyExitCodeOnDecompilation: Unit = |
| 121 | + val testName = "positiveTest" |
| 122 | + val outputDirectory = testCompilationExitCode(s"$testName.scala", 0) |
| 123 | + val tastyFilePath = outputDirectory.toPath.resolve(s"$testName.tasty").toString |
| 124 | + testCommandExitCode(Seq(scalacPath, "-decompile", tastyFilePath), 0) |
| 125 | + |
| 126 | + @Test def verifyExitCodeOnPrintTasty: Unit = |
| 127 | + val testName = "positiveTest" |
| 128 | + val outputDirectory = testCompilationExitCode(s"$testName.scala", 0) |
| 129 | + val tastyFilePath = outputDirectory.toPath.resolve(s"$testName.tasty").toString |
| 130 | + testCommandExitCode(Seq(scalacPath, "-print-tasty", tastyFilePath), 0) |
| 131 | + |
| 132 | + @Test def verifyExitCodeOnDecompilationFailure: Unit = |
| 133 | + val tempFile = Files.createTempFile("temp-file", ".class").toFile |
| 134 | + testCommandExitCode(Seq(scalacPath, "-decompile", "non-existing-file.tasty"), 1) |
| 135 | + testCommandExitCode(Seq(scalacPath, "-decompile", tempFile.absPath), 1) |
| 136 | + Util.deleteFile(tempFile) |
| 137 | + |
| 138 | + @Test def verifyExitCodeOnPrintTastyFailure: Unit = |
| 139 | + val tempFile = Files.createTempFile("temp-file", ".class").toFile |
| 140 | + testCommandExitCode(Seq(scalacPath, "-print-tasty", "non-existing-file.tasty"), 1) |
| 141 | + testCommandExitCode(Seq(scalacPath, "-print-tasty", tempFile.absPath), 1) |
| 142 | + Util.deleteFile(tempFile) |
| 143 | + |
| 144 | + @Test def verifyExitCodeOnExpressionCompileError: Unit = |
| 145 | + testCommandExitCode(Seq(scalaPath, "-e", "'prinln(10*10)'"), 1) |
| 146 | + |
| 147 | + @Test def verifyExitCodeOnExpressionRuntimeError: Unit = |
| 148 | + testCommandExitCode(Seq(scalaPath, "-e", "'1/0'"), 1) |
| 149 | + |
| 150 | + @Test def verifyExitCodeOnExpression: Unit = |
| 151 | + testCommandExitCode(Seq(scalaPath, "-e", "'println(10*10)'"), 0) |
| 152 | + |
| 153 | + @Test def verifyExitCodeOnInfo: Unit = |
| 154 | + List("--help", "--version", "-Xplugin-list", "-Vphases").foreach { flag => |
| 155 | + testCommandExitCode(Seq(scalaPath, flag), 0) |
| 156 | + } |
0 commit comments