All my .go files are reported to be "excluded for coverage"

I am trying to get unit test coverage reporting for a Go project. At the moment I am not seeing any coverage details reported in the SonarQube server. I did get this working for a Go project several months ago but that one was not Go Modules based.

Which versions are you using?
13:37:39.616 INFO: SonarQube Scanner 4.0.0.1744
13:37:39.617 INFO: Java 11.0.1 Oracle Corporation (64-bit)
13:37:39.617 INFO: Linux 4.15.0-106-generic amd64
13:37:40.999 INFO: SonarQube server 8.1.0

What are you trying to achieve?
Add Test Coverage reporting to a Go / Golang project. I seem to have managed to add the Test results as I get issues about some Unit Tests not being run. I do not get any coverage reporting on the feature brach underwhich I am trying to add the unit test coder coverage reporting.

What have you tried so far to achieve this?

I have setup my properties file:

# Turn on debugging while working out when Coverage is not shown
sonar.verbose=true

# The name and description displayed in the SonarQube UI
sonar.projectName=ProjName
sonar.projectDescription=A short description

# Project Link Data
sonar.links.homepage=https://...
sonar.links.ci=https://...
sonar.links.issue=...
sonar.links.scm=https://...

# General Project Settings
sonar.coverage.exclusions=./test/**
sonar.exclusions=**/opencv/*.cpp,**/opencv/*.h
sonar.tests=.
sonar.test.exclusions=**/*integration_test.go,**/vendor/**
sonar.test.inclusions=**/*_test.go
sonar.sources=.

# C, C++ & ObjC Settings
# Block C, C++ and Objective-C Analysis to avoid confusing the Python Analysis
sonar.c.file.suffixes=-
sonar.cpp.file.suffixes=-
sonar.objc.file.suffixes=-

# Go Settings
sonar.go.coverage.reportPaths=cover.cov
#sonar.go.golint.reportPaths=golint-report.out
#sonar.go.govet.reportPaths=govet-report.out
sonar.go.tests.reportPaths=unit-test-results.json

I have generated a cover.cov report using: go test -v -count=1 -json .../. | tee unit-test-results.json

It contains lists similar to:

{"Time":"2020-06-25T15:30:31.682179872+02:00","Action":"run","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation"}
{"Time":"2020-06-25T15:30:31.68353597+02:00","Action":"output","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Output":"=== RUN   TestDirectoryCreation\n"}
{"Time":"2020-06-25T15:30:31.690894599+02:00","Action":"output","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Output":"--- PASS: TestDirectoryCreation (0.00s)\n"}
{"Time":"2020-06-25T15:30:31.69224484+02:00","Action":"pass","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestDirectoryCreation","Elapsed":0}
{"Time":"2020-06-25T15:30:31.692372686+02:00","Action":"run","Package":"example.company.com/some/url/stuff/project.git/fileutils","Test":"TestFileCreation"}
...

I have generated a unit-test-results.json report using: `go test -covermode=count -coverprofile=“cover.cov” “./…”

It contains lists similar to:

mode: count
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:15.68,16.49 1 3
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:16.49,18.17 2 3
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:18.17,20.4 1 0
example.company.com/some/url/stuff/project.git/fileutils/fileutils.go:24.96,30.2 4 2

My directory structure goes several layers deep in places but you get the idea. I am using Go Modules.

The possibly related parts from the scanner output look like:

 13:37:44.752 DEBUG:   * SonarGo 1.6.0.719 (go)
...
13:37:46.478 INFO: Indexing files...
 13:37:46.479 INFO: Project configuration:
 13:37:46.480 INFO:   Excluded sources: **/opencv/*.cpp, **/opencv/*.h, **/*_test.go
 13:37:46.480 INFO:   Included tests: **/*_test.go
 13:37:46.480 INFO:   Excluded tests: **/*integration_test.go, **/vendor/**
 13:37:46.481 INFO:   Excluded sources for coverage: **/**
 13:37:46.502 DEBUG: readpipe [git, --version],/usr/bin
 13:37:46.518 DEBUG: readpipe may return 'git version 2.19.1'
 13:37:46.519 DEBUG: remaining output:
 13:37:46.519 DEBUG: readpipe [git, config, --system, --edit],/usr/bin
 13:37:46.527 DEBUG: readpipe may return '/etc/gitconfig'
 13:37:46.527 DEBUG: remaining output:
 13:37:46.637 DEBUG: 487 non excluded files in this Git repository
 13:37:46.651 DEBUG: 'tools/prebuild.go' indexed with language 'go'
 13:37:46.652 DEBUG: File tools/prebuild.go excluded for coverage
 13:37:46.655 DEBUG: 'tools/api/logger/test_resources/anEvent.json' indexed with language 'null'
 13:37:46.655 DEBUG: File tools/api/logger/test_resources/anEvent.json excluded for coverage
...
13:37:47.130 DEBUG: 'modules/event/event_adapter_test.go' indexed as test with language 'go'
 13:37:47.130 DEBUG: File modules/event/event_adapter_test.go excluded for coverage
 13:37:47.131 DEBUG: 'log/logger_utils_test.go' indexed as test with language 'go'
 13:37:47.131 DEBUG: File log/logger_utils_test.go excluded for coverage
 13:37:47.132 INFO: 467 files indexed
 13:37:47.132 INFO: 0 files ignored because of inclusion/exclusion patterns
 13:37:47.132 INFO: 0 files ignored because of scm ignore settings
 13:37:47.133 INFO: Quality profile for go: Frauscher Go way
 13:37:47.133 INFO: Quality profile for py: Frauscher PEP-008 Python Way
 13:37:47.133 INFO: ------------- Run sensors on module Sensonyx
 13:37:47.271 INFO: Load metrics repository
...
 13:37:49.762 DEBUG: 'Import of Bandit issues' skipped because one of the required properties is missing
 13:37:49.763 DEBUG: 'Import of go vet issues' skipped because one of the required properties is missing
 13:37:49.763 DEBUG: 'Import of Golint issues' skipped because one of the required properties is missing
 13:37:49.763 DEBUG: 'Import of GoMetaLinter issues' skipped because one of the required properties is missing
 13:37:49.764 DEBUG: 'Import of GolangCI-Lint issues' skipped because one of the required properties is missing
 13:37:49.764 DEBUG: 'Kotlin Sensor' skipped because there is no related file in current project
...
13:37:50.641 INFO: Sensor SonarGo [go]
 13:37:50.643 INFO: 199 source files to be analyzed
 13:37:50.703 DEBUG: 'test/testsupport/server_testcontainer.go' generated metadata with charset 'UTF-8'
 13:37:50.813 DEBUG: 'modules/tracking/detector/detector_module.go' generated metadata with charset 'UTF-8'
 13:37:50.854 DEBUG: 'modules/tracking/detector/recorder_factory.go' generated metadata with charset 'UTF-8'
...

13:37:54.276 WARN: Failed to find test file for package example.company.com/some/url/stuff/project.git/modules/monitoring/changedetection and test Test_Integration_Detection
 13:37:54.315 INFO: Sensor Go Unit Test Report [go] (done) | time=285ms
 13:37:54.315 INFO: Sensor Go Cover sensor for Go coverage [go]
 13:37:54.316 INFO: Load coverage report from '/builds/rnd/group/projectname/cover.cov'
 13:37:54.420 DEBUG: Resolving file example.company.com/some/url/stuff/project.git/tools/api/controller/event_rate_controller_mock.go using relative path
 13:37:54.421 DEBUG: Saving coverage measures for file 'example.company.com/some/url/stuff/project.git/tools/api/controller/event_rate_controller_mock.go'
...
13:37:54.500 DEBUG: Resolving file example.company.com/some/url/stuff/project.git/modules/tracking/edgedetector/edge_detector.go using relative path
 13:37:54.500 DEBUG: Saving coverage measures for file 'example.company.com/some/url/stuff/project.git/modules/tracking/edgedetector/edge_detector.go'
 13:37:54.501 INFO: Sensor Go Cover sensor for Go coverage [go] (done) | time=186ms
 13:37:54.501 INFO: Sensor JavaSecuritySensor [security]
...
13:37:54.857 DEBUG: Detection of duplications for /builds/rnd/group/projectname/modules/monitoring/changedetection/layout_manager.go
 13:37:54.866 DEBUG: Detection of duplications for /builds/rnd/group/projectname/tools/recorder/recoder.go
...

Hello,

You are probably facing this problem https://jira.sonarsource.com/browse/SONARSLANG-470
It would be great to manually change the paths in your cover.cov file to confirm I’m correct.

Another point: why did you use sonar.coverage.exclusions parameter?

Alex

1 Like

If I am understanding the problem correctly I would need to update these entries:

{“Time”:“2020-06-25T15:30:31.682179872+02:00”,“Action”:“run”,“Package”:“example.company.com/some/url/stuff/project.git/fileutils",“Test”:"TestDirectoryCreation”}

to read something like this:

{“Time”:“2020-06-25T15:30:31.682179872+02:00”,“Action”:“run”,“Package”:"./fileutils",“Test”:“TestDirectoryCreation”}

I can attempt that and report back.

The top level ./test/ directory in my project contains a number of test harnesses and scripts. I do not want them included in the code coverage counts as incuding the code used for these non-production things would dillute the results of the code areas I do care about.

I could ommit the sonar.coverage.exclusions= to see if that is part of the problem as well or to see how it changes the reported values if I get some.

Changing the paths to “./fileutils” does not seem to have changed the behaviour or the issuing of exluded debug messages:

08:15:43.116 DEBUG: 'tools/api/feature/wire_gen.go' indexed with language 'go'
08:15:43.116 DEBUG: File tools/api/feature/wire_gen.go excluded for coverage

I shall comment out the sonar.coverage.exclusions= property and then I will try changing the paths to “fileutils” instead.

I have commented out my setting in the properties file and I still see an INFO message in the scanner output like this. I also see the DEBUG “excluded for coverage” notes for every file that is analysed.

Is the problem that I need to change that pattern somehow?

Setting
sonar.coverage.exclusions="" still gives a report of

 10:40:35.888 INFO: Project configuration:
 10:40:35.889 INFO:   Excluded sources: **/opencv/*.cpp, **/opencv/*.h, **/*_test.go
 10:40:35.889 INFO:   Included tests: **/*_test.go
 10:40:35.889 INFO:   Excluded tests: **/*integration_test.go, **/vendor/**
 10:40:35.890 INFO:   Excluded sources for coverage: **/**
 10:40:35.908 DEBUG: readpipe [git, --version],/usr/bin

So it seems nothing in the properties file will change what is reporting by that “INFO” message for me.

:man_facepalming:

I might have found my problem, at least for the exclusion wildcard.

In the depths of the scripts we run the analysis via is a `"–define", “sonar.coverage.exclusions=/” I assume this command line thing will be overriding the .properties file. I will test changing that and report back later.

My problem is now fixed after I found the exclude command line hiding in a script out CI pipeline uses. I was also seeing the bug you pointed to. Thank you for your help.

That’s a great news!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.