Skip to content

Commit 07066f7

Browse files
committed
Add a test for positional arguments and fix formatting
1 parent 8c215b7 commit 07066f7

File tree

5 files changed

+69
-35
lines changed

5 files changed

+69
-35
lines changed

cli/src/main/scala/scala/scalanative/cli/ScalaNativeCli.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import caseapp.core.RemainingArgs
1111
object ScalaNativeCli extends CaseApp[CliOptions] {
1212

1313
def run(options: CliOptions, args: RemainingArgs) = {
14-
val positionalArgs = args.all
14+
val positionalArgs = args.all
1515
val buildOptionsMaybe = ConfigConverter.convert(options, positionalArgs)
1616

1717
buildOptionsMaybe.map { buildOptions =>

cli/src/main/scala/scala/scalanative/cli/options/CliOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import caseapp._
44

55
@AppName("ScalaNativeCli")
66
@ProgName("scala-native-cli")
7-
@ArgsName("main] [nir-files")
7+
@ArgsName("main-class] [nir-sources")
88
case class CliOptions(
99
@Recurse
1010
config: ConfigOptions,

cli/src/main/scala/scala/scalanative/cli/utils/ConfigConverter.scala

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@ case class BuildOptions(
1515

1616
object ConfigConverter {
1717

18-
def convert(options: CliOptions, positionalArgs: Seq[String]): Either[Throwable, BuildOptions] = {
19-
val main = positionalArgs.head
20-
val classpath = positionalArgs.tail
21-
generateConfig(options, main, classpath).flatMap(config =>
22-
Try(Paths.get(options.config.outpath)).toEither.map(outpath =>
23-
BuildOptions(config, outpath)
18+
def convert(
19+
options: CliOptions,
20+
positionalArgs: Seq[String]
21+
): Either[Throwable, BuildOptions] = {
22+
if (positionalArgs.size < 2) {
23+
Left(
24+
new IllegalArgumentException(
25+
"Not enough positional arguments. Main and at least one source file need to be specified."
26+
)
27+
)
28+
} else {
29+
val main = positionalArgs.head
30+
val classpath = positionalArgs.tail
31+
generateConfig(options, main, classpath).flatMap(config =>
32+
Try(Paths.get(options.config.outpath)).toEither.map(outpath =>
33+
BuildOptions(config, outpath)
34+
)
2435
)
25-
)
36+
}
2637
}
2738

2839
private def generateNativeConfig(
@@ -61,7 +72,11 @@ object ConfigConverter {
6172
} yield maybeNativeConfig
6273
}
6374

64-
private def generateConfig(options: CliOptions, main: String, classPath: Seq[String]): Either[Throwable, Config] = {
75+
private def generateConfig(
76+
options: CliOptions,
77+
main: String,
78+
classPath: Seq[String]
79+
): Either[Throwable, Config] = {
6580
for {
6681
nativeConfig <- generateNativeConfig(options)
6782
classPath <- Try(parseClassPath(classPath)).toEither

cli/src/main/scala/scala/scalanative/cli/utils/NativeConfigParserImplicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ object NativeConfigParserImplicits {
4242
caseapp.core.Error.UnrecognizedArgument(other)
4343
)
4444
}
45-
}
45+
}

cli/src/test/scala/scala/scalanative/cli/ConfigConverterTest.scala

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class ConfigConverterTest extends AnyFlatSpec {
1717
val dummyNativeConfigOptions = NativeConfigOptions()
1818
val dummyConfigOptions = ConfigOptions()
1919

20-
val dummyArguments = Seq("$Main", "A.nir", "B.nir") // TODO incomplete arguments
20+
val dummyArguments =
21+
Seq("$Main", "A.nir", "B.nir")
2122

2223
val dummyCliOptions: CliOptions = CliOptions(
2324
config = dummyConfigOptions,
@@ -30,28 +31,33 @@ class ConfigConverterTest extends AnyFlatSpec {
3031
assert(config.isRight)
3132
}
3233

33-
//TODO incomplete arguments check reporting
34-
35-
// it should "return default nativeConfig which lines up with empty nativeConfig" in {
36-
// val withInternallyDiscovered =
37-
// CliOptions(
38-
// dummyConfigOptions,
39-
// NativeConfigOptions(clang = Some(""), clangPP = Some("")),
40-
// logger = dummyLoggerOptions
41-
// )
42-
// val result = ConfigConverter.convert(withInternallyDiscovered, dummyArguments)
43-
// assert(result.right.get.config.compilerConfig == NativeConfig.empty)
44-
// }
34+
// TODO incomplete arguments check reporting
35+
it should "report incomplete arguments" in {
36+
val noArgs = Seq()
37+
val noArgsResult = ConfigConverter.convert(dummyCliOptions, noArgs)
38+
assert(noArgsResult.isLeft)
39+
assert(noArgsResult.left.get.isInstanceOf[IllegalArgumentException])
40+
41+
val mainOnly = Seq("Main$")
42+
val mainOnlyResult = ConfigConverter.convert(dummyCliOptions, mainOnly)
43+
assert(mainOnlyResult.isLeft)
44+
assert(mainOnlyResult.left.get.isInstanceOf[IllegalArgumentException])
45+
}
4546

4647
it should "parse classpath strings correctly" in {
47-
val classPathStrings = Seq("/home/dir/file", "/home/dirfile2", "/home/dir/with spaces/") // check case app passing with spaces
48+
val classPathStrings = Seq(
49+
"/home/dir/file",
50+
"/home/dirfile2",
51+
"/home/dir/with spaces/"
52+
) // check case app passing with spaces
4853
val expected = Seq(
4954
Paths.get("/home/dir/file"),
5055
Paths.get("/home/dirfile2"),
5156
Paths.get("/home/dir/with spaces/")
5257
)
5358

54-
val config = ConfigConverter.convert(dummyCliOptions, Seq("$Main") ++ classPathStrings)
59+
val config =
60+
ConfigConverter.convert(dummyCliOptions, Seq("$Main") ++ classPathStrings)
5561

5662
assert(config != None)
5763
assert(config.right.get.config.classPath.sameElements(expected))
@@ -61,10 +67,13 @@ class ConfigConverterTest extends AnyFlatSpec {
6167
def gcAssertion(gcString: String, expectedGC: GC) = {
6268
val options = CliOptions(
6369
dummyConfigOptions,
64-
NativeConfigOptions(gc = NativeConfigParserImplicits.gcParser(None, gcString).right.get),
70+
NativeConfigOptions(gc =
71+
NativeConfigParserImplicits.gcParser(None, gcString).right.get
72+
),
6573
dummyLoggerOptions
6674
)
67-
val config = ConfigConverter.convert(options, dummyArguments).right.get.config
75+
val config =
76+
ConfigConverter.convert(options, dummyArguments).right.get.config
6877
assert(config.compilerConfig.gc == expectedGC)
6978
}
7079
gcAssertion("immix", GC.immix)
@@ -77,10 +86,13 @@ class ConfigConverterTest extends AnyFlatSpec {
7786
def modeAssertion(modeString: String, expectedMode: Mode) = {
7887
val options = CliOptions(
7988
dummyConfigOptions,
80-
NativeConfigOptions(mode = NativeConfigParserImplicits.modeParser(None, modeString).right.get),
89+
NativeConfigOptions(mode =
90+
NativeConfigParserImplicits.modeParser(None, modeString).right.get
91+
),
8192
dummyLoggerOptions
8293
)
83-
val config = ConfigConverter.convert(options, dummyArguments).right.get.config
94+
val config =
95+
ConfigConverter.convert(options, dummyArguments).right.get.config
8496
assert(config.compilerConfig.mode == expectedMode)
8597
}
8698
modeAssertion("debug", Mode.debug)
@@ -92,10 +104,13 @@ class ConfigConverterTest extends AnyFlatSpec {
92104
def ltoAssertion(ltoString: String, expectedLto: LTO) = {
93105
val options = CliOptions(
94106
dummyConfigOptions,
95-
NativeConfigOptions(lto = NativeConfigParserImplicits.ltoParser(None, ltoString).right.get),
107+
NativeConfigOptions(lto =
108+
NativeConfigParserImplicits.ltoParser(None, ltoString).right.get
109+
),
96110
dummyLoggerOptions
97111
)
98-
val config = ConfigConverter.convert(options, dummyArguments).right.get.config
112+
val config =
113+
ConfigConverter.convert(options, dummyArguments).right.get.config
99114
assert(config.compilerConfig.lto == expectedLto)
100115
}
101116
ltoAssertion("none", LTO.none)
@@ -160,12 +175,16 @@ class ConfigConverterTest extends AnyFlatSpec {
160175
val expectedClangPPPath = Paths.get(clangPPString)
161176

162177
val options = CliOptions(
163-
dummyConfigOptions,
164-
NativeConfigOptions(clang=Some(clangString), clangPP = Some(clangPPString)),
178+
dummyConfigOptions,
179+
NativeConfigOptions(
180+
clang = Some(clangString),
181+
clangPP = Some(clangPPString)
182+
),
165183
dummyLoggerOptions
166184
)
167185

168-
val nativeConfig = ConfigConverter.convert(options, dummyArguments).right.get
186+
val nativeConfig =
187+
ConfigConverter.convert(options, dummyArguments).right.get
169188

170189
assert(nativeConfig.config.compilerConfig.clang == expectedClangPath)
171190
assert(nativeConfig.config.compilerConfig.clangPP == expectedClangPPPath)

0 commit comments

Comments
 (0)