Skip to content

Commit 830987e

Browse files
committed
Add unit tests
1 parent dc17145 commit 830987e

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

+24-20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dotty.tools.dotc.config.Properties.envOrNone
1515
import java.util.jar._
1616
import java.util.jar.Attributes.Name
1717
import dotty.tools.io.Jar
18+
import dotty.tools.runner.ScalaClassLoader
1819

1920
enum ExecuteMode:
2021
case Guess
@@ -34,19 +35,19 @@ case class Settings(
3435
possibleEntryPaths: List[String] = List.empty,
3536
scriptArgs: List[String] = List.empty,
3637
targetScript: String = "",
37-
targetFqName: String = "",
38+
targetToRun: String = "",
3839
save: Boolean = false,
3940
modeShouldBePossibleRun: Boolean = false,
4041
modeShouldBeRun: Boolean = false,
4142
compiler: Boolean = false,
4243
) {
43-
def withExecuteMode(em: ExecuteMode): Settings = //this.executeMode match
44-
// case ExecuteMode.Guess =>
45-
this.copy(executeMode = em)
46-
// case _ =>
47-
// println(s"execute_mode==[$executeMode], attempted overwrite by [$em]")
48-
// this.copy(exitCode = 1)
49-
// end withExecuteMode
44+
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
45+
case ExecuteMode.Guess | ExecuteMode.PossibleRun =>
46+
this.copy(executeMode = em)
47+
case _ =>
48+
println(s"execute_mode==[$executeMode], attempted overwrite by [$em]")
49+
this.copy(exitCode = 1)
50+
end withExecuteMode
5051

5152
def withScalaArgs(args: String*): Settings =
5253
this.copy(scalaArgs = scalaArgs.appendedAll(args.toList))
@@ -71,8 +72,8 @@ case class Settings(
7172
this.copy(exitCode = 2)
7273
end withTargetScript
7374

74-
def withTargetFqName(targetFqName: String): Settings =
75-
this.copy(targetFqName = targetFqName)
75+
def withTargetToRun(targetToRun: String): Settings =
76+
this.copy(targetToRun = targetToRun)
7677

7778
def withSave: Settings =
7879
this.copy(save = true)
@@ -99,7 +100,7 @@ object MainGenericRunner {
99100
case Nil =>
100101
settings
101102
case "-run" :: fqName :: tail =>
102-
process(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetFqName(fqName))
103+
process(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
103104
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
104105
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
105106
case ("-version" | "--version") :: _ =>
@@ -150,14 +151,17 @@ object MainGenericRunner {
150151
repl.Main.main(properArgs.toArray)
151152

152153
case ExecuteMode.PossibleRun =>
153-
val targetFqName = settings.possibleEntryPaths.find { entryPath =>
154-
Try(Thread.currentThread().getContextClassLoader.loadClass(entryPath)) match
155-
case Failure(_) => false
156-
case Success(_) => true
154+
val newClasspath = (settings.classPath :+ ".").map(File(_).toURI.toURL)
155+
import dotty.tools.runner.RichClassLoader._
156+
val newClassLoader = ScalaClassLoader.fromURLsParallelCapable(newClasspath)
157+
val targetToRun = settings.possibleEntryPaths.to(LazyList).find { entryPath =>
158+
newClassLoader.tryToLoadClass(entryPath).orElse {
159+
Option.when(Jar.isJarOrZip(dotty.tools.io.Path(entryPath)))(Jar(entryPath).mainClass).flatten
160+
}.isDefined
157161
}
158-
targetFqName match
162+
targetToRun match
159163
case Some(fqName) =>
160-
run(settings.withTargetFqName(fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }*).withExecuteMode(ExecuteMode.Run))
164+
run(settings.withTargetToRun(fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }*).withExecuteMode(ExecuteMode.Run))
161165
case None =>
162166
run(settings.withExecuteMode(ExecuteMode.Repl))
163167
case ExecuteMode.Run =>
@@ -171,9 +175,9 @@ object MainGenericRunner {
171175
cp
172176
val newClasspath = (settings.classPath ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
173177

174-
val res = ObjectRunner.runAndCatch(newClasspath, settings.targetFqName, settings.residualArgs).flatMap {
175-
case ex: ClassNotFoundException if ex.getMessage == settings.residualArgs.head =>
176-
val file = settings.residualArgs.head
178+
val res = ObjectRunner.runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
179+
case ex: ClassNotFoundException if ex.getMessage == settings.targetToRun =>
180+
val file = settings.targetToRun
177181
Jar(file).mainClass match
178182
case Some(mc) =>
179183
ObjectRunner.runAndCatch(newClasspath :+ File(file).toURI.toURL, mc, settings.residualArgs)

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CoursierScalaTests:
7171
emptyArgsEqualsRepl()
7272

7373
def run() =
74-
val output = CoursierScalaTests.csScalaCmd("-run", "-classpath", scripts("/run").head.getParentFile.getParent, "run.myfile")
74+
val output = CoursierScalaTests.csScalaCmd("-classpath", scripts("/run").head.getParentFile.getParent, "-run", "run.myfile")
7575
assertEquals(output.mkString("\n"), "Hello")
7676
run()
7777

@@ -117,6 +117,11 @@ class CoursierScalaTests:
117117
assertEquals(output4.mkString("\n"), "Hello")
118118
compileFilesToJarAndRun()
119119

120+
def replWithArgs() =
121+
val output = CoursierScalaTests.csScalaCmd("-source", "3.0-migration")
122+
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
123+
replWithArgs()
124+
120125
object CoursierScalaTests:
121126

122127
def execCmd(command: String, options: String*): List[String] =

0 commit comments

Comments
 (0)