Skip to content

Commit

Permalink
Disable the standard:filename rule whenever Ktlint CLI is run with …
Browse files Browse the repository at this point in the history
…option `--stdin

Closes pinterest#1742
  • Loading branch information
paul-dingemans committed Dec 24, 2022
1 parent 557c640 commit e2fe366
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Fixed

* An enumeration class having a primary constructor and in which the list of enum entries is followed by a semicolon then do not remove the semicolon in case it is followed by code element `no-semi` ([#1733](https://github.com/pinterest/ktlint/issues/1733))
* Disable the `standard:filename` rule whenever Ktlint CLI is run with option `--stdin` ([#1742](https://github.com/pinterest/ktlint/issues/1742))

### Changed

Expand Down
Expand Up @@ -236,6 +236,22 @@ internal class KtlintCommandLine {
)
private var minLogLevel: Level = Level.INFO

init {
if (debugOld != null || trace != null || verbose != null) {
if (minLogLevel == Level.OFF) {
minLogLevel = Level.ERROR
}
configureLogger().error {
"Options '--debug', '--trace', '--verbose' and '-v' are deprecated and replaced with option '--log-level=<level>' or '-l=<level>'."
}
exitKtLintProcess(1)
}

// Ensure that logger is initialized even when the run method is not executed because a subcommand like (--help)
// is executed so that method exitKtLintProcess only prints a log line when the appropriate loglevel is set.
logger = configureLogger()
}

private val tripped = AtomicBoolean()
private val fileNumber = AtomicInteger()
private val errorNumber = AtomicInteger()
Expand All @@ -261,6 +277,8 @@ internal class KtlintCommandLine {
plus(*disabledRulesEditorConfigOverrides())
}.applyIf(android) {
plus(CODE_STYLE_PROPERTY to CodeStyleValue.android)
}.applyIf(stdin) {
plus(createRuleExecutionEditorConfigProperty("standard:filename") to RuleExecution.disabled)
}

private fun disabledRulesEditorConfigOverrides() =
Expand All @@ -270,22 +288,6 @@ internal class KtlintCommandLine {
.map { ruleId -> createRuleExecutionEditorConfigProperty(ruleId) to RuleExecution.disabled }
.toTypedArray()

init {
if (debugOld != null || trace != null || verbose != null) {
if (minLogLevel == Level.OFF) {
minLogLevel = Level.ERROR
}
configureLogger().error {
"Options '--debug', '--trace', '--verbose' and '-v' are deprecated and replaced with option '--log-level=<level>' or '-l=<level>'."
}
exitKtLintProcess(1)
}

// Ensure that logger is initialized even when the run method is not executed because a subcommand like (--help)
// is executed so that method exitKtLintProcess only prints a log line when the appropriate loglevel is set.
logger = configureLogger()
}

fun run() {
assertStdinAndPatternsFromStdinOptionsMutuallyExclusive()

Expand Down
17 changes: 17 additions & 0 deletions ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt
Expand Up @@ -2,6 +2,8 @@ package com.pinterest.ktlint

import java.io.ByteArrayInputStream
import java.nio.file.Path
import javax.annotation.RegEx
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.SoftAssertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
Expand Down Expand Up @@ -168,4 +170,19 @@ class SimpleCLITest {
}.assertAll()
}
}

@Test
fun `Issue 1742 - Disable the filename rule when --stdin is used`(
@TempDir
tempDir: Path,
) {
CommandLineTestRunner(tempDir)
.run(
testProjectName = "too-many-empty-lines",
arguments = listOf("--stdin"),
stdin = ByteArrayInputStream("fun foo() = 42".toByteArray()),
) {
assertThat(normalOutput).containsLineMatching(Regex(".*ktlint_standard_filename: disabled.*"))
}
}
}

0 comments on commit e2fe366

Please sign in to comment.