spotbugs / discuss

SpotBugs mailing list
6 stars 1 forks source link

BugPattern categories and plugin testing #111

Open gianluca-nitti opened 3 years ago

gianluca-nitti commented 3 years ago

I'm writing a SpotBugs plugin, starting from the maven archetype as suggested in the documentation.

I wrote a detector and some test classes for it. Also, I migrated the test code to JUnit5, using SpotBugsExtension class from the test-harness-jupiter artifact. Everything worked as expected until I tried to change the category of the detected bug, by changing the value of the category attribute of the BugPattern tag for the bug type reported by my detector in findbugs.xml.

The bugs in the "bad case" classes are in fact detected in my tests if the category is the default CORRECTNESS, but not, for example, if I set it to BAD_PRACTICE or STYLE.

Is it possible to instruct the SpotBugsRunner which the JUnit extension injects in my test methods to also detect bugs of these categories?

matthewlowry commented 2 years ago

I stumbled on this same issue when playing around with making a custom detector.

As best I can tell, you can have a "custom" category defined in your plugin descriptor, and that works fine:

<FindbugsPlugin>
        <Detector class="..." reports="MUH_PATTERN" />
        <BugCategory category="PROBLEMATIC" />
        <BugPattern abbrev="FOO" type="MUH_PATTERN" category="PROBLEMATIC" />
</FindbugsPlugin>

The problem comes if your custom detector tries to refer to any of the "built-in" categories other than CORRECTNESS.

Not sure why this is happening but it seems to be some interaction between the way the test harness AnalysisRunner is constructing a UserPreferences instance to pass to the engine.

preferences = UserPreferences.createDefaultUserPreferences();
preferences.getFilterSettings().clearAllCategories(); // <--- Eh?
preferences.enableAllDetectors(true);
engine.setUserPreferences(preferences);
iloveeclipse commented 2 years ago

@matthewlowry : if this is a bug, feel free to provide a patch for it.