Skip to content

Commit

Permalink
No files matched message (#1534)
Browse files Browse the repository at this point in the history
Print an error message and return with non-zero exit code when no files are found that match with the globs

Closes #629
  • Loading branch information
paul-dingemans committed Jul 18, 2022
1 parent 7abe7da commit 07ce058
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,8 @@ The "visit" life cycle hook will be removed in Ktlint 0.48. In KtLint 0.47 the "

### Changed

* Print an error message and return with non-zero exit code when no files are found that match with the globs ([#629](https://github.com/pinterest/ktlint/issue/629)).

### Removed

## [0.46.1] - 2022-06-21
Expand Down
Expand Up @@ -26,7 +26,7 @@ private val tildeRegex = Regex("^(!)?~")
private val os = System.getProperty("os.name")
private val userHome = System.getProperty("user.home")

private val defaultPatterns = setOf(
internal val defaultPatterns = setOf(
"**$globSeparator*.kt",
"**$globSeparator*.kts"
)
Expand Down
Expand Up @@ -211,6 +211,13 @@ internal class KtlintCommandLine {

failOnOldRulesetProviderUsage()

// Set default value to patterns only after the logger has been configured to avoid a warning about initializing
// the logger multiple times
if (patterns.isEmpty()) {
logger.info { "Enable default patterns $defaultPatterns" }
patterns = ArrayList(defaultPatterns)
}

val start = System.currentTimeMillis()

val baselineResults = loadBaseline(baseline)
Expand Down Expand Up @@ -242,6 +249,10 @@ internal class KtlintCommandLine {
)
}
reporter.afterAll()
if (fileNumber.get() == 0) {

This comment has been minimized.

Copy link
@baylrock

baylrock Aug 25, 2022

This was quite a surprise when all non-kotlin maven pom submodules that inherit parent plugin configuration started failing because of this check... And apparently, there is no way to ignore this check?!

This comment has been minimized.

Copy link
@baylrock

baylrock Aug 25, 2022

@paul-dingemans was this considered, or is there a chance we could make an option to exclude this check?

This comment has been minimized.

Copy link
@paul-dingemans

paul-dingemans Aug 25, 2022

Author Collaborator

This looks like an unintended side effect. Can you please raise an issue for it and include a sample project for reproduction and test?

This comment has been minimized.

Copy link
@paul-dingemans

paul-dingemans Sep 3, 2022

Author Collaborator

Issue has already been created by another user

logger.error { "No files matched $patterns" }
exitProcess(1)
}
logger.debug { "${System.currentTimeMillis() - start}ms / $fileNumber file(s) / $errorNumber error(s)" }
if (tripped.get()) {
exitProcess(1)
Expand Down Expand Up @@ -281,8 +292,7 @@ internal class KtlintCommandLine {
ruleSets
)
}
}
.parallel({ (file, errList) -> report(file.location(relative), errList, reporter) })
}.parallel({ (file, errList) -> report(file.location(relative), errList, reporter) })
}

private fun lintStdin(
Expand Down
23 changes: 20 additions & 3 deletions ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt
Expand Up @@ -63,6 +63,20 @@ class SimpleCLITest : BaseCLITest() {
}
}

@Test
fun `Given some code with an error but a glob which does not select the file`() {
runKtLintCliProcess(
"too-many-empty-lines",
listOf("SomeOtherFile.kt")
) {
assertErrorExitCode()

assert(normalOutput.find { it.contains("No files matched [SomeOtherFile.kt]") } != null) {
"Unexpected output:\n${normalOutput.joinToString(separator = "\n")}"
}
}
}

@Test
fun `Given some code with an error which can be autocorrected then return from from with the normal exit code`() {
runKtLintCliProcess(
Expand All @@ -78,14 +92,17 @@ class SimpleCLITest : BaseCLITest() {
}

@Test
fun `Given some code with an error for a rule which is disabled via CLI argument --disabled_rules then return from lint with the normal exit code and without error output`() {
fun `Given some code which only contains errors for rules which are disabled via CLI argument --disabled_rules then return from lint with the normal exit code and without error output`() {
runKtLintCliProcess(
"too-many-empty-lines",
listOf("disabled_rules=no-consecutive-blank-lines")
listOf("--disabled_rules=no-consecutive-blank-lines,no-empty-first-line-in-method-block")
) {
assertNormalExitCode()

assertThat(normalOutput).doesNotContain("Needless blank line(s)")
assertThat(normalOutput).doesNotContain(
"no-consecutive-blank-lines",
"no-empty-first-line-in-method-block"
)
}
}
}

0 comments on commit 07ce058

Please sign in to comment.