Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test class and function naming: Optional separate pattern for Test files #5412

Open
samuelneff opened this issue Oct 14, 2022 · 3 comments
Open
Labels
Milestone

Comments

@samuelneff
Copy link

samuelneff commented Oct 14, 2022

Class and function naming rules for main and test share the same configuration but it is very common to have different conventions, particularly with the use of ticks in test names. I would like to add an option to define a separate pattern for test classes/functions. Even a separate inner class pattern for tests would be good, as Detekt's test code reports violations of the ClassNaming rule (at least when I opened it up; maybe my IDE is not configured correctly?).

Expected Behavior of the rule

New configuration option for ClassNaming and FunctionNaming to allow different patterns for test identifiers.

  ClassNaming:
    classPattern: '[A-Z][a-zA-Z0-9]*'
    testClassPattern: '[A-Z][a-zA-Z0-9]*(Test|Spec)'
    testInnerClassPattern: '[A-Z][a-zA-Z0-9 _=+,!@#$^&()-]*'

  FunctionNaming:
    functionPattern: '[a-z][a-zA-Z0-9]*'
    testFunctionPattern: '[A-Z][a-zA-Z0-9 _=+,!@#$^&()-]*'

Confirmed valid symbols in back-tick names: https://pl.kotl.in/zC3_rHFfG

Context

Conventions for main and test code often different and should be able to be configured separately.

Examples from Detekt's tests (code not related to this report removed for clarity)

class AvoidReferentialEqualitySpec {

    @Nested
    inner class `ReferentialEquality with defaults` {

        @Test
        fun `reports usage of === for strings`() {
        }

        @Test
        fun `reports usage of !== for strings`() {
}

class DebtSpec {

    fun `should print 20min, 10min and 5min`() {
    }
}

class IgnoredReturnValueSpec {

    @Test
    fun `reports when the containing class of a function has _@CheckReturnValue_`() {
    }
}

Caveat; Related Requirement

As far as I know, Detekt does not currently have a global way to differentiate main code from test code. There are a number of options to do this by default with optional overriding behavior:

  • Allow configuration to define a global pattern for main and test code. This is currently repeated often so it can simplify a lot of configuration. While the most usable solution, I would consider it as a separate enhancement.
  • Add a new configuration property to each of these rules to define what "test" means. Would require duplication unless the user utilizes YAML alias nodes.
  • Consider any file that imports a *.Test annotation to be a test file. Likely other common ones should be supported to. Not very clean.
  • At least initially, always use the same pattern for test packages, \w*([Tt]est|[Ss]pec. Make this globally configurable in a future enhancement.

Related issues and PRs

#5212 - Remove rule from NamingRules multi rule (Merged)
#4144 / #4438 - Don't exclude tests for naming rules (Merged)
#2656 / #3977 - Don't treat ` as part of class name (Merged)

@cortinico
Copy link
Member

As far as I know, Detekt does not currently have a global way to differentiate main code from test code. There are a number of options to do this by default with optional overriding behavior:

Theoretically, you have a detektMain and a detektTest task (if you use type resolution). You could provide different config file for the two configurations.

I agree that is not easy to do and we have no documentation about it

@BraisGabin
Copy link
Member

We should have the ability to configure the same rule multiple times with different configurations. This kind of request is not new and it have a lot of sense.

@JavierSegoviaCordoba
Copy link
Contributor

Related: #5896

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants