From 5b97b8e085269813f64a91314a77f7a4be8c1d56 Mon Sep 17 00:00:00 2001 From: Joseph Cooper Date: Tue, 21 May 2024 18:17:41 +0100 Subject: [PATCH 1/9] Move filename/stdin parsing logic to ParsedArgs Checking whether we have a single filename '-' or multiple files, none of which are '-' is parsing and should be part of the parsing step --- core/src/main/java/com/facebook/ktfmt/cli/Main.kt | 14 ++------------ .../main/java/com/facebook/ktfmt/cli/ParsedArgs.kt | 8 ++++++++ .../test/java/com/facebook/ktfmt/cli/MainTest.kt | 10 ---------- .../java/com/facebook/ktfmt/cli/ParsedArgsTest.kt | 6 ++++++ 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt index fba648b3..a3a6104a 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt @@ -56,10 +56,6 @@ class Main( } val result = mutableListOf() 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") @@ -95,14 +91,8 @@ class Main( return 1 } - val files: List - try { - files = expandArgsToFileNames(parsedArgs.fileNames) - } catch (e: java.lang.IllegalStateException) { - err.println(e.message) - return 1 - } - + val files: List = expandArgsToFileNames(parsedArgs.fileNames) + if (files.isEmpty()) { err.println("Error: no .kt files found") return 1 diff --git a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt index 6179f5db..d37b3bb6 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt @@ -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, diff --git a/core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt b/core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt index 925ad271..5c046db8 100644 --- a/core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/cli/MainTest.kt @@ -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" diff --git a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt index 72f9bec2..2e019014 100644 --- a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt @@ -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 = From b2e59446fed0ef8f1f7f76d4aa4e3b5e1ce0c4b0 Mon Sep 17 00:00:00 2001 From: Joseph Cooper Date: Sun, 9 Jun 2024 16:31:56 +0100 Subject: [PATCH 2/9] Use --style=