From c083858366e765a30078b91c84406451694a058d Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Fri, 5 May 2023 14:13:45 +0200 Subject: [PATCH] Handle parameter `--code-style=android_studio` in Ktlint CLI identical to deprecated parameter `--android` Closes #1982 --- CHANGELOG.md | 3 +- .../ktlint/cli/internal/KtlintCommandLine.kt | 6 ++-- .../com/pinterest/ktlint/cli/SimpleCLITest.kt | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e0b985e28..9e898fa463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed * Store path of file containing a lint violation relative to the location of the baseline file itself ([#1962](https://github.com/pinterest/ktlint/issues/1962)) -* Print absolute path of file in lint violations when flag "--relative" is not specified in Ktlint CLI ([#1963](https://github.com/pinterest/ktlint/issues/1963)) +* Print absolute path of file in lint violations when flag "--relative" is not specified in Ktlint CLI ([#1963](https://github.com/pinterest/ktlint/issues/1963)) +* Handle parameter `--code-style=android_studio` in Ktlint CLI identical to deprecated parameter `--android` ([#1982](https://github.com/pinterest/ktlint/issues/1982)) ### Changed * Separated Baseline functionality out of `ktlint-cli` into separate `ktlint-baseline` module for API consumers diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index 9a7ec5ee3a..e7da1d8482 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -260,9 +260,9 @@ internal class KtlintCommandLine { }.applyIf(disabledRules.isNotBlank()) { logger.debug { "Add editor config override to disable rules: '$disabledRules'" } plus(*disabledRulesEditorConfigOverrides()) - }.applyIf(android) { - logger.debug { "Add editor config override to set code style to 'android'" } - plus(CODE_STYLE_PROPERTY to CodeStyleValue.android) + }.applyIf(android || codeStyle == CodeStyleValue.android || codeStyle == CodeStyleValue.android_studio) { + logger.debug { "Add editor config override to set code style to 'android_studio'" } + plus(CODE_STYLE_PROPERTY to CodeStyleValue.android_studio) }.applyIf(stdin) { logger.debug { "Add editor config override to disable 'filename' rule which can not be used in combination with reading from " + diff --git a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt index 54cec00d40..9e69ce8983 100644 --- a/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt +++ b/ktlint-cli/src/test/kotlin/com/pinterest/ktlint/cli/SimpleCLITest.kt @@ -476,4 +476,39 @@ class SimpleCLITest { .doesNotContainLineMatching(Regex(".*Needless blank line.*")) } } + + @Nested + inner class `Issue 1983 - Enable android code style` { + @Test + fun `Enable android code style via parameter --android`( + @TempDir + tempDir: Path, + ) { + CommandLineTestRunner(tempDir) + .run( + testProjectName = "too-many-empty-lines", + arguments = listOf("--android"), + ) { + assertThat(normalOutput).containsLineMatching( + Regex(".*Add editor config override to set code style to 'android_studio'.*"), + ) + } + } + + @Test + fun `Enable android code style via parameter --code-style=android_studio`( + @TempDir + tempDir: Path, + ) { + CommandLineTestRunner(tempDir) + .run( + testProjectName = "too-many-empty-lines", + arguments = listOf("--code-style=android_studio"), + ) { + assertThat(normalOutput).containsLineMatching( + Regex(".*Add editor config override to set code style to 'android_studio'.*"), + ) + } + } + } }