pinterest / ktlint

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

Apply the `function-naming` exception for backticked test functions to apply to Test classes extending an org.junit Test class #2620

Closed takanuva15 closed 3 months ago

takanuva15 commented 3 months ago

Expected Behavior

Currently, the function-naming rule allows function names enclosed in backticks to deviate from function-naming's preset rules as long as the function itself resides in a class that imports one of io.kotest, junit.framework, kotlin.test, org.junit, or org.testng.

In very-large projects, such as the IntelliJ IDE itself, a lot of testing infrastructure gets repeated which results in the development of TestBase classes that other tests extend from. These TestBase classes contain all the setup code and assertion functions for child test classes to use, which means that developers extending those TestBase classes would write test methods and then use the assertion functions in the parent class for verifying their code ran successfully. Thus, there is never any need to actually import something from the test-package import list above, yet the child class is still a true Test class by standard interpretation (which should thus have its methods exempted from the function-naming rule).

As a practical example, the IntelliJ Platform provides a class called BasePlatformTestCase, which IntelliJ plugin developers are expected to extend for testing out their own plugin's features. BasePlatformTestCase does not contain any test imports, but it actually extends another class UsefulTestCase which does import junit.framework.TestCase;.

In my own IntelliJ plugin, I extended BasePlatformTestCase in my test class AhkCommenterTest, and added some test methods with backticks. Unfortunately, ktlint complains that the test methods are violating the function-naming rule because AhkCommenterTest does not actually import one of the testing packages mentioned above, even though it doesn't need to because BasePlatformTestCase provides all the relevant assertion methods required to verify test results.

image

Current Behavior

Ktlint marks backticked functions in child Test classes extending another BaseTest class as violations since the child class doesn't import a testing package, even though the child Test class does not need to import such a package since the BaseTest class already imports it and defines the relevant assertions for child test classes to use.

Additional information

I had raised this issue in the IntelliJ ktlint plugin repository before I was told that ktlint reporting the function as a violation was expected behavior.

paul-dingemans commented 3 months ago

I do understand where you come from. Unfortunately ktlint can not accomodate this. Linting/formatting happens per file. It does not not take any other file into account.

Best what you can do is to exclude this rule for test packages. For example, your .editorconfig can look something like:

root = true

[*.{kt,kts}]
# your default settings here

[**/test/**/*.{kt,kts}]
ktlint_standard_function-naming = disabled
takanuva15 commented 3 months ago

Ok fair enough, thanks for sharing the workaround