pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.06k stars 504 forks source link

Cannot apply default formatting Kotest\StringSpec when migrate from 1.0.1 -> 1.3.0 #2722

Closed ievgen-kapinos closed 1 day ago

ievgen-kapinos commented 2 days ago

Hi, there!

My team uses in Kotlin projects:

Now my team uses ktlint version 1.0.1 (hardcoded in IDE plugin and Gradle plugin). All tests look like it is described in Kotest documentation. See https://kotest.io/docs/framework/testing-styles.html#string-spec

When I try to migrate ktlint to version 1.3.0 all tests have to be reformatted (with extra indent), because they violate rule https://pinterest.github.io/ktlint/1.3.0/rules/standard/#class-signature.

Screenshot 2024-06-28 at 13 46 00

I've tried to set ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than=2 but it did not help.

Issue happens due to approach how tests to be written. It is expected that test classes to be inherited from kotest class. On printscreen above it is StringSpec. This class should be initialised with single argument, lambda function which should contain full code of unit test.

Expected Behavior

class MyTests : StringSpec({
    "strings.length should return size of string" {
        "hello".length shouldBe 5
    }
})

Current Behavior

class MyTests : 
    StringSpec({
        "strings.length should return size of string" {
            "hello".length shouldBe 5
        }
})

Additional information

We have a tons of tests. Please help to keep them as is.

paul-dingemans commented 2 days ago

For now, most practical approach will be to disable the class-signature rule. If you want to, you can do this only for test code, or for the entire project.

ievgen-kapinos commented 2 days ago

Hi @paul-dingemans

It is nice to find you as a maintaner of this repo as well. I appreciate your contribution to community.

I agree with you, it will work as temporary workaround. But in case of multi-module project, let's say 7 sub-modules, we have 7 folders with Tests. Each of them requires copy of .editorconfig file. But if we have many projects like that, then number of files will explode. I have to admit, in some cases (like mine), it is also possible to target all test files in single root .editorconfig file by adding a section [*Test.{kt,kts}].

Anyway, for the latest ktlint it is non-official formatting. Disabling official rules looks smelly.

Could you consider to add support for this case in next versions of ktlint?

Arguments are:

paul-dingemans commented 1 day ago

It was okay in version 1.0.1

You can still upgrade to 1.2.1 in. which the class-signature rule was still experimental.

  • There are no benefits put super class/interface on new line, if it is ony one (no other interfaces declared). I suppose, if it will be implemented, then problem with extra indent will be sloved automatically.

See #2423, #2710 and #2711. I will close this issue as it will not be resolved until the aforementioned issues are resolved. That might take quit a long while though. Until that time, disabling the class-signature is the best you can do, although it feels smelly.