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
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.
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.
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.
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/**/*.
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.
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?
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.
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?
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?
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/**/*
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.