Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inform user about using --format option of KtLint CLI #2091

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ At this point in time, it is not yet decided what the next steps will be. Ktlint
* Add new experimental rule `statement-wrapping` which ensures function, class, or other blocks statement body doesn't start or end at starting or ending braces of the block ([#1938](https://github.com/pinterest/ktlint/issues/1938))
* Add new experimental rule `blank-line-before-declaration`. This rule requires a blank line before class, function or property declarations ([#1939](https://github.com/pinterest/ktlint/issues/1939))
* Wrap multiple statements on same line `wrapping` ([#1078](https://github.com/pinterest/ktlint/issues/1078))

* Inform user about using `--format` option of KtLint CLI when finding a violation that can be autocorrected ([#1071](https://github.com/pinterest/ktlint/issues/1071))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ internal class KtlintCommandLine {
private val tripped = AtomicBoolean()
private val fileNumber = AtomicInteger()
private val errorNumber = AtomicInteger()
private val adviseToUseFormat = AtomicBoolean()

internal var debug: Boolean = false
get() = Level.DEBUG.isGreaterOrEqual(minLogLevel)
Expand Down Expand Up @@ -329,6 +330,13 @@ internal class KtlintCommandLine {
baseline.lintErrorsPerFile,
aggregatedReporter,
)
if (adviseToUseFormat.get()) {
if (format) {
logger.error { "Format was not able to autocorrect all errors that theoretically can be autocorrected." }
} else {
logger.warn { "Lint has found errors than can be autocorrected using 'ktlint --format'" }
}
}
}
aggregatedReporter.afterAll()

Expand Down Expand Up @@ -454,6 +462,9 @@ internal class KtlintCommandLine {
fileNumber.incrementAndGet()
val errListLimit = minOf(ktlintCliErrors.size, maxOf(limit - errorNumber.get(), 0))
errorNumber.addAndGet(errListLimit)
if (!adviseToUseFormat.get() && ktlintCliErrors.containsErrorThatCanBeAutocorrected()) {
adviseToUseFormat.set(true)
}

reporter.before(relativeRoute)
ktlintCliErrors
Expand All @@ -462,6 +473,8 @@ internal class KtlintCommandLine {
reporter.after(relativeRoute)
}

private fun List<KtlintCliError>.containsErrorThatCanBeAutocorrected() = any { it.status == LINT_CAN_BE_AUTOCORRECTED }

private fun process(
ktLintRuleEngine: KtLintRuleEngine,
code: Code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ class SimpleCLITest {
}
}

@Test
fun `Given some code with an error then return from lint with the error exit code and warning to use --format`(
@TempDir
tempDir: Path,
) {
CommandLineTestRunner(tempDir)
.run(
"too-many-empty-lines",
listOf("**/*.test"),
) {
SoftAssertions().apply {
assertErrorExitCode()
assertThat(normalOutput)
.containsLineMatching(Regex(".* WARN .* Lint has found errors than can be autocorrected using 'ktlint --format'"))
}.assertAll()
}
}

@Test
fun `Given some code with an error but a glob which does not select the file`(
@TempDir
Expand Down