Skip to content

Commit

Permalink
improve cleanup after running scala cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed May 10, 2024
1 parent 2b95324 commit fc899a6
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 9 deletions.
35 changes: 34 additions & 1 deletion bin/scala
Expand Up @@ -2,4 +2,37 @@

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

"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" "--power" "$@" "--offline" "--server=false"
scala_args() {

declare -a CLI_ARGS
declare -a SCRIPT_ARGS
declare DISABLE_BLOOP=1

while (( "$#" )); do
case "$1" in
"--")
shift
SCRIPT_ARGS+=("--")
SCRIPT_ARGS+=("$@")
break
;;
"clean")
CLI_ARGS+=("$1")
DISABLE_BLOOP=0 # clean command should not add --offline --server=false
shift
;;
*)
CLI_ARGS+=("$1")
shift
;;
esac
done

if [ $DISABLE_BLOOP -eq 1 ]; then
CLI_ARGS+=("--offline" "--server=false")
fi

echo "--power ${CLI_ARGS[@]} ${SCRIPT_ARGS[@]}"
}

"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" $(scala_args "$@")
6 changes: 5 additions & 1 deletion compiler/test/dotty/tools/scripting/BashExitCodeTests.scala
Expand Up @@ -16,7 +16,11 @@ import ScriptTestEnv.*
class BashExitCodeTests:
private var myTmpDir: String | Null = null
private lazy val tmpDir = { myTmpDir = Files.createTempDirectory("exit-code-tests").toFile.absPath; myTmpDir }
@After def cleanup(): Unit = if myTmpDir != null then io.Directory(myTmpDir).deleteRecursively()
@After def cleanup(): Unit = {
if myTmpDir != null then io.Directory(myTmpDir).deleteRecursively()

cleanupScalaCLIDirs()
}

/** Verify the exit code of running `cmd args*`. */
def verifyExit(cmd: String, args: String*)(expectedExitCode: Int): Unit =
Expand Down
6 changes: 4 additions & 2 deletions compiler/test/dotty/tools/scripting/BashScriptsTests.scala
Expand Up @@ -25,11 +25,13 @@ object BashScriptsTests:
def testFiles = scripts("/scripting")

@AfterClass def cleanup: Unit = {
cleanupScalaCLIDirs()

val af = argsfile.toFile
if (af.exists) {
if af.exists then
af.delete()
}
}

printf("osname[%s]\n", osname)
printf("uname[%s]\n", ostypeFull)
printf("using JAVA_HOME=%s\n", envJavaHome)
Expand Down
6 changes: 5 additions & 1 deletion compiler/test/dotty/tools/scripting/ClasspathTests.scala
Expand Up @@ -11,8 +11,12 @@ import org.junit.{Test, Ignore, AfterClass}
import vulpix.TestConfiguration
import ScriptTestEnv.*

/** Test java command line generated by bin/scala and bin/scalac */
object ClasspathTests:
@AfterClass def cleanup: Unit = {
cleanupScalaCLIDirs()
}

/** Test java command line generated by bin/scala and bin/scalac */
class ClasspathTests:
/*
* Test disabled (temporarily).
Expand Down
4 changes: 4 additions & 0 deletions compiler/test/dotty/tools/scripting/ExpressionTest.scala
Expand Up @@ -55,6 +55,10 @@ class ExpressionTest:

object ExpressionTest:

@AfterClass def cleanup(): Unit = {
cleanupScalaCLIDirs()
}

def main(args: Array[String]): Unit =
val tests = new ExpressionTest
println("\n=== verifyCommandLineExpression ===")
Expand Down
18 changes: 18 additions & 0 deletions compiler/test/dotty/tools/scripting/ScriptTestEnv.scala
Expand Up @@ -28,6 +28,24 @@ object ScriptTestEnv {
def whichJava: String = whichExe("java")
def whichBash: String = whichExe("bash")

def cleanupScalaCLIDirs(): Unit = {
val scriptingDir = io.Directory(scriptsDir("/scripting").getPath)
val dottyDir = io.Directory(workingDirectory)

val residueDirs = Seq(
(scriptingDir / ".bsp"),
(scriptingDir / ".scala-build"),
(dottyDir / ".scala-build")
)

for f <- residueDirs do
f.deleteRecursively()

val bspDir = dottyDir / ".bsp"
(bspDir / "scala.json").delete()
if bspDir.isEmpty then bspDir.delete()
}

lazy val workingDirectory: String = {
val dirstr = if testCwd.nonEmpty then
if verbose then printf("TEST_CWD set to [%s]\n", testCwd)
Expand Down
13 changes: 9 additions & 4 deletions compiler/test/dotty/tools/utils.scala
Expand Up @@ -20,14 +20,19 @@ import dotc.config.CommandLineParser
object Dummy

def scripts(path: String): Array[File] = {
val dir = new File(Dummy.getClass.getResource(path).getPath)
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
val dir = scriptsDir(path)
dir.listFiles.filter { f =>
val path = if f.isDirectory then f.getPath + "/" else f.getPath
Properties.testsFilter.isEmpty || Properties.testsFilter.exists(path.contains)
}
}

def scriptsDir(path: String): File = {
val dir = new File(Dummy.getClass.getResource(path).getPath)
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
dir
}

extension (f: File) def absPath =
f.getAbsolutePath.replace('\\', '/')

Expand Down Expand Up @@ -101,10 +106,10 @@ def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,S
case toolArg(name, args) => List((name, args))
case _ => Nil
} ++
lines.flatMap {
lines.flatMap {
case directiveOptionsArg(args) => List(("scalac", args))
case directiveJavacOptions(args) => List(("javac", args))
case _ => Nil
case _ => Nil
}

import org.junit.Test
Expand Down
7 changes: 7 additions & 0 deletions project/scripts/bootstrappedOnlyCmdTests
Expand Up @@ -101,6 +101,13 @@ grep -qe "See 'scala <command> --help' to read about a specific subcommand." "$t

./bin/scala -d hello.jar tests/run/hello.scala
ls hello.jar
clear_cli_dotfiles tests/run

# check that `scala` runs scripts with args
echo "testing ./bin/scala with arguments"
./bin/scala run project/scripts/echoArgs.sc -- abc true 123 > "$tmp"
test "$EXPECTED_OUTPUT_ARGS" = "$(cat "$tmp")"
clear_cli_dotfiles project/scripts

echo "testing i12973"
clear_out "$OUT"
Expand Down
14 changes: 14 additions & 0 deletions project/scripts/cmdTestsCommon.inc.sh
Expand Up @@ -9,6 +9,7 @@ SOURCE="tests/pos/HelloWorld.scala"
MAIN="HelloWorld"
TASTY="HelloWorld.tasty"
EXPECTED_OUTPUT="hello world"
EXPECTED_OUTPUT_ARGS="[0:abc],[1:true],[2:123]"

OUT=$(mktemp -d)
OUT1=$(mktemp -d)
Expand All @@ -24,3 +25,16 @@ clear_out()
local out="$1"
rm -rf "$out"/*
}

clear_cli_dotfiles()
{
local out="$1"
rm -rf "$out"/.bsp
rm -rf "$out"/.scala-build

rm -f "$ROOT"/.bsp/scala.json
if [ -z "$(ls -A "$ROOT"/.bsp)" ]; then
rm -rf "$ROOT"/.bsp
fi
rm -rf "$ROOT"/.scala-build
}
6 changes: 6 additions & 0 deletions project/scripts/echoArgs.sc
@@ -0,0 +1,6 @@
// This is a Scala CLI script

val formatted =
(for (arg, i) <- args.zipWithIndex yield
s"[$i:$arg]").mkString(",")
println(formatted)

0 comments on commit fc899a6

Please sign in to comment.