Skip to content

Commit 897d2ad

Browse files
committed
fix bash script tests
1 parent 84b2e66 commit 897d2ad

15 files changed

+108
-38
lines changed

bin/scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >& /dev/null && pwd)/.."
44

5-
"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" "$@"
5+
"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" "$@" "--offline" "--server=false"

compiler/test-resources/scripting/classpathReport.sc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!bin/scala -classpath 'dist/target/pack/lib/*'
1+
#!/usr/bin/env bin/scala
22

33
import java.nio.file.Paths
44

5-
def main(args: Array[String]): Unit =
5+
// def main(args: Array[String]): Unit = // MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
66
val cwd = Paths.get(".").toAbsolutePath.normalize.toString.norm
77
printf("cwd: %s\n", cwd)
88
printf("classpath: %s\n", sys.props("java.class.path").norm)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
2+
println("Hello " + util.Properties.propOrNull("key"))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bin/scala
2+
3+
// THIS FILE IS RAN WITH SCALA CLI, which wraps scripts exposing scriptPath and args variables
4+
5+
args.zipWithIndex.foreach { case (arg,i) => printf("arg %d: [%s]\n",i,arg) }
6+
7+
if !scriptPath.endsWith("scriptPathNu.sc") then
8+
printf( s"incorrect script.path defined as [$scriptPath]")
9+
else
10+
printf("scriptPath: %s\n", scriptPath) // report the value
11+
12+
extension(s: String)
13+
def norm: String = s.replace('\\', '/')

compiler/test-resources/scripting/showArgs.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env scala
1+
#!/usr/bin/env bin/scala
22

33
// precise output format expected by BashScriptsTests.scala
44
def main(args: Array[String]): Unit =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bin/scala
2+
3+
// precise output format expected by BashScriptsTests.scala
4+
// MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
5+
for (a,i) <- args.zipWithIndex do
6+
printf(s"arg %2d:[%s]\n",i,a)

compiler/test-resources/scripting/sqlDateError.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!bin/scala
1+
#!/usr/bin/env bin/scala
22

33
def main(args: Array[String]): Unit = {
44
println(new java.sql.Date(100L))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bin/scala
2+
3+
// def main(args: Array[String]): Unit = { MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
4+
println(new java.sql.Date(100L))
5+
System.err.println("SCALA_OPTS="+Option(System.getenv("SCALA_OPTS")).getOrElse(""))
6+
// }
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#!bin/scala -classpath 'dist/target/pack/lib/*'
1+
// won't compile unless classpath is set correctly
2+
import dotty.tools.tasty.TastyFormat
23

3-
// won't compile unless the hashbang line sets classpath
4-
import org.jline.terminal.Terminal
5-
6-
def main(args: Array[String]) =
4+
// def main(args: Array[String]) = // MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
75
val cp = sys.props("java.class.path")
86
printf("unglobbed classpath: %s\n", cp)

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BashExitCodeTests:
2929
}, expectedExitCode, exitCode)
3030

3131
// Helpers for running scala, scalac, and scalac without the the output directory ("raw")
32-
def scala(args: String*) = verifyExit(scalaPath, args*)
32+
def scala(args: String*) = verifyExit(scalaPath, ("--offline" +: "--server=false" +: args)*)
3333
def scalacRaw(args: String*) = verifyExit(scalacPath, args*)
3434
def scalac(args: String*) = scalacRaw(("-d" +: tmpDir +: args)*)
3535

@@ -38,12 +38,16 @@ class BashExitCodeTests:
3838
Files.write(Files.createTempFile(tmpDir.toPath, getClass.getSimpleName, suffix), body.getBytes(UTF_8)).absPath
3939

4040
@Test def neg = scalac(f("@main def Test = prin"))(1)
41-
@Test def run = scalac(f("@main def Test = ???"))(0) & scala("-classpath", tmpDir, "Test")(1)
42-
@Test def pos = scalac(f("@main def Test = ()"))(0) & scala("-classpath", tmpDir, "Test")(0)
41+
@Test def run = scalac(f("@main def Test = ???"))(0) & scala("-classpath", tmpDir, "-M", "Test")(1)
42+
@Test def pos = scalac(f("@main def Test = ()"))(0) & scala("-classpath", tmpDir, "-M", "Test")(0)
4343

44-
@Test def runNeg = scala(f("@main def Test = prin", ".sc"))(1)
45-
@Test def runRun = scala(f("@main def Test = ???", ".sc"))(1)
46-
@Test def runPos = scala(f("@main def Test = ()", ".sc"))(0)
44+
@Test def runNeg_script = scala(f("prin", ".sc"))(1)
45+
@Test def runRun_script = scala(f("???", ".sc"))(1)
46+
@Test def runPos_script = scala(f("()", ".sc"))(0)
47+
48+
@Test def runNeg = scala(f("@main def Test = prin", ".scala"))(1)
49+
@Test def runRun = scala(f("@main def Test = ???", ".scala"))(1)
50+
@Test def runPos = scala(f("@main def Test = ()", ".scala"))(0)
4751

4852
@Test def scNeg = scalac("-script", f("@main def Test = prin", ".sc"))(1)
4953
@Test def scRun = scalac("-script", f("@main def Test = ???", ".sc"))(1)

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package scripting
55
import scala.language.unsafeNulls
66

77
import java.nio.file.Paths
8-
import org.junit.{Test, AfterClass}
8+
import org.junit.{Test, Ignore, AfterClass}
99
import org.junit.Assert.assertEquals
1010
import org.junit.Assume.assumeFalse
1111
import org.junit.experimental.categories.Category
@@ -50,7 +50,9 @@ object BashScriptsTests:
5050
val testScriptArgs = Seq(
5151
"a", "b", "c", "-repl", "-run", "-script", "-debug"
5252
)
53-
val showArgsScript = testFiles.find(_.getName == "showArgs.sc").get.absPath
53+
val Seq(showArgsScript, showArgsScalaCli) = Seq("showArgs.sc", "showArgsNu.sc").map { name =>
54+
testFiles.find(_.getName == name).get.absPath
55+
}
5456

5557
def testFile(name: String): String =
5658
val file = testFiles.find(_.getName == name) match {
@@ -64,13 +66,13 @@ object BashScriptsTests:
6466
}
6567
file
6668

67-
val Seq(envtestSc, envtestScala) = Seq("envtest.sc", "envtest.scala").map { testFile(_) }
69+
val Seq(envtestNuSc, envtestScala) = Seq("envtestNu.sc", "envtest.scala").map { testFile(_) }
6870

6971
// create command line with given options, execute specified script, return stdout
7072
def callScript(tag: String, script: String, keyPre: String): String =
7173
val keyArg = s"$keyPre=$tag"
7274
printf("pass tag [%s] via [%s] to script [%s]\n", tag, keyArg, script)
73-
val cmd: String = Seq("SCALA_OPTS= ", scalaPath, keyArg, script).mkString(" ")
75+
val cmd: String = Seq("SCALA_OPTS= ", scalaPath, "run", keyArg, "--offline", "--server=false", script).mkString(" ")
7476
printf("cmd: [%s]\n", cmd)
7577
val (validTest, exitCode, stdout, stderr) = bashCommand(cmd)
7678
stderr.filter { !_.contains("Inappropriate ioctl") }.foreach { System.err.printf("stderr [%s]\n", _) }
@@ -84,13 +86,15 @@ class BashScriptsTests:
8486
////////////////////////// begin tests //////////////////////
8587

8688
/* verify that `dist/bin/scala` correctly passes args to the jvm via -J-D for script envtest.sc */
89+
@Ignore // SCALA CLI does not support `-J` to pass java properties, only things like -Xmx5g
8790
@Test def verifyScJProperty =
8891
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
8992
val tag = "World1"
90-
val stdout = callScript(tag, envtestSc, s"-J-Dkey")
93+
val stdout = callScript(tag, envtestNuSc, s"-J-Dkey")
9194
assertEquals( s"Hello $tag", stdout)
9295

9396
/* verify that `dist/bin/scala` correctly passes args to the jvm via -J-D for script envtest.scala */
97+
@Ignore // SCALA CLI does not support `-J` to pass java properties, only things like -Xmx5g
9498
@Test def verifyScalaJProperty =
9599
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
96100
val tag = "World2"
@@ -101,7 +105,7 @@ class BashScriptsTests:
101105
@Test def verifyScDProperty =
102106
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
103107
val tag = "World3"
104-
val stdout = callScript(tag, envtestSc, s"-Dkey")
108+
val stdout = callScript(tag, envtestNuSc, s"-Dkey")
105109
assertEquals(s"Hello $tag", stdout)
106110

107111
/* verify that `dist/bin/scala` can set system properties via -D for envtest.scala */
@@ -114,7 +118,9 @@ class BashScriptsTests:
114118
/* verify that `dist/bin/scala` can set system properties via -D when executing compiled script via -jar envtest.jar */
115119
@Test def saveAndRunWithDProperty =
116120
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
117-
val commandline = Seq("SCALA_OPTS= ", scalaPath.relpath, "-save", envtestScala.relpath).mkString(" ")
121+
val libOut = envtestScala.relpath.stripSuffix(".scala") + ".jar"
122+
val commandline = Seq(
123+
"SCALA_OPTS= ", scalaPath.relpath, "--power", "package", envtestScala.relpath, "-o", libOut, "--library", "--offline", "--server=false").mkString(" ")
118124
val (_, _, _, _) = bashCommand(commandline) // compile jar, discard output
119125
val testJar = testFile("envtest.jar") // jar is created by the previous bashCommand()
120126
if (testJar.isFile){
@@ -124,7 +130,8 @@ class BashScriptsTests:
124130
}
125131

126132
val tag = "World5"
127-
val commandline2 = Seq("SCALA_OPTS= ", scalaPath.relpath, s"-Dkey=$tag", testJar.relpath)
133+
val commandline2 = Seq(
134+
"SCALA_OPTS= ", scalaPath.relpath, "run", s"-Dkey=$tag", "-classpath", testJar.relpath, "--offline", "--server=false")
128135
printf("cmd[%s]\n", commandline2.mkString(" "))
129136
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline2.mkString(" "))
130137
assertEquals(s"Hello $tag", stdout.mkString("/n"))
@@ -148,7 +155,11 @@ class BashScriptsTests:
148155
/* verify `dist/bin/scala` non-interference with command line args following script name */
149156
@Test def verifyScalaArgs =
150157
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
151-
val commandline = (Seq("SCALA_OPTS= ", scalaPath, showArgsScript) ++ testScriptArgs).mkString(" ")
158+
val commandline = (
159+
Seq("SCALA_OPTS= ", scalaPath, showArgsScalaCli)
160+
++ Seq("--offline", "--server=false")
161+
++ ("--" +: testScriptArgs)
162+
).mkString(" ")
152163
val (validTest, exitCode, stdout, stderr) = bashCommand(commandline)
153164
if verifyValid(validTest) then
154165
var fail = false
@@ -162,13 +173,13 @@ class BashScriptsTests:
162173
assert(stdout == expectedOutput)
163174

164175
/*
165-
* verify that scriptPath.sc sees a valid script.path property,
166-
* and that it's value is the path to "scriptPath.sc".
176+
* verify that scriptPathNu.sc sees a valid script.path property,
177+
* and that it's value is the path to "scriptPathNu.sc".
167178
*/
168179
@Category(Array(classOf[BootstrappedOnlyTests]))
169180
@Test def verifyScriptPathProperty =
170181
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
171-
val scriptFile = testFiles.find(_.getName == "scriptPath.sc").get
182+
val scriptFile = testFiles.find(_.getName == "scriptPathNu.sc").get
172183
val expected = s"${scriptFile.getName}"
173184
printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
174185
printf("calling scriptFile: %s\n", scriptFile)
@@ -177,8 +188,8 @@ class BashScriptsTests:
177188
stdout.foreach { printf("stdout: [%s]\n", _) }
178189
stderr.foreach { printf("stderr: [%s]\n", _) }
179190
val valid = stdout.exists { _.endsWith(expected) }
180-
if valid then printf("# valid script.path reported by [%s]\n", scriptFile.getName)
181-
assert(valid, s"script ${scriptFile.absPath} did not report valid script.path value")
191+
if valid then printf("# valid scriptPath reported by [%s]\n", scriptFile.getName)
192+
assert(valid, s"script ${scriptFile.absPath} did not report valid scriptPath value")
182193

183194
/*
184195
* verify SCALA_OPTS can specify an @argsfile when launching a scala script in `dist/bin/scala`.
@@ -208,7 +219,7 @@ class BashScriptsTests:
208219
*/
209220
@Test def sqlDateTest =
210221
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
211-
val scriptBase = "sqlDateError"
222+
val scriptBase = "sqlDateErrorNu"
212223
val scriptFile = testFiles.find(_.getName == s"$scriptBase.sc").get
213224
val testJar = testFile(s"$scriptBase.jar") // jar should not be created when scriptFile runs
214225
val tj = Paths.get(testJar).toFile
@@ -236,7 +247,6 @@ class BashScriptsTests:
236247
printf("===> verify -e <expression> is properly handled by `dist/bin/scala`\n")
237248
val expected = "9"
238249
val expression = s"println(3*3)"
239-
val cmd = s"bin/scala -e $expression"
240250
val (validTest, exitCode, stdout, stderr) = bashCommand(s"""bin/scala -e '$expression'""")
241251
val result = stdout.filter(_.nonEmpty).mkString("")
242252
printf("stdout: %s\n", result)

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ClasspathTests:
5151
// convert scriptCp to a list of files
5252
val hashbangJars: List[File] = scriptCp.split(psep).map { _.toFile }.toList
5353
val hashbangClasspathJars = hashbangJars.map { _.name }.sorted.distinct // get jar basenames, remove duplicates
54-
val packlibDir = s"$scriptCwd/$packLibDir" // classpathReport.sc specifies a wildcard classpath in this directory
54+
val packlibDir: String = ??? /* ??? was s"$scriptCwd/$packLibDir" */ // classpathReport.sc specifies a wildcard classpath in this directory
5555
val packlibJars: List[File] = listJars(packlibDir) // classpath entries expected to have been reported by the script
5656

5757
printf("%d jar files in dist/target/pack/lib\n", packlibJars.size)
@@ -84,11 +84,23 @@ class ClasspathTests:
8484
case Some(file) => file
8585

8686
val relpath = testScript.toPath.relpath.norm
87+
val scalaCommand = scalaPath.relpath.norm
8788
printf("===> unglobClasspathVerifyTest for script [%s]\n", relpath)
8889
printf("bash is [%s]\n", bashExe)
8990

9091
if packBinScalaExists then
91-
val bashCmdline = s"set +x ; SCALA_OPTS= $relpath"
92+
val sv = packScalaVersion
93+
val tastyDirGlob = s"$packMavenDir/org/scala-lang/tasty-core_3/$sv/*"
94+
// ^^^^^^^^^^^^^
95+
// the classpath is a glob pattern that should be unglobbed by scala command,
96+
// otherwise the script could not compile because it references a class
97+
// from tasty-core
98+
99+
val bashCmdline = Seq(
100+
"set +x ;",
101+
"SCALA_OPTS=",
102+
scalaCommand, "run", "--classpath", s"'$tastyDirGlob'", "--offline", "--server=false", relpath
103+
).mkString(" ")
92104
val cmd = Array(bashExe, "-c", bashCmdline)
93105

94106
cmd.foreach { printf("[%s]\n", _) }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ExpressionTest:
4444
assert(success)
4545

4646
def getResult(expression: String): String =
47-
val (_, _, stdout, stderr) = bashCommand(s"$scalaPath -e '$expression'")
47+
val (_, _, stdout, stderr) = bashCommand(s"$scalaPath -e '$expression' --offline --server=false")
4848
printf("stdout: %s\n", stdout.mkString("|"))
4949
printf("stderr: %s\n", stderr.mkString("\n", "\n", ""))
5050
stdout.filter(_.nonEmpty).mkString("")

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,22 @@ object ScriptTestEnv {
125125

126126

127127
def packBinDir = "dist/target/pack/bin"
128-
def packLibDir = "dist/target/pack/lib"
128+
// def packLibDir = "dist/target/pack/lib" // replaced by packMavenDir
129+
def packMavenDir = "dist/target/pack/maven2"
130+
def packVersionFile = "dist/target/pack/VERSION"
129131
def packBinScalaExists: Boolean = Files.exists(Paths.get(s"$packBinDir/scala"))
130132

133+
def packScalaVersion: String = {
134+
val versionFile = Paths.get(packVersionFile)
135+
if Files.exists(versionFile) then
136+
val lines = Files.readAllLines(versionFile).asScala
137+
lines.find { _.startsWith("version:=") } match
138+
case Some(line) => line.drop(9)
139+
case None => sys.error(s"no version:= found in $packVersionFile")
140+
else
141+
sys.error(s"no $packVersionFile found")
142+
}
143+
131144
def listJars(dir: String): List[File] =
132145
val packlibDir = Paths.get(dir).toFile
133146
if packlibDir.isDirectory then

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class ScriptingTests:
4747
*/
4848
@Test def scriptingMainTests =
4949
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
50-
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
50+
for
51+
(scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc")
52+
if !scriptFile.getName().endsWith("Nu.sc")
53+
do
5154
showScriptUnderTest(scriptFile)
5255
val unexpectedJar = script2jar(scriptFile)
5356
unexpectedJar.delete
@@ -66,7 +69,10 @@ class ScriptingTests:
6669
*/
6770
@Test def scriptingJarTest =
6871
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
69-
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
72+
for
73+
(scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc")
74+
if !scriptFile.getName().endsWith("Nu.sc")
75+
do
7076
showScriptUnderTest(scriptFile)
7177
val expectedJar = script2jar(scriptFile)
7278
expectedJar.delete

0 commit comments

Comments
 (0)