Skip to content

Commit

Permalink
adjust test sources to be clear which are meant to be self-executing
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed May 10, 2024
1 parent bb166c9 commit 2b95324
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 55 deletions.
9 changes: 0 additions & 9 deletions compiler/test-resources/scripting/argfileClasspath.sc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bin/scala

// This file is a Scala CLI script.
import java.nio.file.Paths

// def main(args: Array[String]): Unit = // MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
Expand Down
1 change: 0 additions & 1 deletion compiler/test-resources/scripting/cpArgumentsFile.txt

This file was deleted.

2 changes: 2 additions & 0 deletions compiler/test-resources/scripting/envtest.sc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

def main(args: Array[String]): Unit =
println("Hello " + util.Properties.propOrNull("key"))
2 changes: 0 additions & 2 deletions compiler/test-resources/scripting/envtestNu.sc

This file was deleted.

3 changes: 3 additions & 0 deletions compiler/test-resources/scripting/envtest_scalacli.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is a Scala CLI script.

println("Hello " + util.Properties.propOrNull("key"))
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/hashBang.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env scala
#!/usr/bin/env fake-program-to-test-hashbang-removal
# comment
STUFF=nada
!#
Expand Down
4 changes: 2 additions & 2 deletions compiler/test-resources/scripting/hashBang.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env scala
#!/usr/bin/env fake-program-to-test-hashbang-removal
# comment
STUFF=nada
!#

// everything above this point should be ignored by the compiler
def main(args: Array[String]): Unit =
System.err.printf("mainClassFromStack: %s\n",mainFromStack)
assert(mainFromStack.contains("hashBang"),s"fromStack[$mainFromStack]")
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/scriptName.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env scala
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

def main(args: Array[String]): Unit =
val name = Option(sys.props("script.name")) match {
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/scriptPath.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!dist/target/pack/bin/scala
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

def main(args: Array[String]): Unit =
args.zipWithIndex.foreach { case (arg,i) => printf("arg %d: [%s]\n",i,arg) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

args.zipWithIndex.foreach { case (arg,i) => printf("arg %d: [%s]\n",i,arg) }

if !scriptPath.endsWith("scriptPathNu.sc") then
if !scriptPath.endsWith("scriptPath_scalacli.sc") then
printf( s"incorrect script.path defined as [$scriptPath]")
else
printf("scriptPath: %s\n", scriptPath) // report the value
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/showArgs.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bin/scala
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

// precise output format expected by BashScriptsTests.scala
def main(args: Array[String]): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bin/scala

// This file is a Scala CLI script.

// precise output format expected by BashScriptsTests.scala
// MIGRATION: Scala CLI expects `*.sc` files to be straight-line code
for (a,i) <- args.zipWithIndex do
printf(s"arg %2d:[%s]\n",i,a)
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/sqlDateError.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bin/scala
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

def main(args: Array[String]): Unit = {
println(new java.sql.Date(100L))
Expand Down
6 changes: 0 additions & 6 deletions compiler/test-resources/scripting/sqlDateErrorNu.sc

This file was deleted.

6 changes: 6 additions & 0 deletions compiler/test-resources/scripting/sqlDateError_scalacli.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bin/scala

// This file is a Scala CLI script.

println(new java.sql.Date(100L))
System.err.println("SCALA_OPTS="+Option(System.getenv("SCALA_OPTS")).getOrElse(""))
2 changes: 1 addition & 1 deletion compiler/test-resources/scripting/touchFile.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env scala
// this file is intended to be ran as an argument to the dotty.tools.scripting.ScriptingDriver class

import java.io.File

Expand Down
6 changes: 0 additions & 6 deletions compiler/test-resources/scripting/unglobClasspath.sc

This file was deleted.

9 changes: 9 additions & 0 deletions compiler/test-resources/scripting/unglobClasspath_scalacli.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is a Scala CLI script.

import dotty.tools.tasty.TastyFormat
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// not visible on default classpath, "compiler/test/dotty/tools/scripting/ClasspathTests.scala"
// adds it to classpath via a compiler argument `-classpath 'org/scala-lang/tasty-core_3/$VERSION/*'`

val cp = sys.props("java.class.path")
printf("unglobbed classpath: %s\n", cp)
14 changes: 7 additions & 7 deletions compiler/test/dotty/tools/scripting/BashScriptsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object BashScriptsTests:
val testScriptArgs = Seq(
"a", "b", "c", "-repl", "-run", "-script", "-debug"
)
val Seq(showArgsScript, showArgsScalaCli) = Seq("showArgs.sc", "showArgsNu.sc").map { name =>
val Seq(showArgsScript, showArgsScalaCli) = Seq("showArgs.sc", "showArgs_scalacli.sc").map { name =>
testFiles.find(_.getName == name).get.absPath
}

Expand All @@ -66,7 +66,7 @@ object BashScriptsTests:
}
file

val Seq(envtestNuSc, envtestScala) = Seq("envtestNu.sc", "envtest.scala").map { testFile(_) }
val Seq(envtestNuSc, envtestScala) = Seq("envtest_scalacli.sc", "envtest.scala").map { testFile(_) }

// create command line with given options, execute specified script, return stdout
def callScript(tag: String, script: String, keyPre: String): String =
Expand Down Expand Up @@ -173,13 +173,13 @@ class BashScriptsTests:
assert(stdout == expectedOutput)

/*
* verify that scriptPathNu.sc sees a valid script.path property,
* and that it's value is the path to "scriptPathNu.sc".
* verify that scriptPath_scalacli.sc sees a valid script.path property,
* and that it's value is the path to "scriptPath_scalacli.sc".
*/
@Category(Array(classOf[BootstrappedOnlyTests]))
@Test def verifyScriptPathProperty =
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
val scriptFile = testFiles.find(_.getName == "scriptPathNu.sc").get
val scriptFile = testFiles.find(_.getName == "scriptPath_scalacli.sc").get
val expected = s"${scriptFile.getName}"
printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
printf("calling scriptFile: %s\n", scriptFile)
Expand All @@ -196,7 +196,7 @@ class BashScriptsTests:
*/
@Test def verifyScalaOpts =
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
val scriptFile = testFiles.find(_.getName == "classpathReport.sc").get
val scriptFile = testFiles.find(_.getName == "classpathReport_scalacli.sc").get
printf("===> verify SCALA_OPTS='@argsfile' is properly handled by `dist/bin/scala`\n")
val envPairs = List(("SCALA_OPTS", s"@$argsfile"))
val (validTest, exitCode, stdout, stderr) = bashCommand(scriptFile.absPath, envPairs)
Expand All @@ -219,7 +219,7 @@ class BashScriptsTests:
*/
@Test def sqlDateTest =
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
val scriptBase = "sqlDateErrorNu"
val scriptBase = "sqlDateError_scalacli"
val scriptFile = testFiles.find(_.getName == s"$scriptBase.sc").get
val testJar = testFile(s"$scriptBase.jar") // jar should not be created when scriptFile runs
val tj = Paths.get(testJar).toFile
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/dotty/tools/scripting/ClasspathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ClasspathTests:
@Ignore
@Test def hashbangClasspathVerifyTest = {
// only interested in classpath test scripts
val testScriptName = "classpathReport.sc"
val testScriptName = "classpathReport_scalacli.sc"
val testScript = scripts("/scripting").find { _.getName.matches(testScriptName) } match
case None => sys.error(s"test script not found: ${testScriptName}")
case Some(file) => file
Expand All @@ -39,7 +39,7 @@ class ClasspathTests:

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

// classpathReport.sc is expected to produce two lines:
// classpathReport_scalacli.sc is expected to produce two lines:
// cwd: <current-working-directory-seen-by-the-script>
// classpath: <classpath-seen-by-the-script>

Expand All @@ -51,7 +51,7 @@ class ClasspathTests:
// convert scriptCp to a list of files
val hashbangJars: List[File] = scriptCp.split(psep).map { _.toFile }.toList
val hashbangClasspathJars = hashbangJars.map { _.name }.sorted.distinct // get jar basenames, remove duplicates
val packlibDir: String = ??? /* ??? was s"$scriptCwd/$packLibDir" */ // classpathReport.sc specifies a wildcard classpath in this directory
val packlibDir: String = ??? /* ??? was s"$scriptCwd/$packLibDir" */ // classpathReport_scalacli.sc specifies a wildcard classpath in this directory
val packlibJars: List[File] = listJars(packlibDir) // classpath entries expected to have been reported by the script

printf("%d jar files in dist/target/pack/lib\n", packlibJars.size)
Expand All @@ -78,7 +78,7 @@ class ClasspathTests:
* verify classpath is unglobbed by MainGenericRunner.
*/
@Test def unglobClasspathVerifyTest = {
val testScriptName = "unglobClasspath.sc"
val testScriptName = "unglobClasspath_scalacli.sc"
val testScript = scripts("/scripting").find { _.name.matches(testScriptName) } match
case None => sys.error(s"test script not found: ${testScriptName}")
case Some(file) => file
Expand Down
16 changes: 7 additions & 9 deletions compiler/test/dotty/tools/scripting/ScriptingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import org.junit.Assume.assumeFalse
/** Runs all tests contained in `compiler/test-resources/scripting/` */
class ScriptingTests:
// classpath tests managed by scripting.ClasspathTests.scala
def testFiles = scripts("/scripting").filter { ! _.getName.toLowerCase.contains("classpath") }
def testFiles = scripts("/scripting").filter { sc =>
val name = sc.getName.toLowerCase
!name.contains("classpath")
&& !name.contains("_scalacli")
}

/*
* Call .scala scripts without -save option, verify no jar created
Expand Down Expand Up @@ -47,10 +51,7 @@ class ScriptingTests:
*/
@Test def scriptingMainTests =
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
for
(scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc")
if !scriptFile.getName().endsWith("Nu.sc")
do
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
showScriptUnderTest(scriptFile)
val unexpectedJar = script2jar(scriptFile)
unexpectedJar.delete
Expand All @@ -69,10 +70,7 @@ class ScriptingTests:
*/
@Test def scriptingJarTest =
assumeFalse("Scripts do not yet support Scala 2 library TASTy", Properties.usingScalaLibraryTasty)
for
(scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc")
if !scriptFile.getName().endsWith("Nu.sc")
do
for (scriptFile, scriptArgs) <- scalaFilesWithArgs(".sc") do
showScriptUnderTest(scriptFile)
val expectedJar = script2jar(scriptFile)
expectedJar.delete
Expand Down

0 comments on commit 2b95324

Please sign in to comment.