SonarQube Analysis With Ginkgo on Mac
Ginkgo is the testing framework of choice for many projects developed in Golang. Find out how to setup SonarQube analysis for Golang projects on Mac.
Join the DZone community and get the full member experience.
Join For FreeGinkgo is the testing framework of choice for many projects developed in Golang. Here is an example setup for SonarQube project analysis.
Prerequisites
- SonarQube server installation: For the purpose of the tutorial I have a docker image running on my machine on http://localhost:9000.
- SonarQube project: For the purpose of the tutorial I have created a project with project key
com.bdpanajotova.golang-sonar-example
and nameGolang Sonar Example
. - Golang project for analysis with Ginkgo tests: Here is the example project I use in GitHub.
Here is the documentation for the fast local setup of SonarQube.
My environment is macOS Big Sur 11.3.
Step 1: Setup sonar-scanner
Download the respective version of SonarScanner for your system: sonar-scanner.
Unzip the downloaded file and export it in your PATH
.
% unzip sonar-scanner-cli-4.6.2.2472-macosx.zip
Archive: sonar-scanner-cli-4.6.2.2472-macosx.zip
creating: sonar-scanner-4.6.2.2472-macosx/
...
% pwd
/Users/barzana.nikolova/Downloads
% export PATH=$PATH:/Users/barzana.nikolova/Downloads/sonar-scanner-4.6.2.2472-macosx/bin
% sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
INFO: Options:
INFO: -D,--define <arg> Define property
INFO: -h,--help Display help information
INFO: -v,--version Display version information
INFO: -X,--debug Produce execution debug output
Step 2: Create sonar-scanner.properties File
Create a sonar-scanner.properties
file in your project root directory.
#----- SonarQube Analysis Setup
sonar.host.url=http://localhost:9000
sonar.projectKey=com.bdpanajotova.golang-sonar-example
sonar.projectName=Golang Sonar Example
sonar.login=b58b1848e45247651387fe23dd580e464e67be49
# Generated token in SonarQube - http://localhost:9000/account/security/
sonar.sources=.
sonar.exclusions=**/*_test.go
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.go.coverage.reportPaths=.coverage/coverage.out
This configuration expects a single coverage report in folder .coverage/coverage.out
.
Step 3: Generate Coverage Report
From the root project directory, execute the following to generate a coverage report file:
% ginkgo -cover -coverprofile=coverage.out -outputdir=.coverage ./...
Running Suite: Animal Suite
===========================
Random Seed: 1634367783
Will run 2 of 2 specs
••
Ran 2 of 2 Specs in 0.000 seconds
SUCCESS! -- 2 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
coverage: 66.7% of statements
path is /Users/barzana.nikolova/GolandProjects/golang-sonar-example/.coverage
All profiles combined
Ginkgo ran 1 suite in 4.455071542s
Test Suite Passed
barzana.nikolova@Krasimirs-MacBook-Pro golang-sonar-example %
This command combines all the test coverage output in one file that will be used by sonar-scanner when analyzing your project.
Step 4: Run sonar-scanner
To analyze your project, run the following command:
% sonar-scanner -Dproject.settings=sonar-scanner.properties
INFO: Scanner configuration file: /Users/barzana.nikolova/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /Users/barzana.nikolova/GolandProjects/golang-sonar-example/sonar-scanner.properties
INFO: SonarScanner 4.6.2.2472
INFO: Java 11.0.11 AdoptOpenJDK (64-bit)
INFO: Mac OS X 11.3 x86_64
...
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=com.bdpanajotova.golang-sonar-example
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AXyH7DsT6By06DIoRBpH
INFO: Analysis total time: 5.955 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 7.804s
INFO: Final Memory: 14M/54M
INFO: ------------------------------------------------------------------------
barzana.nikolova@Krasimirs-MacBook-Pro golang-sonar-example %
Step 5: See the Project Analysis in SonarQube
Here is the overall status of the project:
I hope it has become clear how to configure simple sonar scans for Golang Ginkgo projects.
I welcome your thoughts and feedback.
Opinions expressed by DZone contributors are their own.
Comments