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

Fix report output to truncate existing files #1951

Merged
merged 5 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
### Fixed
- Fixed spotbugs build with ecj compiler ([#1903](https://github.com/spotbugs/spotbugs/issues/1903))
- Moved tests from spotbugs project to spotbugs-tests project ([#1914](https://github.com/spotbugs/spotbugs/issues/1914))
- Fixed reports to truncate existing files before writing new content ([#1950](https://github.com/spotbugs/spotbugs/issues/1950))

### Added
* New detector `FindInstanceLockOnSharedStaticData` for new bug type `SSD_DO_NOT_USE_INSTANCE_LOCK_ON_SHARED_STATIC_DATA`. This detector reports a bug if an instance level lock is used to modify a shared static data. (See [SEI CERT rule LCK06-J](https://wiki.sei.cmu.edu/confluence/display/java/LCK06-J.+Do+not+use+an+instance+lock+to+protect+shared+static+data))
Expand Down
Expand Up @@ -36,6 +36,19 @@ public void handleOutputFilePathUsesGzip() throws IOException {
assertThat("GZip file should have -117 as its header", written[1], is((byte) -117));
}

@Test
public void handleOutputFileTruncatesExisting() throws IOException {
Path file = Files.createTempFile("spotbugs", ".html");
Files.writeString(file, "content");
TextUICommandLine commandLine = new TextUICommandLine();
SortingBugReporter reporter = new SortingBugReporter();
commandLine.handleOutputFilePath(reporter, "withMessages=" + file.toFile().getAbsolutePath());

reporter.finish();
byte[] written = Files.readAllBytes(file);
assertThat("Output file should be truncated to 0 bytes", written.length, is(0));
}

@Test
public void htmlReportWithOption() throws IOException {
Path xmlFile = Files.createTempFile("spotbugs", ".xml");
Expand Down
Expand Up @@ -278,7 +278,8 @@ public boolean justPrintVersion() {
if (index >= 0) {
Path path = Paths.get(optionExtraPart.substring(index + 1));
try {
OutputStream oStream = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
OutputStream oStream = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
Copy link
Contributor Author

@sdati sdati Feb 8, 2022

Choose a reason for hiding this comment

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

Alternatively, this could have been changed to use the implicit/default behavior of newOutputStream() by not specifying any OpenOptions:

OutputStream oStream = Files.newOutputStream(path);

if ("gz".equals(Util.getFileExtension(path.toFile()))) {
oStream = new GZIPOutputStream(oStream);
}
Expand Down