Skip to content

Commit

Permalink
Merge pull request #2580 from pinterest/2578-patterns-from-stdin
Browse files Browse the repository at this point in the history
Fix null byte as default value for "--pattern-from-stdin"
  • Loading branch information
paul-dingemans authored Feb 29, 2024
2 parents 7a4587a + d80e910 commit e4dc74b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.deprecated
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.multiple
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.optionalValue
import com.github.ajalt.clikt.parameters.options.split
Expand Down Expand Up @@ -186,7 +185,7 @@ internal class KtlintCommandLine :
help =
"Read additional patterns to check/format from stdin. Patterns are delimited by the given argument. (default is " +
"newline). If the argument is an empty string, the NUL byte is used.",
).optionalValue(default = "\n", acceptsUnattachedValue = false)
).optionalValue(default = "", acceptsUnattachedValue = false)

private val editorConfigPath: String? by
option(
Expand Down Expand Up @@ -637,15 +636,15 @@ internal class KtlintCommandLine :
}
}

private fun readPatternsFromStdin(delimiter: String): Set<String> {
require(delimiter.isNotEmpty())

return String(System.`in`.readBytes())
.split(delimiter)
private fun readPatternsFromStdin(delimiter: String): Set<String> =
String(System.`in`.readBytes())
// The NUL byte (\u0000) is used as separator of file names, for example produced by the command "git diff --name-only -z".
// The output looks like:
// src/main/kotlin/Foo.ktsrc/main/kotlin/FooComposable.kt
.split(delimiter.ifEmpty { "\u0000" })
.let { patterns: List<String> ->
patterns.filterTo(LinkedHashSet(patterns.size), String::isNotEmpty)
}
}

/**
* Executes "Callable"s in parallel (lazily).
Expand Down Expand Up @@ -735,13 +734,3 @@ internal fun exitKtLintProcess(status: Int): Nothing {
logger.debug { "Exit ktlint with exit code: $status" }
exitProcess(status)
}

private sealed class StdinOption {
data class Stdin(
val enabled: Boolean,
) : StdinOption()

data class PatternsFromStdin(
val delimiter: String,
) : StdinOption()
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,31 @@ class SimpleCLITest {
}
}

@Test
@ParameterizedTest(name = "Option: {0}")
@ValueSource(
strings = [
"--patterns-from-stdin",
"--patterns-from-stdin=",
],
)
fun `Given some code with an error and a pattern which is read in from stdin which does not select the file then return the no files matched warning`(
patternsFromStdin: String,
@TempDir
tempDir: Path,
) {
val somePatternProvidedViaStdin = "some-pattern-provided-via-stdin"
val pathToFile1 = "path/to/file/1.kt"
val pathToFile2 = "path/to/file/2.kts"
val somePatternProvidedViaStdin = "$pathToFile1\u0000$pathToFile2\u0000"
CommandLineTestRunner(tempDir)
.run(
"too-many-empty-lines",
listOf("--patterns-from-stdin"),
listOf(patternsFromStdin),
stdin = ByteArrayInputStream(somePatternProvidedViaStdin.toByteArray()),
) {
SoftAssertions()
.apply {
assertNormalExitCode()
assertThat(normalOutput).containsLineMatching("No files matched [$somePatternProvidedViaStdin]")
assertThat(normalOutput).containsLineMatching("No files matched [$pathToFile1, $pathToFile2]")
}.assertAll()
}
}
Expand Down

0 comments on commit e4dc74b

Please sign in to comment.