Kotlin rule: 'Function names should comply with a naming convention' has false positive

We get a false positive on rule:
kotlin:S100 (Function names should comply with a naming convention)

Which states:

But, according to Coding conventions | Kotlin Documentation

In tests (and only in tests), you can use method names with spaces enclosed in backticks.

We’re using SonarCloud

I guess it can be reproduced by running it on a kotlin file with a test named something like

@Test
fun `totally allowed name by Kotlin naming conventions`() {
}

I guess something like this regex would make it pass,

^([a-zA-Z][a-zA-Z0-9]*|`[a-zA-Z][a-zA-Z0-9 ]*`)$

however, it would not take into account the explicit

only in tests

note in the naming convention. For that i guess we need to check if the function is prefixed with @Test, but i guess that might require an overly complex regex.

1 Like

Hi,

Welcome to the community!

Generally, “normal” rules aren’t run on tests. Are you sure your file is being correctly identified as a test file, rather than a source file? One way to tell would be to see if Lines of Code has been measured for it. You can do that in the code tab:

 
Ann

As far as i can tell, all test files that are part of this pull request analysis have had their line count measured.

So i guess they are not identified as test files? How are they identified then?

Edit: I guess this page answers my question:

I’m checking with our guys to see how we configured it.

1 Like

So initially we might not have imported our repository as a monorepo

We may have to look into that.

But secondly, we’re not convinced that making SonaCloud ignore all the test files is what we really want.
So, i think we would just rather disable that rule in particular.

1 Like

Ok, so to “fix” this, we extended the Kotlin Quality Profile. The nice thing about that is, that all rules are inherited, so we can just override the kotlin:S100 rule. Either by disabling the rule entirely, or changing the pattern - which we did.

1 Like

But apparently SonarCloud doesn’t like this regex:

^[a-zA-Z][a-zA-Z0-9]*|`[a-zA-Z][a-zA-Z0-9\s]*`$

Any ideas?

1 Like

Hi,

My search tells me that spaces are only allowed in test names. But you’re telling analysis that these aren’t test files. Thus that can’t be the name of a test…? Altho I suppose the rule should apply the regex you supply.

I’ll bet that this is about the backticks. You’ve added them to the regex because a function name with spaces will be enclosed in them. But they delimit the name. They’re not (I think) technically part of the name. What happens if you remove them?

 
Thx,
Ann

1 Like

Removing the backticks from the regex made no difference.

1 Like

Hi,

Thanks for checking. I’ve flagged this for the language experts.

 
Ann

Hello @mjga-trifork,

Currently, the Kotlin Analyzer doesn’t support the scan of the test code. This feature will be added in the future, although it is not in the roadmap for 2024.

Please note that configuring test code to be analyzed as the main code can lead to False Positives caused by applying rules not designed for test code; it also prevents rules designed for test code from being used.

Regarding your question about SonarCloud not liking the regex:

^[a-zA-Z][a-zA-Z0-9]*|`[a-zA-Z][a-zA-Z0-9\s]*`$

Your regex seems to be missing the character - that is present in the test name. Try with:

^[a-zA-Z][a-zA-Z0-9]*|`[a-zA-Z][a-zA-Z0-9\s\-]*`$

I use https://regex101.com/ to test a regex. Give it a try if you don’t use it already! :slight_smile:

Cheers,
Angelo

1 Like

Hi Angelo

You’re right i missed the dash in my regex. I’ve substituted with \w to also make it match underscores:

^[a-zA-Z][a-zA-Z0-9]*|`[a-zA-Z][\w\s\-]*`$

Actually i think the test naming kotlin might allow for a lot more characters. Maybe even Unicode. However something like the above should be fine for now.
But it still won’t allow the test names.

From the discussion above, I realized that test code isn’t supported and I guess we’re somewhat abusing it but we would like it to scan the test code anyway. Otherwise we probably need to set it up all over again, as the repository the scan i performed on is a monorepo, and I don’t think we configured it as such.