Skip to content

Commit

Permalink
Move filename/stdin parsing logic to ParsedArgs
Browse files Browse the repository at this point in the history
Checking whether we have a single filename '-' or multiple files, none
of which are '-' is parsing and should be part of the parsing step
  • Loading branch information
Joseph Cooper committed Jun 9, 2024
1 parent 6cc7efb commit 5b97b8e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
14 changes: 2 additions & 12 deletions core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ class Main(
}
val result = mutableListOf<File>()
for (arg in args) {
if (arg == "-") {
error(
"Error: '-', which causes ktfmt to read from stdin, should not be mixed with file name")
}
result.addAll(
File(arg).walkTopDown().filter {
it.isFile && (it.extension == "kt" || it.extension == "kts")
Expand Down Expand Up @@ -95,14 +91,8 @@ class Main(
return 1
}

val files: List<File>
try {
files = expandArgsToFileNames(parsedArgs.fileNames)
} catch (e: java.lang.IllegalStateException) {
err.println(e.message)
return 1
}

val files: List<File> = expandArgsToFileNames(parsedArgs.fileNames)

if (files.isEmpty()) {
err.println("Error: no .kt files found")
return 1
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ data class ParsedArgs(
}
}

if (fileNames.contains("-") && fileNames.size > 1) {
val filesExceptStdin = fileNames - "-"
return ParseResult.Error(
"Cannot read from stdin and files in same run. Found stdin specifier '-'" +
" and files ${filesExceptStdin.joinToString(", ")} "
)
}

return ParseResult.Ok(
ParsedArgs(
fileNames,
Expand Down
10 changes: 0 additions & 10 deletions core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ class MainTest {
.containsExactly(foo1, bar1, foo2, bar2)
}

@Test
fun `expandArgsToFileNames - a dash is an error`() {
try {
Main.expandArgsToFileNames(listOf(root.resolve("foo.bar").toString(), File("-").toString()))
fail("expected exception, but nothing was thrown")
} catch (e: IllegalStateException) {
assertThat(e.message).contains("Error")
}
}

@Test
fun `Using '-' as the filename formats an InputStream`() {
val code = "fun f1 ( ) : Int = 0"
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ class ParsedArgsTest {
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions rejects '-' and files at the same time`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("-", "File.kt"))
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `processArgs use the @file option with non existing file`() {
val e =
Expand Down

0 comments on commit 5b97b8e

Please sign in to comment.