SonarScanner error "The folder 'src/test' does not exist"

This one has been bugging me for quite some time and I felt like I should report it.

If one module, in a multi-module project, does not have any unit tests, for some reason, scanning fails.

There are numerous cases when it can happen that a module does not have any unit tests, for example if sources in the module is used by test code only. Or if tests at the moment do not exist (SonarQube could then warn about low code coverage, there are rules for that).

The current workaround is to create a dummy file in “src/test” so that the folder exists… But no tests is are actually needed.

My suggestion is to fix this and convert this build error into a warning instead.

Hey Alix

Can you confirm what specific error you get?

Hi,
no details:

The folder ‘src/test’ does not exist for ‘:domainTestStubs’ (base directory = /Users/buildagent/builds/zxtdf38xr/0/x/y/apps/myproject/core/domain-test-stubs)

Would it be an acceptable workaround for you to explicitly set sonar.sources for those modules without a test folder?

I’m thinking of the docs here.

// build.gradle
project(":project1") {
    sonar {
        properties {
            property "sonar.sources", "src/main"
        }
    }}

I did this before, as I was told it was no longer necessary to specify sonar.sources/sonar.tests but then I ran into problems (I don’t remember the details, but either code was missing from the analysis or test coverage got skewed) so I had to revert to explicitly set these properties.

So to answer your question: No, unfortunately not

I think ideally we would figure out what those problems are so that you can successfully use these properties and avoid the issue altogether.

So your suggestion is that I do not specify sonar.sources and sonar.tests explicitly and report whatever problem I find with that approach?

Ah, I missed something. I thought for a moment that the issue was sonar.sources containing a reference to your test folder. That wouldn’t make sense, since tests should be passed to sonar.tests.

From the docs:

Additional defaults for projects with Java-base or Java plugin applied

Property Gradle default
sonar.tests ${sourceSets.test.allJava.srcDirs} (filtered to only include existing directories)

Additional defaults for Android projects

Property Gradle default
sonar.sources (for non test variants) ${variant.sourcesets.map} (ManifestFile/CDirectories/AidlDirectories/AssetsDirectories/CppDirectories/JavaDirectories/RenderscriptDirectories/ResDirectories/ResourcesDirectories)
sonar.tests (for test variants) ${variant.sourcesets.map} (ManifestFile/CDirectories/AidlDirectories/AssetsDirectories/CppDirectories/JavaDirectories/RenderscriptDirectories/ResDirectories/ResourcesDirectories)

Is this a Java project or an Android project?

It’s an Android project. Sorry for the confusion, the code is correctly classified…

It’s just that sonar.sources in this case is test stubs and similar…
sonar.tests is set but the directory does not exist (no tests)

So I wonder if for Java projects it makes sure the directory exists… but not for Android projects. While we have an integration test for java projects with no tests, I don’t see one for Android.

I’m going to flag this for some expert eyes. Sorry for the confusion earlier.

1 Like

Hello @Alix,

I have a few questions: does the folder src/test exist? If no, then you have to explicitly specify sonar.tests to be an empty String and that should fix your problem.

Another question do you want to analyze that module at all or it can be skipped?

We’ll see what we can do here, but most likely it’s better to explicitly mention that you don’t have test files.

You don’t need to specify sonar.sources and sonar.tests in the default case, but your situation is not default, so it’s valid to specify sonar.tests to be an empty string.

Best,
Margarita

Hi,
no, the folder does not exist (not possible to check in empty folders in git).
Thanks for the workaround, but it doesn’t really address the real issue in my opinion.
I think the analysis should handle that you specify a directory that doesn’t exist that could result in a warning instead of failing the analysis

Regards
Alix

Hi @Alix,

Thanks for your concern. We’ll discuss within the team. while you’re right. There are also cases when the absence of sonar.tests is indeed a bug a misconfiguration, so it would be nice to notice this faster the have an incorrect analysis result.

Best,
Margarita

Hi Margarita, I suggest using this pre-existing “banner” for that warning:

image