SonarQube: Scanning process ignores lcov.info

孤者浪人 提交于 2019-12-24 09:09:11

问题


I am scanning a ui project.

The source code is in typescript.

gulp test-coverage generates .js files (which are then scanned for coverage). (each .ts file gets a .js file right next to it, in the same location)

I am pointing the scanner to the lcov.info file as follows:

sonar.javascript.lcov.reportPaths=test-coverage/lcov.info

The problem:

The lcov.info, provides coverage information for .js files

For some reason, SonarQube also provides coverage information for the *.ts files (although not incorporated in the test coverage report).

Why is that?

If I explicitly use

sonar.inclusions=**/*.ts

or

sonar.language=ts

the .js files will be ignored from the coverage report

If I use

sonar.coverage.exclusions=**/*.ts

and no specific inclusions, this will lead to both the .ts and .js files being scanned for errors, which will end up in duplicate errors (after all, .js files are generated by their .ts counterparts.

Any suggestions?

The whole issue of course would just go away, if sonarqube took litteraly the lcov.info and did not take initiatives about scanning other files.)


回答1:


If your source code is written in TypeScript, the coverage report (lcov.info) must contain information about TypeScript files, and not compiled JavaScript. The property you must be using in this case is sonar.typescript.lcov.reportPaths.

You can check out this example (https://github.com/SonarSource/SonarTS-example) to get more details.




回答2:


I have faced the same kind of issue in my Typescript project also,

In order to implement static code analysis for Typescript & code coverage report for compile Javascript. Configure your sonar properties like the following snippet.

Folder Info, app folder contains typescript source files & build folder contains compiled Javascript files

sonar.sources=app/src/,build/src
sonar.exclusions=**/node_modules/**

#Excluding app folder in code coverage analysis
sonar.coverage.exclusions=app/src/**.ts    
sonar.ts.tslint.configPath=tslint.json
sonar.javascript.lcov.reportPaths=reports/lcov.dat

As Javascript is used for code coverage, Sonar project has 2 Quality Profile (Javascript and Typescript).

Both the quality profiles will analyze the sources which had included, it will end up in duplicate errors.

In order to implement static code analysis only for Typescript source. All Linting rules for Javascript needs to be disabled.

Default Javascript Sonarway Quality Profiles rules cannot be customized.

To disable all Javscript rules, create your own Quality Profile for Javascript disable all rules.

Configure the newly created JavaScript Quality Profile for your project.

Here attached the screenshot of my project dashboard Sonarqube project dashboard with quality profile list

This solution worked for me well, Hopefully it will help you too!!!




回答3:


All of the following configurations work together, I also need jest-sonar-reporter to generate the XML file which sonar requires.

sonar properties

npm run sonar-scanner -- \
  -Dsonar.sources=./src \
  -Dsonar.exclusions=**/node_modules/**/*,**/coverage-reports/** \
  -Dsonar.typescript.exclusions=**/node_modules/** \
  -Dsonar.typescript.lcov.reportPaths=coverage-reports/lcov.info \
  # be sure to include your test files
  -Dsonar.test.inclusions=**/__test__/*.spec.ts,**/__test__/*.test.ts \
  # generate by jest sonar reporter at <rootDir>
  -Dsonar.testExecutionReportPaths=test-report.xml \ 
  -Dsonar.coverage.exclusions=src/**/*.tsx

jest.config.json

"testResultsProcessor": "jest-sonar-reporter",


来源:https://stackoverflow.com/questions/48383149/sonarqube-scanning-process-ignores-lcov-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!