Sonarcloud cpd exclusions being ignored in the log

Greetings,

I am sonarCloud for my project. I am trying to setup the code coverage for the project. I use a local script called ‘test-coverage’ in conjunction with npm to generate the coverage reports. They are generated in the coverage directory. Sonarcloud successfully detects the reports . However, I want it to exclude the folder test from duplication by giving it in the sonar.cpd.exclusions parameter. However, the analysis ignores that parameter. I would like to get some insights about what might be going on. Here is the sonar-project.properties file

sonar.organization=xxxx
sonar.projectKey=xxx
sonar.host.url=https://sonarcloud.io
sonar.sourceEncoding=UTF-8
sonar.sources=./
sonar.exclusions=**/node_modules/**,  **/helmchart/**
sonar.coverage.exclusions=**/test/**/*.*
sonar.cpd.exclusions=**/test/**/*.* 
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
sonar.c.file.suffixes=- 
sonar.cpp.file.suffixes=- 

Here is


the analysis log

Any insight would be appreciated.

Hi,

Where are your test files located? Are they all under a single directory named test, or are there multiple test directories? Because there’s an easier way than how you’re currently approaching it:

// set source file locations to current directory, recursive
sonar.sources=.
// set test file locations to the test directory, recursive
sonar.tests=test
// at this point everything under test will be indexed 
// as both a source file and a test file, thus => analysis error
// so we have to keep the test files from being indexed as source
sonar.exclusions=test/**/*

Test files are automatically excluded from coverage and duplication calculations, so at this point, you’re done.

 
HTH,
Ann

Hey Ann,

Thanks for the reply. I will try it. I also want another query resolved. I am trying to exclude a whole bunch of directories and files from coverage and duplications.

**sonar.coverage.exclusions=**/test/**,  **/bin/**,  **/config/**,  **/jest-stare/**,  **/keys/**,  **/logs/**,  \
**/.*,  **/*.js,  **/*.json,  **/*.yml,  **/*.yaml,  **/README.md,  **/Jenkins*,  **/Docker*,   **/.*/**

sonar.cpd.exclusions=**/test/**,  **/bin/**,  **/config/**,  **/jest-stare/**,  **/keys/**,  **/logs/**,  \
**/.*,  **/*.js,  **/*.json,  **/*.yml,  **/*.yaml,  **/README.md,  **/Jenkins*,  **/Docker*,   **/.*/**

Ignoring the test directory, the analysis log is still not considering the other directories. I have tried seperating each directory or file with a comma, a comma + space and a comma + 2 spaces( as in this case ). The analysis log ignores the other directories. What’s going wrong here? And surprisingly, it doesnt show the **/test/** but **/test/**/*.* which was a previous commit.

Hi,

We try to keep it to one topic per thread. Otherwise it can get messy, fast. Since this seems related, I’ll give a quick answer, but reserve the right to ask you to create a new thread.

As I said earlier, files recognized by analysis as test files are naturally excluded from coverage and duplications calculations, so no need to include them in this list.

But beyond that, I’m starting to really question your definition of sonar.sources as ..

Where, exactly are your source files located? Because I’m starting to think it would be a whole lot easier to set a good, tight definition there than to get all these exclusions set up.

That said, ** excludes 0-n directories. To exclude the files in them, you’ll need to finish the patterns with /*. E.G. **/config/**/*.

 
HTH,
Ann

Hey Ann,

Thanks for the reply. I wanted the topmost level of the repo to be the source. Since that’s the origin of the path, I figured giving . would be appropriate.

Hi,

Okay, but why? What’s it getting you but a lot of extra work?

 
Ann

Since all the directories and code files are in there and I wanted them all to be covered for the analysis.

Hi,

Except… you don’t? Otherwise you wouldn’t need all these exceptions.

 
Ann

Hey Ann,

I get your point. However, there are about 15 directories/files in total. I want the analysis to cover about 6-8 of them. So I would be anyway required to mentioned them explicitly. Hence, I though I could mention the directories I do not want as exclusions. But what do you suggest?

Hi,

I suggest:

sonar.sources=dir1,dir2,dir3,dir4,dir5,dir6,dir7,dir8
sonar.tests=test

So much simpler, IMO. And less typing too :joy:

 
Ann

Hey Ann,

While giving the directory/file names for sonar.sources, do we need to use the wildcards to ensure its contents get covered too?

Hi,

The rules are slightly different for sonar.sources. It takes a comma-delimited list of directories, which will be scanned recursively. So it’s really just like I listed it: dir1,dir2 &etc.

 
Ann

Hey Ann,

Sounds good. Can code files be listed in the comma seperated way too? Let’s say I have 2 dirs, dir1, dir2 and a code file named, trial.js. Would sonar.sources=dir1,dir2,trial.js work?

Hi,

I had to look that one up: yes.

 
HTH,
Ann

Hey Ann,

Thanks a lot for the replies. I was curious about one thing. If you refer to my 2nd post where I asked about multiple directory exclusions, I mentioned about the analysis log showing its excluding the test directory but the pattern shown was of the previous commit. Do you know why that’s happening?

Hi,

No clue. Analysis acts on the code that was checked out into the workspace, so this is likely a question of what’s going on in your workflow/CI.

 
Ann

Hey Ann,

One more query. Let’s say I want the test files to be included in analysis but be removed from coverage and duplications alone. Would the code suggested below work?

// set source file locations to current directory, recursive
sonar.sources=.
// set test file locations to the test directory, recursive
sonar.tests=test
// at this point everything under test will be indexed 
// as both a source file and a test file, thus => analysis error
// so we have to keep the test files from being indexed as source
sonar.exclusions=test/**/*

Hi,

I’m very tempted to tell you to create a new thread for this “one more” question. :slight_smile:

No. It would not work. Read the comments.

You would have to do the explicit coverage and duplications exclusions.

 
Ann

Hey Ann,

Thanks for the replies.

Hey Ann,

While giving a list of file exclusions for cpd or any such list of exclusions, can we use \to give the files names in a new line in the sonar-project.properties file. I was unable to find info on this.