Skip to content

Fix: Unexpected warning "remove this assertion from production code" on sources inside Eclipse test plugin projects#3424

Open
nbauma109 wants to merge 5 commits intoSonarSource:masterfrom
nbauma109:copilot/fix-sonarqube-assertion-issue
Open

Fix: Unexpected warning "remove this assertion from production code" on sources inside Eclipse test plugin projects#3424
nbauma109 wants to merge 5 commits intoSonarSource:masterfrom
nbauma109:copilot/fix-sonarqube-assertion-issue

Conversation

@nbauma109
Copy link
Copy Markdown

@nbauma109 nbauma109 commented Apr 10, 2026

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:

  • Added a new method isEclipseTestPlugin in ProjectFilePreprocessor.java to detect Eclipse test plugins by inspecting the Bundle-SymbolicName in META-INF/MANIFEST.MF, treating modules ending with .test or .tests as test plugins.
  • Updated processModule in ProjectFilePreprocessor.java to 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:

  • Added several integration tests in FileSystemMediumIT.java to verify correct classification of files as test or main for Eclipse test plugins, regular plugins, and combinations with explicit test directories.

Codebase Maintenance:

  • Added necessary imports for manifest processing and testing annotations in 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 all sonar.sources as Type.MAIN, with no awareness of OSGi test bundles.

Changes

ProjectFilePreprocessor

  • Added isEclipseTestPlugin(DefaultInputModule): reads META-INF/MANIFEST.MF, parses Bundle-SymbolicName (stripping OSGi directive attributes like ;singleton:=true), and returns true if the name ends with .test or .tests.
  • Modified processModule(): when the module is detected as an Eclipse test plugin and sonar.tests is not explicitly configured, sources are indexed as Type.TEST instead of Type.MAIN. Explicit user configuration still takes precedence.
// com.example.myfeature.tests → sources treated as Type.TEST
// com.example.myfeature       → sources treated as Type.MAIN (unchanged)

FileSystemMediumIT

Three new integration tests cover:

  • Bundle-SymbolicName: com.example.feature.testsType.TEST
  • Bundle-SymbolicName: com.example.feature.test;singleton:=trueType.TEST
  • Regular plugin (no .test/.tests suffix) → Type.MAIN

Copilot AI and others added 4 commits April 10, 2026 06:11
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>
Copilot AI review requested due to automatic review settings April 10, 2026 07:12
@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha bot commented Apr 10, 2026

Summary

This 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 META-INF/MANIFEST.MF, checks if Bundle-SymbolicName ends with .test or .tests, and treats those sources as Type.TEST instead of Type.MAIN — but only when sonar.tests is not explicitly configured. Explicit user configuration always takes precedence.

What reviewers should know

Start here: Look at ProjectFilePreprocessor.processModule() to see the conditional logic that gates the test-plugin detection — it only activates when both conditions are true: (1) no explicit sonar.tests is set, and (2) isEclipseTestPlugin() returns true.

Non-obvious details:

  • The isEclipseTestPlugin() method correctly strips OSGi directive syntax (e.g., ;singleton:=true) before checking the suffix, so bundles like com.example.test;singleton:=true are properly detected.
  • This is a safe, opt-in change: it only affects projects with a properly formatted MANIFEST.MF file and only when the naming pattern matches. Regular plugins (no .test/.tests suffix) are unaffected.
  • Explicit configuration always wins: if a project has both a test-plugin name AND an explicit sonar.tests setting, the user's setting is respected.

Test coverage: Four integration tests verify the behavior — check FileSystemMediumIT for the singular suffix (.test), plural suffix (.tests), directive handling, explicit config precedence, and regular plugins remaining as MAIN type.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.MF and checking Bundle-SymbolicName for .test / .tests suffixes (handling OSGi directives like ;singleton:=true).
  • When a module is an Eclipse test plugin and sonar.tests is not explicitly configured, index sonar.sources as InputFile.Type.TEST instead of MAIN.
  • 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.

Comment on lines +174 to +181
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"));
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

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).

Copilot uses AI. Check for mistakes.
Comment on lines +1672 to +1679
@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();
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
…system/ProjectFilePreprocessor.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

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.

🗣️ Give feedback

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.

4 participants