|
| 1 | +package dotty |
| 2 | +package tools |
| 3 | +package scripting |
| 4 | + |
| 5 | +import java.io.File |
| 6 | +import java.nio.file.{Files, Paths, Path} |
| 7 | + |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +import vulpix.TestConfiguration |
| 11 | + |
| 12 | +import scala.sys.process._ |
| 13 | +import dotty.tools.dotc.config.Properties._ |
| 14 | + |
| 15 | +/** Runs all tests contained in `compiler/test-resources/scripting/` */ |
| 16 | +class ClasspathTests: |
| 17 | + extension (str: String) def dropExtension = |
| 18 | + str.reverse.dropWhile(_ != '.').drop(1).reverse |
| 19 | + |
| 20 | + extension(f: File) def absPath = |
| 21 | + f.getAbsolutePath.replace('\\','/') |
| 22 | + |
| 23 | + // only interested in classpath test scripts |
| 24 | + def testFiles = scripts("/scripting").filter { _.getName.matches("classpath.*[.]sc") } |
| 25 | + |
| 26 | + /* |
| 27 | + * Call test scripts |
| 28 | + */ |
| 29 | + @Test def scriptingJarTest = |
| 30 | + for scriptFile <- testFiles do |
| 31 | + val relpath = scriptFile.toPath.relpath.norm |
| 32 | + printf("===> test script name [%s]\n",relpath) |
| 33 | + printf("%s\n",relpath) |
| 34 | + val bashExe = which("bash") |
| 35 | + printf("bash is [%s]\n",bashExe) |
| 36 | + |
| 37 | + // ask [dist/bin/scalac] to echo generated command line so we can verify some things |
| 38 | + val cmd = Array(bashExe,"-c",s"SCALAC_ECHO_TEST=1 dist/target/pack/bin/scala -classpath '*/lib' $relpath") |
| 39 | + cmd.foreach { printf("cmd[%s]\n",_) } |
| 40 | + val javaCommandLine = exec(cmd:_*).mkString(" ").split(" ").filter { _.trim.nonEmpty } |
| 41 | + val args = scala.collection.mutable.Queue(javaCommandLine:_*) |
| 42 | + args.dequeueWhile( _ != "dotty.tools.scripting.Main") |
| 43 | + |
| 44 | + def consumeNext = args.dequeue() |
| 45 | + |
| 46 | + // assert that we found "dotty.tools.scripting.Main" |
| 47 | + assert(consumeNext == "dotty.tools.scripting.Main") |
| 48 | + val mainArgs = args.copyToArray(Array.ofDim[String](args.length)) |
| 49 | + |
| 50 | + // display command line starting with "dotty.tools.scripting.Main" |
| 51 | + args.foreach { line => |
| 52 | + printf("%s\n",line) |
| 53 | + } |
| 54 | + |
| 55 | + // expecting -classpath next |
| 56 | + assert(consumeNext.replaceAll("'","") == "-classpath") |
| 57 | + |
| 58 | + // 2nd arg to scripting.Main is 'lib/*', with semicolon added if Windows jdk |
| 59 | + |
| 60 | + // PR #10761: verify that [dist/bin/scala] -classpath processing adds $psep to wildcard if Windows |
| 61 | + val classpathValue = consumeNext |
| 62 | + assert( !isWin || classpathValue.contains(psep) ) |
| 63 | + |
| 64 | + // expecting -script next |
| 65 | + assert(consumeNext.replaceAll("'","") == "-script") |
| 66 | + |
| 67 | + // PR #10761: verify that Windows jdk did not expand single wildcard classpath to multiple file paths |
| 68 | + if javaCommandLine.last != relpath then |
| 69 | + printf("last: %s\nrelp: %s\n",javaCommandLine.last,relpath) |
| 70 | + assert(javaCommandLine.last == relpath,s"unexpected args passed to scripting.Main") |
| 71 | + |
| 72 | + |
| 73 | +lazy val cwd = Paths.get(".").toAbsolutePath |
| 74 | + |
| 75 | +import scala.jdk.CollectionConverters._ |
| 76 | +lazy val env:Map[String,String] = System.getenv.asScala.toMap |
| 77 | + |
| 78 | +def exec(cmd: String *): Seq[String] = Process(cmd).lazyLines_!.toList |
| 79 | + |
| 80 | +def which(str:String) = |
| 81 | + var out = "" |
| 82 | + path.find { entry => |
| 83 | + val it = Paths.get(entry).toAbsolutePath |
| 84 | + it.toFile.isDirectory && { |
| 85 | + var testpath = s"$it/$str".norm |
| 86 | + val test = Paths.get(testpath) |
| 87 | + if test.toFile.exists then |
| 88 | + out = testpath |
| 89 | + true |
| 90 | + else |
| 91 | + val test = Paths.get(s"$it/$str.exe".norm) |
| 92 | + if test.toFile.exists then |
| 93 | + out = testpath |
| 94 | + true |
| 95 | + else |
| 96 | + false |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + out |
| 101 | + |
| 102 | +def bashExe = which("bash") |
| 103 | +def path = envOrElse("PATH","").split(psep).toList |
| 104 | +def psep = sys.props("path.separator") |
| 105 | + |
| 106 | +extension(p:Path) |
| 107 | + def relpath: Path = cwd.relativize(p) |
| 108 | + def norm: String = p.toString.replace('\\','/') |
| 109 | + |
| 110 | +extension(path: String) |
| 111 | + // Normalize path separator, convert relative path to absolute |
| 112 | + def norm: String = |
| 113 | + path.replace('\\', '/') match { |
| 114 | + case s if s.secondChar == ":" => s // .drop(2) // path without drive letter |
| 115 | + case s if s.startsWith("./") => s.drop(2) |
| 116 | + case s => s |
| 117 | + } |
| 118 | + |
| 119 | + def parent: String = norm.replaceAll("/[^/]*$","") |
| 120 | + |
| 121 | + // convert to absolute path relative to cwd. |
| 122 | + def absPath: String = norm match |
| 123 | + case str if str.isAbsolute => norm |
| 124 | + case _ => Paths.get(userDir,norm).toString.norm |
| 125 | + |
| 126 | + def isDir: Boolean = Files.isDirectory(Paths.get(path)) |
| 127 | + |
| 128 | + def toUrl: String = Paths.get(absPath).toUri.toURL.toString |
| 129 | + |
| 130 | + // Treat norm paths with a leading '/' as absolute. |
| 131 | + // Windows java.io.File#isAbsolute treats them as relative. |
| 132 | + def isAbsolute = path.norm.startsWith("/") || (isWin && path.secondChar == ":") |
| 133 | + def secondChar: String = path.take(2).drop(1).mkString("") |
| 134 | + |
0 commit comments