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

feat: allow stack trace suppression (#266) #275

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Configure `spotbugs` extension to configure the behaviour of tasks:
```groovy
spotbugs {
ignoreFailures = false
showStackTraces = true
showProgress = true
effort = 'default'
reportLevel = 'default'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,44 @@ spotbugs {

then:
result.task(':spotbugsMain').outcome == TaskOutcome.SUCCESS
result.output.contains('\tat ')

where:
isWorkerApi << [true, false]
}

@Unroll
def 'build does not show stack traces when bugs are found with `showStacktraces = false` (Worker API? #isWorkerApi)'() {
given:
def badCode = new File(rootDir, 'src/main/java/Bar.java')
badCode << '''
|public class Bar {
| public int unreadField = 42; // warning: URF_UNREAD_FIELD
|}
|'''.stripMargin()

buildFile << """
spotbugs {
ignoreFailures = true
showStackTraces = false
}"""
when:
def arguments = [':spotbugsMain', '-is']
if(!isWorkerApi) {
arguments.add('-Pcom.github.spotbugs.snom.worker=false')
}
def runner = GradleRunner.create()
.withProjectDir(rootDir)
.withArguments(arguments)
.withPluginClasspath()
.forwardOutput()
.withGradleVersion(version)

def result = runner. build()

then:
result.task(':spotbugsMain').outcome == TaskOutcome.SUCCESS
!(result.output.contains('\tat '))

where:
isWorkerApi << [true, false]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.gradle.api.provider.Property;
* <p>After you apply the SpotBugs Gradle plugin to project, write extension like below:<div><code>
* spotbugs {<br>
* &nbsp;&nbsp;&nbsp;&nbsp;ignoreFailures = false<br>
* &nbsp;&nbsp;&nbsp;&nbsp;showStackTraces = true<br>
* &nbsp;&nbsp;&nbsp;&nbsp;showProgress = false<br>
* &nbsp;&nbsp;&nbsp;&nbsp;reportLevel = 'default'<br>
* &nbsp;&nbsp;&nbsp;&nbsp;effort = 'default'<br>
Expand All @@ -57,6 +58,8 @@ class SpotBugsExtension {

@NonNull
final Property<Boolean> ignoreFailures;
@NonNull
final Property<Boolean> showStackTraces;
/**
* Property to enable progress reporting during the analysis. Default value is {@code false}.
*/
Expand Down Expand Up @@ -150,6 +153,7 @@ class SpotBugsExtension {
@Inject
SpotBugsExtension(Project project, ObjectFactory objects) {
ignoreFailures = objects.property(Boolean).convention(false);
showStackTraces = objects.property(Boolean).convention(true);
showProgress = objects.property(Boolean);
reportLevel = objects.property(Confidence);
effort = objects.property(Effort);
Expand Down
17 changes: 17 additions & 0 deletions src/main/groovy/com/github/spotbugs/snom/SpotBugsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import javax.inject.Inject
* &nbsp;&nbsp;&nbsp;&nbsp;auxClassPaths = sourceSets.main.compileClasspath<br>
* <br>
* &nbsp;&nbsp;&nbsp;&nbsp;ignoreFailures = false<br>
* &nbsp;&nbsp;&nbsp;&nbsp;showStackTraces = true<br>
* &nbsp;&nbsp;&nbsp;&nbsp;showProgress = false<br>
* &nbsp;&nbsp;&nbsp;&nbsp;reportLevel = 'default'<br>
* &nbsp;&nbsp;&nbsp;&nbsp;effort = 'default'<br>
Expand Down Expand Up @@ -92,6 +93,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
private final WorkerExecutor workerExecutor;

@NonNull final Property<Boolean> ignoreFailures;
@NonNull final Property<Boolean> showStackTraces;
/**
* Property to enable progress reporting during the analysis. Default value is {@code false}.
*/
Expand Down Expand Up @@ -279,6 +281,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
sourceDirs = objects.fileCollection()
auxClassPaths = objects.fileCollection()
ignoreFailures = objects.property(Boolean)
showStackTraces = objects.property(Boolean)
showProgress = objects.property(Boolean);
reportLevel = objects.property(Confidence);
effort = objects.property(Effort);
Expand Down Expand Up @@ -318,6 +321,7 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
*/
void init(SpotBugsExtension extension) {
ignoreFailures.convention(extension.ignoreFailures)
showStackTraces.convention(extension.showStackTraces)
showProgress.convention(extension.showProgress)
reportLevel.convention(extension.reportLevel)
effort.convention(extension.effort)
Expand Down Expand Up @@ -410,11 +414,24 @@ class SpotBugsTask extends DefaultTask implements VerificationTask {
ignoreFailures.set(b);
}

void setShowStackTraces(Provider<Boolean> b) {
showStackTraces.set(b);
}

void setShowStackTraces(boolean b) {
showStackTraces.set(b)
}

@Input
boolean getIgnoreFailures() {
ignoreFailures.get();
}

@Input
boolean getShowStackTraces() {
showStackTraces.get();
}

@Internal
String getBaseName() {
String prunedName = name.replaceFirst("spotbugs", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void run(@NonNull SpotBugsTask task) {
task.getProject().javaexec(configureJavaExec(task)).rethrowFailure().assertNormalExitValue();
} catch (ExecException e) {
if (task.getIgnoreFailures()) {
log.warn("SpotBugs reported failures", e);
log.warn("SpotBugs reported failures", task.getShowStackTraces() ? e : null);
} else {
String errorMessage = "Verification failed: SpotBugs execution thrown exception.";
List<String> reportPaths =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ private Action<SpotBugsWorkParameters> configureWorkParameters(SpotBugsTask task
return params -> {
params.getArguments().addAll(buildArguments(task));
params.getIgnoreFailures().set(task.getIgnoreFailures());
params.getShowStackTraces().set(task.getShowStackTraces());
};
}

interface SpotBugsWorkParameters extends WorkParameters {
ListProperty<String> getArguments();

Property<Boolean> getIgnoreFailures();

Property<Boolean> getShowStackTraces();
}

public abstract static class SpotBugsExecutor implements WorkAction<SpotBugsWorkParameters> {
Expand Down Expand Up @@ -107,7 +110,9 @@ public void execute() {
}
} catch (GradleException e) {
if (params.getIgnoreFailures().getOrElse(Boolean.FALSE).booleanValue()) {
log.warn("SpotBugs reported failures", e);
final boolean showStackTraces =
params.getShowStackTraces().getOrElse(Boolean.TRUE).booleanValue();
log.warn("SpotBugs reported failures", showStackTraces ? e : null);
} else {
throw e;
}
Expand Down