Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Generated sonar-project.properties Configuration #120

Merged
merged 3 commits into from
Jul 15, 2022

Conversation

jungleraptor
Copy link
Contributor

@jungleraptor jungleraptor commented Jul 14, 2022

Fixes test and header files not being analyzed by sonarcloud.

From the sonarcloud documentation:

The sonar.tests property is used to identify the directories containing test source files. Identifying test files helps the analyzers to tune their rules. For example, the analyzers can enable test-specific rules and disable rules that don't make sense in the context of testing.

The C/C++/Objective-C analyzer currently analyzes main and test source files in the same way. Consequently, sonar.tests is not yet supported; the analyzer ignores it.

If you wish to analyze test source files, you should include them in the sonar.sources property.

Since we were putting all of our test files under sonar.test.inclusions, they were being ignored by the scanner.

This changes the sonar.inclusions property to sonar.sources, and appends the test paths to it instead of sonar.tests. With these changes, header and test files are properly analyzed by the sonar-scanner.

Copy link
Contributor

@RReichert RReichert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the slight refactor, I see no issue. Would be nice to see a PR that uses this cmake changes to see that this actually solves the issue, but I'm okay without it as well.

I'm still shocked that this was what was required to make the header files visible. To me its completely illogical that that is how sonarcloud expects things, there is no distinction between project code and test code.

Sonarcloud.cmake Outdated
Comment on lines 197 to 205
list(APPEND source_targets ${test_targets})

_extract_sonarcloud_project_files(source_source_files source_include_directories ${source_targets})
_extract_sonarcloud_project_files(test_source_files test_include_directories ${test_targets})

set(sonarcloud_project_properties_content "sonar.sourceEncoding=UTF-8\n")

set(source_files ${source_source_files} ${source_include_directories})
list(JOIN source_files ",${_sonarcloud_newline}" sonar_sources)
string(APPEND sonarcloud_project_properties_content "sonar.inclusions=${_sonarcloud_newline}${sonar_sources}\n")

set(test_files ${test_source_files})
list(JOIN test_files ",${_sonarcloud_newline}" sonar_tests)
string(APPEND sonarcloud_project_properties_content "sonar.tests.inclusions=${_sonarcloud_newline}${sonar_tests}\n")
string(APPEND sonarcloud_project_properties_content "sonar.sources=${_sonarcloud_newline}${sonar_sources}\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to refactor this to:

  _extract_sonarcloud_project_files(source_source_files source_include_directories ${source_targets})
  _extract_sonarcloud_project_files(test_source_files test_include_directories ${test_targets})

  set(sonarcloud_project_properties_content "sonar.sourceEncoding=UTF-8\n")
  set(source_files ${source_source_files} ${source_include_directories} ${test_source_files})
  list(JOIN source_files ",${_sonarcloud_newline}" sonar_sources)
  string(APPEND sonarcloud_project_properties_content "sonar.sources=${_sonarcloud_newline}${sonar_sources}\n")

I believe this keeps the same logic, but it just makes it clearer that project source files are source_source_files and test files are actually test_source_files and for the purposes of sonarcloud's implementation we combine them under sonar.sources.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@jungleraptor jungleraptor merged commit 3550197 into master Jul 15, 2022
@jungleraptor jungleraptor deleted the itorres/try_fix_sonar_headers branch July 15, 2022 20:06
jungleraptor added a commit that referenced this pull request Jul 29, 2022
jungleraptor added a commit that referenced this pull request Jul 30, 2022
* Revert "sonarcloud: Use Inclusions Property (#121)"

This reverts commit 1773f34.

* Revert "Fix Sonarcloud sonar-project.properties (#120)"

This reverts commit 3550197.

Rolling back recent sonarcloud changes until we get a better grip
on how to properly configure C/C++ projects.
krisukox pushed a commit that referenced this pull request Aug 2, 2022
Fixes the emitted sonar.properties file so that header and test files
are picked up by Sonarcloud analysis.

According to the docs `sonar.tests` is ignored for C/C++ code, so
test paths should be treated the same as source paths.

Also switches `sonar.inclusions` with `sonar.sources` to fix the include
paths being filtered out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants