Skip to content

Commit 1fae969

Browse files
committed
Handle save scala option, move coursier tests to isolated configuration, apply requested changes
1 parent 216b70b commit 1fae969

File tree

10 files changed

+65
-227
lines changed

10 files changed

+65
-227
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ jobs:
111111

112112
- name: Cmd Tests
113113
run: |
114-
./project/scripts/sbt ";scala3-bootstrapped/compile; scala3-bootstrapped/publishLocal; scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;configureIDE ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test"
114+
./project/scripts/sbt ";scala3-bootstrapped/compile; scala3-bootstrapped/test;sjsSandbox/run;sjsSandbox/test;sjsJUnitTests/test;sjsCompilerTests/test ;sbt-test/scripted scala2-compat/* ;configureIDE ;stdlib-bootstrapped/test:run ;stdlib-bootstrapped-tasty-tests/test; scala3-compiler-bootstrapped/scala3CompilerCoursierTest:test"
115115
./project/scripts/bootstrapCmdTests
116116
117117
- name: MiMa

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ community-build/dotty-community-build-deps
9999

100100
# Coursier
101101
cs
102+
103+
# Coursier test product
104+
compiler/test-coursier/run/myfile.jar

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ case class Settings(
2323
residualArgs: List[String] = List.empty,
2424
scriptArgs: List[String] = List.empty,
2525
targetScript: String = "",
26-
areWithCompiler: Boolean = false,
26+
save: Boolean = false,
2727
) {
2828
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
2929
case ExecuteMode.Guess =>
@@ -47,23 +47,21 @@ case class Settings(
4747
this.copy(exitCode = 2)
4848
end withTargetScript
4949

50-
def withCompiler: Settings =
51-
this.copy(areWithCompiler = true)
50+
def withSave: Settings =
51+
this.copy(save = true)
5252
}
5353

5454
object MainGenericRunner {
5555

56-
final val classpathSeparator = ":"
56+
final val classpathSeparator = File.pathSeparator
5757

5858
@tailrec
5959
def process(args: List[String], settings: Settings): Settings = args match
6060
case Nil =>
6161
settings
62-
case "-repl" :: tail =>
63-
process(tail, settings.withExecuteMode(ExecuteMode.Repl))
6462
case "-run" :: tail =>
6563
process(tail, settings.withExecuteMode(ExecuteMode.Run))
66-
case ("-cp" | "-classpath" | "--classpath") :: cp :: tail =>
64+
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
6765
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
6866
case ("-version" | "--version") :: _ =>
6967
settings.copy(
@@ -78,8 +76,8 @@ object MainGenericRunner {
7876
residualArgs = settings.residualArgs :+ "-verbose"
7977
)
8078
)
81-
case "-with-compiler" :: tail =>
82-
process(tail, settings.withCompiler)
79+
case "-save" :: tail =>
80+
process(tail, settings.withSave)
8381
case arg :: tail =>
8482
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
8583
if arg.endsWith(".scala") || arg.endsWith(".sc") || (line.nonEmpty && raw"#!.*scala".r.matches(line.get)) then
@@ -109,6 +107,7 @@ object MainGenericRunner {
109107
val properArgs =
110108
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
111109
++ settings.residualArgs
110+
++ (if settings.save then List("-save") else Nil)
112111
++ List("-script", settings.targetScript)
113112
++ settings.scriptArgs
114113
scripting.Main.main(properArgs.toArray)

compiler/test/dotty/tools/coursier/CoursierScalaTests.scala renamed to compiler/test-coursier/dotty/tools/coursier/CoursierScalaTests.scala

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package dotty
22
package tools
3-
package scripting
3+
package coursier
44

55
import java.io.File
66
import java.nio.file.{Path, Paths, Files}
77
import scala.sys.process._
8-
98
import org.junit.Test
109
import org.junit.BeforeClass
11-
12-
import vulpix.TestConfiguration
13-
14-
import dotty.tools.absPath
10+
import org.junit.Assert._
1511
import scala.collection.mutable.ListBuffer
1612

13+
import java.net.URLClassLoader
14+
import java.net.URL
15+
1716
class CoursierScalaTests:
1817

18+
private def scripts(path: String): Array[File] = {
19+
val dir = new File(getClass.getResource(path).getPath)
20+
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
21+
dir.listFiles
22+
}
23+
24+
extension (f: File) private def absPath =
25+
f.getAbsolutePath.replace('\\', '/')
26+
27+
extension (str: String) private def dropExtension =
28+
str.reverse.dropWhile(_ != '.').drop(1).reverse
29+
1930
// classpath tests are managed by scripting.ClasspathTests.scala
2031
def testFiles = scripts("/scripting").filter { ! _.getName.startsWith("classpath") }
2132

@@ -38,29 +49,31 @@ class CoursierScalaTests:
3849
)
3950
for (line, expect) <- output zip expectedOutput do
4051
printf("expected: %-17s\nactual : %s\n", expect, line)
41-
assert(output == expectedOutput)
52+
assertEquals(expectedOutput, output)
4253
scriptArgs()
4354

4455
def version() =
4556
val output = CoursierScalaTests.csCmd("-version")
46-
assert(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
57+
assertTrue(output.mkString("\n").contains(sys.env("DOTTY_BOOTSTRAPPED_VERSION")))
4758
version()
4859

4960
def emptyArgsEqualsRepl() =
5061
val output = CoursierScalaTests.csCmd()
51-
assert(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
62+
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
5263
emptyArgsEqualsRepl()
5364

54-
def repl() =
55-
val output = CoursierScalaTests.csCmd("-repl")
56-
assert(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
57-
repl()
58-
5965
def run() =
60-
val output = CoursierScalaTests.csCmd("-run", "-classpath", scripts("/run").head.getParent, "myfile")
61-
assert(output.mkString("\n") == "Hello")
66+
val output = CoursierScalaTests.csCmd("-run", "-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
67+
assertEquals(output.mkString("\n"), "Hello")
6268
run()
6369

70+
def jar() =
71+
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
72+
val output = CoursierScalaTests.csCmd("-save", source.absPath)
73+
assertEquals(output.mkString("\n"), "Hello")
74+
assertTrue(source.getParentFile.listFiles.find(_.getName == "myfile.jar").isDefined)
75+
jar()
76+
6477
object CoursierScalaTests:
6578

6679
def execCmd(command: String, options: String*): List[String] =
@@ -69,7 +82,6 @@ object CoursierScalaTests:
6982
cmd.!(ProcessLogger(out += _, out += _))
7083
out.toList
7184

72-
7385
def csCmd(options: String*): List[String] =
7486
val newOptions = options match
7587
case Nil => options
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
package run
2+
13
object myfile extends App:
24
println("Hello")
-2.16 KB
Binary file not shown.
-664 Bytes
Binary file not shown.
-514 Bytes
Binary file not shown.

dist/bin/scala

100644100755
Lines changed: 5 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -28,205 +28,11 @@ fi
2828

2929
source "$PROG_HOME/bin/common"
3030

31-
# This script operates in one of 3 usage modes:
32-
# script
33-
# repl
34-
# run
35-
# execute_mode replaces mutually exclusive booleans:
36-
# execute_repl=false
37-
# execute_run=false
38-
# execute_script=false
39-
setExecuteMode () {
40-
case "${execute_mode-}" in
41-
"") execute_mode="$1" ; shift ;;
42-
*) echo "execute_mode==[${execute_mode-}], attempted overwrite by [$1]" 1>&2
43-
exit 1
44-
;;
45-
esac
46-
}
31+
# exec here would prevent onExit from being called, leaving terminal in unusable state
32+
compilerJavaClasspathArgs
33+
echo $jvm_cp_args
34+
eval "\"$JAVACMD\"" "-classpath \"$jvm_cp_args\"" "dotty.tools.MainGenericRunner" "-classpath \"$jvm_cp_args\"" "$@"
35+
scala_exit_status=$?
4736

4837

49-
with_compiler=false # to add compiler jars to repl classpath
50-
let class_path_count=0 || true # count classpath args, warning if more than 1
51-
save_compiled=false # to save compiled script jar in script directory
52-
CLASS_PATH="" || true # scala classpath
53-
54-
# Little hack to check if all arguments are options
55-
all_params="$*"
56-
truncated_params="${*#-}"
57-
# options_indicator != 0 if at least one parameter is not an option
58-
options_indicator=$(( ${#all_params} - ${#truncated_params} - $# ))
59-
60-
[ -n "${SCALA_OPTS-}" ] && set -- $SCALA_OPTS "$@"
61-
62-
while [[ $# -gt 0 ]]; do
63-
case "$1" in
64-
-repl)
65-
setExecuteMode 'repl'
66-
shift
67-
;;
68-
-run)
69-
setExecuteMode 'run'
70-
shift
71-
;;
72-
-cp | -classpath)
73-
CLASS_PATH="$2${PSEP}"
74-
let class_path_count+=1
75-
shift
76-
shift
77-
;;
78-
-cp*|-classpath*) # partial fix for #10761
79-
# hashbang can combine args, e.g. "-classpath 'lib/*'"
80-
CLASS_PATH="${1#* *}${PSEP}"
81-
let class_path_count+=1
82-
shift
83-
;;
84-
-with-compiler)
85-
with_compiler=true
86-
shift
87-
;;
88-
@*|-color:*)
89-
addScala "${1}"
90-
shift
91-
;;
92-
-save|-savecompiled)
93-
save_compiled=true
94-
addScala "$1"
95-
shift
96-
;;
97-
-compile-only)
98-
addScala "$1"
99-
shift
100-
;;
101-
-version)
102-
# defer to scalac, then exit
103-
shift
104-
eval "\"$PROG_HOME/bin/scalac\" -version"
105-
scala_exit_status=$?
106-
onExit
107-
;;
108-
-J*)
109-
addJava "${1:2}"
110-
addScala "${1}"
111-
shift ;;
112-
-v|-verbose)
113-
verbose=true
114-
addScala "-verbose"
115-
shift ;;
116-
-run)
117-
setExecuteMode 'run'
118-
shift ;;
119-
120-
*)
121-
# script if extension .scala or .sc, or if has scala hashbang line
122-
# no -f test, issue meaningful error message (file not found)
123-
if [[ "$1" == *.scala || "$1" == *.sc ]]; then
124-
setExecuteMode 'script' # execute_script=true
125-
126-
# -f test needed before we examine the hashbang line
127-
elif [[ (-f "$1" && `head -n 1 -- "$1" | grep '#!.*scala'`) ]]; then
128-
setExecuteMode 'script' # execute_script=true
129-
fi
130-
131-
if [ "${execute_mode-}" == 'script' ]; then
132-
target_script="$1"
133-
shift
134-
if [ ! -f $target_script ]; then
135-
# likely a typo or missing script file, quit early
136-
echo "not found: $target_script" 1>&2
137-
scala_exit_status=2
138-
onExit
139-
fi
140-
# all are script args
141-
while [[ $# -gt 0 ]]; do
142-
addScript "${1}"
143-
shift
144-
done
145-
else
146-
# all unrecognized args appearing prior to a script name
147-
addResidual "$1"
148-
shift
149-
fi
150-
;;
151-
152-
esac
153-
done
154-
155-
#[ -n "${dump_args}" ] && dumpArgs ; exit 2
156-
if [ -z "${execute_mode-}" ]; then
157-
# no script was specified, set run or repl mode
158-
if [[ $options_indicator -eq 0 ]]; then
159-
setExecuteMode 'repl'
160-
else
161-
setExecuteMode 'run'
162-
fi
163-
fi
164-
165-
[ -n "${script_trace-}" ] && set -x
166-
167-
case "${execute_mode-}" in
168-
script)
169-
if [ "$CLASS_PATH" ]; then
170-
script_cp_arg="-classpath '$CLASS_PATH'"
171-
fi
172-
setScriptName="-Dscript.path=$target_script"
173-
target_jar="${target_script%.*}.jar"
174-
if [[ $save_compiled == true && "$target_jar" -nt "$target_script" ]]; then
175-
eval "\"$JAVACMD\"" $setScriptName -jar "$target_jar" "${script_args[@]}"
176-
scala_exit_status=$?
177-
else
178-
[[ $save_compiled == true ]] && rm -f $target_jar
179-
PROG_NAME=$ScriptingMain
180-
compilerJavaClasspathArgs # initialize jvm_cp_args with toolchain classpath
181-
scripting_string="-script $target_script ${script_args[@]}"
182-
# use eval instead of exec, to insure that onExit is subsequently called
183-
184-
# $script_cp_arg must be the first argument to $ScriptingMain
185-
# $scripting_string must be last
186-
eval "\"$JAVACMD\"" \
187-
${JAVA_OPTS:-$default_java_opts} \
188-
"${java_args[@]}" \
189-
"-classpath \"$jvm_cp_args\"" \
190-
-Dscala.usejavacp=true \
191-
"$setScriptName" \
192-
"$ScriptingMain" \
193-
${script_cp_arg-} \
194-
"${scala_args[@]}" \
195-
"${residual_args[@]}" \
196-
"${scripting_string-}" # must be the last arguments
197-
scala_exit_status=$?
198-
fi
199-
;;
200-
201-
repl)
202-
if [ "$CLASS_PATH" ]; then
203-
repl_cparg="-classpath \"$CLASS_PATH\""
204-
fi
205-
eval "\"$PROG_HOME/bin/scalac\" ${repl_cparg-} ${scalac_options[@]} -repl ${residual_args[@]}"
206-
scala_exit_status=$?
207-
;;
208-
209-
run)
210-
run_cparg="$DOTTY_LIB$PSEP$SCALA_LIB"
211-
if [ -z "$CLASS_PATH" ]; then
212-
run_cparg+="$PSEP."
213-
else
214-
run_cparg+="$PSEP$CLASS_PATH"
215-
fi
216-
if [ "$class_path_count" -gt 1 ]; then
217-
echo "warning: multiple classpaths are found, scala only use the last one."
218-
fi
219-
if [ $with_compiler == true ]; then
220-
run_cparg+="$PSEP$DOTTY_COMP$PSEP$TASTY_CORE$PSEP$DOTTY_INTF$PSEP$SCALA_ASM$PSEP$DOTTY_STAGING$PSEP$DOTTY_TASTY_INSPECTOR"
221-
fi
222-
# exec here would prevent onExit from being called, leaving terminal in unusable state
223-
eval "\"$JAVACMD\"" "-classpath \"$run_cparg\"" "${java_args[@]}" "${residual_args[@]}"
224-
scala_exit_status=$?
225-
;;
226-
227-
*)
228-
echo "warning: command option is not correct."
229-
;;
230-
esac
231-
23238
onExit

0 commit comments

Comments
 (0)