Skip to content

Commit fc899a6

Browse files
committed
improve cleanup after running scala cli tests
1 parent 2b95324 commit fc899a6

10 files changed

+106
-9
lines changed

bin/scala

+34-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,37 @@
22

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

5-
"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" "--power" "$@" "--offline" "--server=false"
5+
scala_args() {
6+
7+
declare -a CLI_ARGS
8+
declare -a SCRIPT_ARGS
9+
declare DISABLE_BLOOP=1
10+
11+
while (( "$#" )); do
12+
case "$1" in
13+
"--")
14+
shift
15+
SCRIPT_ARGS+=("--")
16+
SCRIPT_ARGS+=("$@")
17+
break
18+
;;
19+
"clean")
20+
CLI_ARGS+=("$1")
21+
DISABLE_BLOOP=0 # clean command should not add --offline --server=false
22+
shift
23+
;;
24+
*)
25+
CLI_ARGS+=("$1")
26+
shift
27+
;;
28+
esac
29+
done
30+
31+
if [ $DISABLE_BLOOP -eq 1 ]; then
32+
CLI_ARGS+=("--offline" "--server=false")
33+
fi
34+
35+
echo "--power ${CLI_ARGS[@]} ${SCRIPT_ARGS[@]}"
36+
}
37+
38+
"$ROOT/bin/common" "$ROOT/dist/target/pack/bin/scala" $(scala_args "$@")

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import ScriptTestEnv.*
1616
class BashExitCodeTests:
1717
private var myTmpDir: String | Null = null
1818
private lazy val tmpDir = { myTmpDir = Files.createTempDirectory("exit-code-tests").toFile.absPath; myTmpDir }
19-
@After def cleanup(): Unit = if myTmpDir != null then io.Directory(myTmpDir).deleteRecursively()
19+
@After def cleanup(): Unit = {
20+
if myTmpDir != null then io.Directory(myTmpDir).deleteRecursively()
21+
22+
cleanupScalaCLIDirs()
23+
}
2024

2125
/** Verify the exit code of running `cmd args*`. */
2226
def verifyExit(cmd: String, args: String*)(expectedExitCode: Int): Unit =

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ object BashScriptsTests:
2525
def testFiles = scripts("/scripting")
2626

2727
@AfterClass def cleanup: Unit = {
28+
cleanupScalaCLIDirs()
29+
2830
val af = argsfile.toFile
29-
if (af.exists) {
31+
if af.exists then
3032
af.delete()
31-
}
3233
}
34+
3335
printf("osname[%s]\n", osname)
3436
printf("uname[%s]\n", ostypeFull)
3537
printf("using JAVA_HOME=%s\n", envJavaHome)

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import org.junit.{Test, Ignore, AfterClass}
1111
import vulpix.TestConfiguration
1212
import ScriptTestEnv.*
1313

14-
/** Test java command line generated by bin/scala and bin/scalac */
14+
object ClasspathTests:
15+
@AfterClass def cleanup: Unit = {
16+
cleanupScalaCLIDirs()
17+
}
1518

19+
/** Test java command line generated by bin/scala and bin/scalac */
1620
class ClasspathTests:
1721
/*
1822
* Test disabled (temporarily).

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

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class ExpressionTest:
5555

5656
object ExpressionTest:
5757

58+
@AfterClass def cleanup(): Unit = {
59+
cleanupScalaCLIDirs()
60+
}
61+
5862
def main(args: Array[String]): Unit =
5963
val tests = new ExpressionTest
6064
println("\n=== verifyCommandLineExpression ===")

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

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ object ScriptTestEnv {
2828
def whichJava: String = whichExe("java")
2929
def whichBash: String = whichExe("bash")
3030

31+
def cleanupScalaCLIDirs(): Unit = {
32+
val scriptingDir = io.Directory(scriptsDir("/scripting").getPath)
33+
val dottyDir = io.Directory(workingDirectory)
34+
35+
val residueDirs = Seq(
36+
(scriptingDir / ".bsp"),
37+
(scriptingDir / ".scala-build"),
38+
(dottyDir / ".scala-build")
39+
)
40+
41+
for f <- residueDirs do
42+
f.deleteRecursively()
43+
44+
val bspDir = dottyDir / ".bsp"
45+
(bspDir / "scala.json").delete()
46+
if bspDir.isEmpty then bspDir.delete()
47+
}
48+
3149
lazy val workingDirectory: String = {
3250
val dirstr = if testCwd.nonEmpty then
3351
if verbose then printf("TEST_CWD set to [%s]\n", testCwd)

compiler/test/dotty/tools/utils.scala

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ import dotc.config.CommandLineParser
2020
object Dummy
2121

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

30+
def scriptsDir(path: String): File = {
31+
val dir = new File(Dummy.getClass.getResource(path).getPath)
32+
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
33+
dir
34+
}
35+
3136
extension (f: File) def absPath =
3237
f.getAbsolutePath.replace('\\', '/')
3338

@@ -101,10 +106,10 @@ def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,S
101106
case toolArg(name, args) => List((name, args))
102107
case _ => Nil
103108
} ++
104-
lines.flatMap {
109+
lines.flatMap {
105110
case directiveOptionsArg(args) => List(("scalac", args))
106111
case directiveJavacOptions(args) => List(("javac", args))
107-
case _ => Nil
112+
case _ => Nil
108113
}
109114

110115
import org.junit.Test

project/scripts/bootstrappedOnlyCmdTests

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ grep -qe "See 'scala <command> --help' to read about a specific subcommand." "$t
101101

102102
./bin/scala -d hello.jar tests/run/hello.scala
103103
ls hello.jar
104+
clear_cli_dotfiles tests/run
105+
106+
# check that `scala` runs scripts with args
107+
echo "testing ./bin/scala with arguments"
108+
./bin/scala run project/scripts/echoArgs.sc -- abc true 123 > "$tmp"
109+
test "$EXPECTED_OUTPUT_ARGS" = "$(cat "$tmp")"
110+
clear_cli_dotfiles project/scripts
104111

105112
echo "testing i12973"
106113
clear_out "$OUT"

project/scripts/cmdTestsCommon.inc.sh

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SOURCE="tests/pos/HelloWorld.scala"
99
MAIN="HelloWorld"
1010
TASTY="HelloWorld.tasty"
1111
EXPECTED_OUTPUT="hello world"
12+
EXPECTED_OUTPUT_ARGS="[0:abc],[1:true],[2:123]"
1213

1314
OUT=$(mktemp -d)
1415
OUT1=$(mktemp -d)
@@ -24,3 +25,16 @@ clear_out()
2425
local out="$1"
2526
rm -rf "$out"/*
2627
}
28+
29+
clear_cli_dotfiles()
30+
{
31+
local out="$1"
32+
rm -rf "$out"/.bsp
33+
rm -rf "$out"/.scala-build
34+
35+
rm -f "$ROOT"/.bsp/scala.json
36+
if [ -z "$(ls -A "$ROOT"/.bsp)" ]; then
37+
rm -rf "$ROOT"/.bsp
38+
fi
39+
rm -rf "$ROOT"/.scala-build
40+
}

project/scripts/echoArgs.sc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is a Scala CLI script
2+
3+
val formatted =
4+
(for (arg, i) <- args.zipWithIndex yield
5+
s"[$i:$arg]").mkString(",")
6+
println(formatted)

0 commit comments

Comments
 (0)