|
| 1 | +package dotty |
| 2 | +package tools |
| 3 | +package scripting |
| 4 | + |
| 5 | +import java.io.File |
| 6 | +import java.nio.file.{Path, Paths, Files} |
| 7 | + |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +import vulpix.TestConfiguration |
| 11 | + |
| 12 | + |
| 13 | +/** Runs all tests contained in `compiler/test-resources/scripting/` */ |
| 14 | +class BashScriptsTests: |
| 15 | + // classpath tests managed by scripting.ClasspathTests.scala |
| 16 | + def testFiles = scripts("/scripting").filter { ! _.getName.startsWith("classpath") } |
| 17 | + |
| 18 | + @Test def verifyScriptArgs = |
| 19 | + import scala.sys.process._ |
| 20 | + val showArgsScript = testFiles.find(_.getName == "showArgs.sc").get.absPath |
| 21 | + val scalacPath = which("scalac") |
| 22 | + val commandline = Seq(scalacPath, "-script", showArgsScript, "a", "b", "c", "-repl", "-run", "-script", "-debug").mkString(" ") |
| 23 | + val bashExe = getBashPath |
| 24 | + val bashPath = Paths.get(bashExe) |
| 25 | + printf("bashExe: [%s]\n", bashExe) |
| 26 | + if bashPath.toFile.exists then |
| 27 | + var cmd = Array(bashExe, "-c", commandline) |
| 28 | + val output = for { |
| 29 | + line <- Process(cmd).lazyLines_! |
| 30 | + } yield line |
| 31 | + val expected = Seq( |
| 32 | + "arg 0:[a]", |
| 33 | + "arg 1:[b]", |
| 34 | + "arg 2:[c]", |
| 35 | + "arg 3:[-repl]", |
| 36 | + "arg 4:[-run]", |
| 37 | + "arg 5:[-script]", |
| 38 | + "arg 6:[-debug]", |
| 39 | + ) |
| 40 | + var fail = false |
| 41 | + printf("\n") |
| 42 | + for (line, expect) <- output zip expected do |
| 43 | + printf("expected: %-17s| actual: %s\n", line, expect) |
| 44 | + if line != expect then |
| 45 | + fail = true |
| 46 | + |
| 47 | + if fail then |
| 48 | + assert(output == expected) |
| 49 | + |
| 50 | + extension (str: String) def dropExtension = |
| 51 | + str.reverse.dropWhile(_ != '.').drop(1).reverse |
| 52 | + |
| 53 | + extension(f: File) def absPath = |
| 54 | + f.getAbsolutePath.replace('\\', '/') |
| 55 | + |
| 56 | + lazy val osname = Option(sys.props("os.name")).getOrElse("").toLowerCase |
| 57 | + |
| 58 | + def getBashPath: String = |
| 59 | + var whichBash = "" |
| 60 | + printf("osname[%s]\n", osname) |
| 61 | + if osname.startsWith("windows") then |
| 62 | + whichBash = which("bash.exe") |
| 63 | + /* |
| 64 | + val testCygpath = which("cygpath.exe") |
| 65 | + printf("testCygpath[%s]\n", testCygpath) |
| 66 | + if testCygpath.nonEmpty then |
| 67 | + whichBash = execCmd(testCygpath, "-m", whichBash).mkString(" ").trim |
| 68 | + printf("whichBash[%s]\n", whichBash) |
| 69 | + */ |
| 70 | + |
| 71 | + else |
| 72 | + whichBash = which("bash") |
| 73 | + |
| 74 | + whichBash |
| 75 | + |
| 76 | + def execCmd(command: String, options: String *): Seq[String] = |
| 77 | + val cmd = (command :: options.toList).toSeq |
| 78 | + import scala.sys.process._ |
| 79 | + for { |
| 80 | + line <- Process(cmd).lazyLines_! |
| 81 | + } yield line |
0 commit comments