Closed takanuva15 closed 7 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
Ok fair enough, thanks for sharing the workaround
Expected Behavior
Currently, the
function-naming
rule allows function names enclosed in backticks to deviate fromfunction-naming
's preset rules as long as the function itself resides in a class that imports one ofio.kotest
,junit.framework
,kotlin.test
,org.junit
, ororg.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. TheseTestBase
classes contain all the setup code and assertion functions for child test classes to use, which means that developers extending thoseTestBase
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 thefunction-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 classUsefulTestCase
which does importjunit.framework.TestCase;
.In my own IntelliJ plugin, I extended
BasePlatformTestCase
in my test classAhkCommenterTest
, and added some test methods with backticks. Unfortunately, ktlint complains that the test methods are violating thefunction-naming
rule becauseAhkCommenterTest
does not actually import one of the testing packages mentioned above, even though it doesn't need to becauseBasePlatformTestCase
provides all the relevant assertion methods required to verify test results.Current Behavior
Ktlint marks backticked functions in child
Test
classes extending anotherBaseTest
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.