Skip to content

Commit 3be3459

Browse files
committed
Run test takes java args
Uses `// java:` syntax as a stopgap. Args are appended to child message line.
1 parent fed9a38 commit 3be3459

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

compiler/test/dotty/tools/utils.scala

+16-11
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import scala.io.Source
1212
import scala.jdk.StreamConverters._
1313
import scala.reflect.ClassTag
1414
import scala.util.Using.resource
15-
import scala.util.chaining.given
1615
import scala.util.control.{ControlThrowable, NonFatal}
1716

1817
import dotc.config.CommandLineParser
18+
import dotc.util.chaining.*
1919

2020
object Dummy
2121

@@ -76,7 +76,11 @@ object ToolName:
7676
def named(s: String): ToolName = values.find(_.toString.equalsIgnoreCase(s)).getOrElse(throw IllegalArgumentException(s))
7777

7878
type ToolArgs = Map[ToolName, List[String]]
79+
object ToolArgs:
80+
def empty = Map.empty[ToolName, List[String]]
7981
type PlatformFiles = Map[TestPlatform, List[String]]
82+
object PlatformFiles:
83+
def empty = Map.empty[TestPlatform, List[String]]
8084

8185
/** Take a prefix of each file, extract tool args, parse, and combine.
8286
* Arg parsing respects quotation marks. Result is a map from ToolName to the combined tokens.
@@ -90,21 +94,23 @@ def toolArgsFor(files: List[JPath], charset: Charset = UTF_8): ToolArgs =
9094
* If the ToolName is Target, then also accumulate the file name associated with the given platform.
9195
*/
9296
def platformAndToolArgsFor(files: List[JPath], charset: Charset = UTF_8): (PlatformFiles, ToolArgs) =
93-
files.foldLeft(Map.empty[TestPlatform, List[String]] -> Map.empty[ToolName, List[String]]) { (res, path) =>
97+
files.foldLeft((PlatformFiles.empty, ToolArgs.empty)) { (res, path) =>
9498
val toolargs = toolArgsParse(resource(Files.lines(path, charset))(_.limit(10).toScala(List)), Some(path.toString))
9599
toolargs.foldLeft(res) {
96-
case ((plat, acc), (tool, args)) =>
100+
case ((plats, tools), (tool, args)) =>
97101
val name = ToolName.named(tool)
98102
val tokens = CommandLineParser.tokenize(args)
99103

100-
val plat1 = if name eq ToolName.Target then
104+
val plats1 = if name eq ToolName.Target then
101105
val testPlatform = TestPlatform.named(tokens.head)
102106
val fileName = path.toString
103-
plat.updatedWith(testPlatform)(_.map(fileName :: _).orElse(Some(fileName :: Nil)))
107+
plats.updatedWith(testPlatform)(_.map(fileName :: _).orElse(Some(fileName :: Nil)))
104108
else
105-
plat
109+
plats
106110

107-
plat1 -> acc.updatedWith(name)(v0 => v0.map(_ ++ tokens).orElse(Some(tokens)))
111+
val tools1 = tools.updatedWith(name)(v0 => v0.map(_ ++ tokens).orElse(Some(tokens)))
112+
113+
(plats1, tools1)
108114
}
109115
}
110116

@@ -131,19 +137,18 @@ private val directiveUnknown = raw"//> using (.*)".r.unanchored
131137
// If args string ends in close comment, stop at the `*` `/`.
132138
// Returns all the matches by the regex.
133139
def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,String)] =
134-
lines.flatMap {
140+
lines.flatMap:
135141
case toolArg("scalac", _) => sys.error(s"`// scalac: args` not supported. Please use `//> using options args`${filename.fold("")(f => s" in file $f")}")
136142
case toolArg("javac", _) => sys.error(s"`// javac: args` not supported. Please use `//> using javacOpt args`${filename.fold("")(f => s" in file $f")}")
137143
case toolArg(name, args) => List((name, args))
138144
case _ => Nil
139-
} ++
140-
lines.flatMap {
145+
++
146+
lines.flatMap:
141147
case directiveOptionsArg(args) => List(("scalac", args))
142148
case directiveJavacOptions(args) => List(("javac", args))
143149
case directiveTargetOptions(platform) => List(("target", platform))
144150
case directiveUnknown(rest) => sys.error(s"Unknown directive: `//> using ${CommandLineParser.tokenize(rest).headOption.getOrElse("''")}`${filename.fold("")(f => s" in file $f")}")
145151
case _ => Nil
146-
}
147152

148153
import org.junit.Test
149154
import org.junit.Assert._

compiler/test/dotty/tools/vulpix/ChildJVMMain.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public class ChildJVMMain {
1212
static final String MessageStart = "##THIS IS THE START FOR ME, HELLO##";
1313
static final String MessageEnd = "##THIS IS THE END FOR ME, GOODBYE##";
1414

15-
private static void runMain(String dir) throws Exception {
15+
private static void runMain(String line) throws Exception {
1616
Method meth = null;
17-
Object[] args = new Object[]{ new String[]{ } };
17+
String[] args = new String[]{ };
1818
try {
19+
String[] tokens = line.split("\\s+", 2);
20+
String dir = tokens[0];
21+
if (tokens.length > 1) {
22+
args = tokens[1].split("\\s+");
23+
}
1924
String jcp = System.getProperty("java.class.path");
2025
String sep = File.pathSeparator;
2126
System.setProperty("java.class.path", jcp == null ? dir : dir + sep + jcp);
@@ -37,15 +42,15 @@ private static void runMain(String dir) throws Exception {
3742
}
3843
System.out.println(MessageStart);
3944

40-
meth.invoke(null, args);
45+
meth.invoke(null, new Object[] { args });
4146
}
4247

4348
public static void main(String[] args) throws Exception {
44-
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
49+
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
4550

46-
while (true) {
47-
runMain(stdin.readLine());
48-
System.out.println(MessageEnd);
49-
}
51+
while (true) {
52+
runMain(stdin.readLine());
53+
System.out.println(MessageEnd);
54+
}
5055
}
5156
}

compiler/test/dotty/tools/vulpix/RunnerOrchestration.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trait RunnerOrchestration {
5353

5454
/** Running a `Test` class's main method from the specified `classpath` */
5555
def runMain(classPath: String, toolArgs: ToolArgs)(implicit summaryReport: SummaryReporting): Status =
56-
monitor.runMain(classPath)
56+
monitor.runMain(classPath, toolArgs.getOrElse(ToolName.Java, Nil))
5757

5858
/** Each method of Debuggee can be called only once, in the order of definition.*/
5959
trait Debuggee:
@@ -86,8 +86,8 @@ trait RunnerOrchestration {
8686
*/
8787
private class RunnerMonitor {
8888

89-
def runMain(classPath: String)(implicit summaryReport: SummaryReporting): Status =
90-
withRunner(_.runMain(classPath))
89+
def runMain(classPath: String, args: List[String])(implicit summaryReport: SummaryReporting): Status =
90+
withRunner(_.runMain(classPath, args))
9191

9292
def debugMain(classPath: String)(f: Debuggee => Unit)(implicit summaryReport: SummaryReporting): Unit =
9393
withRunner(_.debugMain(classPath)(f))
@@ -132,17 +132,17 @@ trait RunnerOrchestration {
132132
process = null
133133

134134
/** Blocks less than `maxDuration` while running `Test.main` from `dir` */
135-
def runMain(classPath: String): Status =
135+
def runMain(classPath: String, args: List[String]): Status =
136136
assert(process ne null, "Runner was killed and then reused without setting a new process")
137-
awaitStatusOrRespawn(startMain(classPath))
137+
awaitStatusOrRespawn(startMain(classPath, args))
138138

139139
def debugMain(classPath: String)(f: Debuggee => Unit): Unit =
140140
assert(process ne null, "Runner was killed and then reused without setting a new process")
141141

142142
val debuggee = new Debuggee:
143143
private var mainFuture: Future[Status] = null
144144
def readJdiPort(): Int = process.getJdiPort()
145-
def launch(): Unit = mainFuture = startMain(classPath)
145+
def launch(): Unit = mainFuture = startMain(classPath, Nil)
146146
def exit(): Status =
147147
awaitStatusOrRespawn(mainFuture)
148148

@@ -153,9 +153,10 @@ trait RunnerOrchestration {
153153
throw e
154154
end debugMain
155155

156-
private def startMain(classPath: String): Future[Status] =
156+
private def startMain(classPath: String, args: List[String]): Future[Status] =
157+
val line = if args.isEmpty then classPath else s"$classPath ${args.mkString(" ")}"
157158
// pass classpath to running process
158-
process.printLine(classPath)
159+
process.printLine(line)
159160

160161
// Create a future reading the object:
161162
Future:

tests/run/Pouring.check

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Illegal command line: more arguments expected
1+
Moves: Vector(Empty(0), Empty(1), Fill(0), Fill(1), Pour(0,1), Pour(1,0))
2+
Solution: Some(Fill(1) Pour(1,0) Empty(0) Pour(1,0) Fill(1) Pour(1,0) --> Vector(4, 6))

tests/run/Pouring.scala

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// java: 6 4 7
2+
13
type Glass = Int
24
type Levels = Vector[Int]
35

0 commit comments

Comments
 (0)