-
-
Notifications
You must be signed in to change notification settings - Fork 29
Gracefully skip parsing errors when source files use unsupported encoding #754
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
Merged
+110
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f8e7b7
Coverage paint fails for files containing extended ascii characters
akash-manna-sky c7652d8
Addressed few requested changes
akash-manna-sky 8d8d4b8
Merge branch 'main' into issue-678
akash-manna-sky 9e5ddb8
Fix many PMD and CheckStyle warnings
akash-manna-sky 8873152
Add assertion to verify that extended ASCII characters are rendered c…
akash-manna-sky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
96 changes: 96 additions & 0 deletions
96
plugin/src/test/java/io/jenkins/plugins/coverage/metrics/source/SourceCodePainterTest.java
This file contains hidden or 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,96 @@ | ||
| package io.jenkins.plugins.coverage.metrics.source; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import edu.hm.hafner.coverage.FileNode; | ||
| import edu.hm.hafner.util.FilteredLog; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Verifies that source painting handles files with extended ASCII characters. | ||
| * | ||
| * @author Akash Manna | ||
| */ | ||
| class SourceCodePainterTest { | ||
| private static final Charset WINDOWS_1252 = Charset.forName("windows-1252"); | ||
|
|
||
| @Test | ||
| void shouldPaintSourceFilesWithExtendedAsciiCharacters() throws IOException, InterruptedException { | ||
| Path workspace = Files.createTempDirectory("source-painter"); | ||
| Path sourceFile = workspace.resolve("Example.m"); | ||
|
|
||
| Files.write(sourceFile, List.of( | ||
| "function y = example()", | ||
| "% Copyright 2026, Café Corporation", | ||
| "y = 1;"), WINDOWS_1252); | ||
|
|
||
| var painter = new SourceCodePainter.AgentCoveragePainter( | ||
| List.of(new CoverageSourcePrinter(new FileNode("", "Example.m"))), | ||
| "windows-1252", | ||
| "coverage"); | ||
|
|
||
| FilteredLog log = painter.invoke(workspace.toFile(), null); | ||
|
|
||
| assertThat(log.getErrorMessages()).isEmpty(); | ||
| assertThat(log.getInfoMessages()).contains("-> finished painting successfully"); | ||
|
|
||
| Path outerZipPath = workspace.resolve(SourceCodeFacade.COVERAGE_SOURCES_ZIP); | ||
| assertThat(outerZipPath).exists(); | ||
|
|
||
| byte[] paintedBytes = readPaintedBytesFromNestedZip(outerZipPath); | ||
|
|
||
| var renderedText = new String(paintedBytes, StandardCharsets.UTF_8).replace("\u00A0", " "); | ||
| assertThat(renderedText).contains("Copyright 2026, Café Corporation"); | ||
| } | ||
|
|
||
| private byte[] readPaintedBytesFromNestedZip(final Path outerZipPath) throws IOException { | ||
| try (var outerZip = new ZipInputStream(Files.newInputStream(outerZipPath))) { | ||
| ZipEntry outerEntry = outerZip.getNextEntry(); | ||
|
|
||
| while (outerEntry != null) { | ||
| if (outerEntry.getName().endsWith(SourceCodeFacade.ZIP_FILE_EXTENSION)) { | ||
| byte[] innerZipBytes = outerZip.readAllBytes(); | ||
| byte[] content = extractBytesFromZip(innerZipBytes); | ||
|
|
||
| if (content.length > 0) { | ||
| return content; | ||
| } | ||
| } | ||
|
|
||
| outerEntry = outerZip.getNextEntry(); | ||
| } | ||
| } | ||
|
|
||
| return new byte[0]; | ||
| } | ||
|
|
||
| private byte[] extractBytesFromZip(final byte[] zipBytes) throws IOException { | ||
| var out = new ByteArrayOutputStream(); | ||
|
|
||
| try (var innerZip = new ZipInputStream(new ByteArrayInputStream(zipBytes))) { | ||
| ZipEntry entry = innerZip.getNextEntry(); | ||
|
|
||
| while (entry != null) { | ||
| if (!entry.isDirectory()) { | ||
| out.write(innerZip.readAllBytes()); | ||
| } | ||
|
|
||
| entry = innerZip.getNextEntry(); | ||
| } | ||
| } | ||
|
|
||
| return out.toByteArray(); | ||
| } | ||
|
uhafner marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.