Fix: Unexpected warning "remove this assertion from production code" on sources inside Eclipse test plugin projects#3424
Conversation
When a project contains META-INF/MANIFEST.MF with a Bundle-SymbolicName ending in '.test' or '.tests', its sources are treated as test sources rather than main/production sources. This prevents SonarQube from raising 'remove this assertion from production code' issues on the main sources of Eclipse test plugin projects. - Modified ProjectFilePreprocessor.processModule() to detect Eclipse test plugins via isEclipseTestPlugin() and reclassify their sources as Type.TEST when no explicit sonar.tests configuration is provided. - Added three integration tests in FileSystemMediumIT to verify the behavior for '.tests' suffix, '.test;singleton:=true' suffix, and regular (non-test) Eclipse plugin bundles. Agent-Logs-Url: https://github.com/nbauma109/sonarqube/sessions/746dcad9-c35a-4885-958b-02526ff79f54 Co-authored-by: nbauma109 <9403560+nbauma109@users.noreply.github.com>
…ng attributes Agent-Logs-Url: https://github.com/nbauma109/sonarqube/sessions/746dcad9-c35a-4885-958b-02526ff79f54 Co-authored-by: nbauma109 <9403560+nbauma109@users.noreply.github.com>
…fs/FileSystemMediumIT.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
SummaryThis PR fixes false "remove this assertion" warnings in Eclipse OSGi test plugins by automatically detecting and reclassifying sources as test code. The scanner now reads What reviewers should knowStart here: Look at Non-obvious details:
Test coverage: Four integration tests verify the behavior — check
|
There was a problem hiding this comment.
Pull request overview
Adjusts file indexing in the scanner engine so sources inside Eclipse test plugin bundles are treated as test code, preventing “remove this assertion from production code” warnings from being raised on those sources.
Changes:
- Detect Eclipse test plugin bundles by reading
META-INF/MANIFEST.MFand checkingBundle-SymbolicNamefor.test/.testssuffixes (handling OSGi directives like;singleton:=true). - When a module is an Eclipse test plugin and
sonar.testsis not explicitly configured, indexsonar.sourcesasInputFile.Type.TESTinstead ofMAIN. - Add medium integration tests verifying indexing behavior for
.test,.tests, directive attributes, and non-test bundles.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java |
Adds Eclipse test-bundle detection and conditionally indexes module sources as test files when appropriate. |
sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java |
Adds integration tests covering Eclipse test plugin detection and precedence of explicit sonar.tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| String bundleSymbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName"); | ||
| if (bundleSymbolicName != null) { | ||
| int semicolonIndex = bundleSymbolicName.indexOf(';'); | ||
| if (semicolonIndex != -1) { | ||
| bundleSymbolicName = bundleSymbolicName.substring(0, semicolonIndex).trim(); | ||
| } | ||
| return !bundleSymbolicName.isEmpty() && (bundleSymbolicName.endsWith(".test") || bundleSymbolicName.endsWith(".tests")); | ||
| } |
There was a problem hiding this comment.
bundleSymbolicName is only trim()ed when a semicolon is present. If the manifest header contains leading/trailing whitespace but no attributes, the suffix check can fail and a test bundle may be misclassified. Consider trimming unconditionally before checking .endsWith() (and before/after stripping directives).
| @Test | ||
| void eclipseTestPluginSourcesAreIndexedAsTestFiles() throws IOException { | ||
| File srcDir = new File(baseDir, "src"); | ||
| assertThat(srcDir.mkdir()).isTrue(); | ||
| writeFile(srcDir, "SampleTest.xoo", "Sample test content"); | ||
|
|
||
| File metaInf = new File(baseDir, "META-INF"); | ||
| assertThat(metaInf.mkdir()).isTrue(); |
There was a problem hiding this comment.
PR description says "Three new integration tests" but this diff adds four new @Test cases. Please update the PR description (or drop/merge a test) so the description matches the actual change set.
…system/ProjectFilePreprocessor.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
LGTM! ✅
Solid, well-scoped implementation with no meaningful bugs. The detection logic is correct: hasTests is derived from module.getTestDirsOrFiles().isPresent(), so short-circuit evaluation ensures isEclipseTestPlugin() (which does file I/O) is never called when explicit sonar.tests is configured. MANIFEST.MF parsing is safe — missing Bundle-SymbolicName, absent MANIFEST.MF, and I/O errors all fall through cleanly to return false.
This pull request adds support for correctly classifying Eclipse test plugin sources as test files during analysis, based on the contents of their
MANIFEST.MF. It introduces logic to detect Eclipse test plugins and updates the file preprocessing to assign the proper file type. The changes are covered by new integration tests.Eclipse Test Plugin Support:
isEclipseTestPlugininProjectFilePreprocessor.javato detect Eclipse test plugins by inspecting theBundle-SymbolicNameinMETA-INF/MANIFEST.MF, treating modules ending with.testor.testsas test plugins.processModuleinProjectFilePreprocessor.javato index all source files as test files if the module is detected as an Eclipse test plugin and no explicit test directories are provided.Testing:
FileSystemMediumIT.javato verify correct classification of files as test or main for Eclipse test plugins, regular plugins, and combinations with explicit test directories.Codebase Maintenance:
ProjectFilePreprocessor.java. [1] [2]SonarQube was raising "remove this assertion from production code" on sources inside Eclipse test plugin projects because the scanner unconditionally indexed allsonar.sourcesasType.MAIN, with no awareness of OSGi test bundles.Changes
ProjectFilePreprocessorisEclipseTestPlugin(DefaultInputModule): readsMETA-INF/MANIFEST.MF, parsesBundle-SymbolicName(stripping OSGi directive attributes like;singleton:=true), and returnstrueif the name ends with.testor.tests.processModule(): when the module is detected as an Eclipse test plugin andsonar.testsis not explicitly configured, sources are indexed asType.TESTinstead ofType.MAIN. Explicit user configuration still takes precedence.FileSystemMediumITThree new integration tests cover:
Bundle-SymbolicName: com.example.feature.tests→Type.TESTBundle-SymbolicName: com.example.feature.test;singleton:=true→Type.TEST.test/.testssuffix) →Type.MAIN