Skip to content

Commit cc6198b

Browse files
committed
Default to the run sub-command when --main-class and -classpath have been passed (but there were no explicit inputs)
1 parent 0e768fa commit cc6198b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

modules/cli/src/main/scala/scala/cli/commands/Default.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Default(
3434
{
3535
val shouldDefaultToRun =
3636
args.remaining.nonEmpty || options.shared.snippet.executeScript.nonEmpty ||
37-
options.shared.snippet.executeScala.nonEmpty || options.shared.snippet.executeJava.nonEmpty
37+
options.shared.snippet.executeScala.nonEmpty || options.shared.snippet.executeJava.nonEmpty ||
38+
(options.shared.extraJarsAndClasspath.nonEmpty && options.sharedRun.mainClass.mainClass.nonEmpty)
3839
if shouldDefaultToRun then RunOptions.parser else ReplOptions.parser
3940
}.parse(rawArgs) match
4041
case Left(e) => error(e)

modules/integration/src/test/scala/scala/cli/integration/DefaultTests.scala

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class DefaultTests extends ScalaCliSuite {
66
test("running scala-cli with no args should default to repl") {
77
TestInputs.empty.fromRoot { root =>
88
val res = os.proc(TestUtil.cli, "--repl-dry-run").call(cwd = root, mergeErrIntoOut = true)
9-
expect(res.out.trim() == "Dry run, not running REPL.")
9+
expect(res.out.trim() == replDryRunOutput)
1010
}
1111
}
1212
test("running scala-cli with no args should not accept run-only options") {
@@ -94,11 +94,66 @@ class DefaultTests extends ScalaCliSuite {
9494
}
9595
}
9696

97+
test("default to the run sub-command if -classpath and --main-class are passed") {
98+
val expectedOutput = "Hello"
99+
val mainClassName = "Main"
100+
TestInputs(
101+
os.rel / s"$mainClassName.scala" -> s"""object $mainClassName extends App { println("$expectedOutput") }"""
102+
).fromRoot { (root: os.Path) =>
103+
val compilationOutputDir = os.rel / "compilationOutput"
104+
// first, precompile to an explicitly specified output directory with -d
105+
os.proc(
106+
TestUtil.cli,
107+
".",
108+
"-d",
109+
compilationOutputDir
110+
).call(cwd = root)
111+
112+
// next, run while relying on the pre-compiled class instead of passing inputs
113+
val runRes = os.proc(
114+
TestUtil.cli,
115+
"--main-class",
116+
mainClassName,
117+
"-classpath",
118+
(os.rel / compilationOutputDir).toString
119+
).call(cwd = root)
120+
expect(runRes.out.trim == expectedOutput)
121+
}
122+
}
123+
124+
test("default to the repl sub-command if -classpath is passed, but --main-class isn't") {
125+
val expectedOutput = "Hello"
126+
val mainClassName = "Main"
127+
TestInputs(
128+
os.rel / s"$mainClassName.scala" -> s"""object $mainClassName extends App { println("$expectedOutput") }"""
129+
).fromRoot { (root: os.Path) =>
130+
val compilationOutputDir = os.rel / "compilationOutput"
131+
// first, precompile to an explicitly specified output directory with -d
132+
os.proc(
133+
TestUtil.cli,
134+
".",
135+
"-d",
136+
compilationOutputDir
137+
).call(cwd = root)
138+
139+
// next, run the repl while relying on the pre-compiled classes
140+
val runRes = os.proc(
141+
TestUtil.cli,
142+
"--repl-dry-run",
143+
"-classpath",
144+
(os.rel / compilationOutputDir).toString
145+
).call(cwd = root, mergeErrIntoOut = true)
146+
expect(runRes.out.trim == replDryRunOutput)
147+
}
148+
}
149+
97150
private def unrecognizedArgMessage(argName: String) =
98151
s"""
99152
|Unrecognized argument: $argName
100153
|
101154
|To list all available options, run
102155
| ${Console.BOLD}${TestUtil.detectCliPath} --help${Console.RESET}
103156
|""".stripMargin.trim
157+
158+
private lazy val replDryRunOutput = "Dry run, not running REPL."
104159
}

0 commit comments

Comments
 (0)