-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Task does not fail if report generation fails (#929)
* test: reproduce the reported issue as #909 Signed-off-by: Kengo TODA <[email protected]> * test: fix test case Signed-off-by: Kengo TODA <[email protected]> * fix: Task does not fail if report generation fails Signed-off-by: Kengo TODA <[email protected]> * chore: format code by spotless Signed-off-by: Kengo TODA <[email protected]> * chore: resolve problems reported by SonarQube Signed-off-by: Kengo TODA <[email protected]> --------- Signed-off-by: Kengo TODA <[email protected]>
- Loading branch information
Showing
5 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/functionalTest/groovy/com/github/spotbugs/snom/Issue909.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2023 SpotBugs team | ||
* | ||
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.spotbugs.snom | ||
|
||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import spock.lang.Specification | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
|
||
/** | ||
* @see <a href="https://github.com/spotbugs/spotbugs-gradle-plugin/issues/909">GitHub Issues</a> | ||
*/ | ||
class Issue909 extends Specification { | ||
Path rootDir | ||
Path buildFile | ||
|
||
def setup() { | ||
rootDir = Files.createTempDirectory("spotbugs-gradle-plugin") | ||
buildFile = rootDir.resolve("build.gradle") | ||
buildFile.toFile() << """ | ||
plugins { | ||
id "java" | ||
id "com.github.spotbugs" | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
tasks.spotbugsMain { | ||
reports { | ||
html { | ||
required = true | ||
stylesheet = resources.text.fromString("I am not valid XSL") | ||
} | ||
} | ||
} | ||
""" | ||
Path sourceDir = rootDir.resolve("src").resolve("main").resolve("java") | ||
sourceDir.toFile().mkdirs() | ||
Path sourceFile = sourceDir.resolve("Foo.java") | ||
sourceFile.toFile() << """ | ||
public class Foo { | ||
public static void main(String... args) { | ||
System.out.println("Hello, SpotBugs!"); | ||
} | ||
} | ||
""" | ||
} | ||
|
||
def "cannot generate HTML report if invalid XSL is provided"() { | ||
when: | ||
def result = GradleRunner.create() | ||
.withProjectDir(rootDir.toFile()) | ||
.withArguments('spotbugsMain') | ||
.withPluginClasspath() | ||
.buildAndFail() | ||
|
||
then: | ||
TaskOutcome.FAILED == result.task(":spotbugsMain").outcome | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/groovy/com/github/spotbugs/snom/internal/OutputScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2023 SpotBugs team | ||
* | ||
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.spotbugs.snom.internal; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Monitors the stdout of forked process, and report when it contains some problems reported by | ||
* SpotBugs core. | ||
*/ | ||
class OutputScanner extends FilterOutputStream { | ||
private final ByteArrayOutputStream builder = new ByteArrayOutputStream(); | ||
private boolean failedToReport = false; | ||
|
||
public OutputScanner(OutputStream out) { | ||
super(out); | ||
} | ||
|
||
boolean isFailedToReport() { | ||
return failedToReport; | ||
} | ||
|
||
@Override | ||
public void write(byte @NotNull [] b, int off, int len) throws IOException { | ||
super.write(b, off, len); | ||
builder.write(b, off, len); | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
super.write(b); | ||
builder.write(b); | ||
|
||
if (b == '\n') { | ||
String line = builder.toString(); | ||
if (line.contains("Could not generate HTML output")) { | ||
failedToReport = true; | ||
} | ||
|
||
builder.reset(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters