-
Notifications
You must be signed in to change notification settings - Fork 53
#405 Fix "File name too long" error with deep paths in JUnit reports #426
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
Open
ascheman
wants to merge
4
commits into
develop
Choose a base branch
from
bugfix/405-enable-junit-results-per-dir
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||
| package org.aim42.htmlsanitycheck.report; | ||||||||||||||||
|
|
||||||||||||||||
| import org.aim42.htmlsanitycheck.Configuration; | ||||||||||||||||
| import org.aim42.htmlsanitycheck.collect.Finding; | ||||||||||||||||
| import org.aim42.htmlsanitycheck.collect.PerRunResults; | ||||||||||||||||
| import org.aim42.htmlsanitycheck.collect.SingleCheckResults; | ||||||||||||||||
|
|
@@ -36,13 +37,25 @@ | |||||||||||||||
| /** | ||||||||||||||||
| * Write the findings' report to JUnit XML. Allows tools processing JUnit to | ||||||||||||||||
| * include the findings. | ||||||||||||||||
| * <p> | ||||||||||||||||
| * Supports two output styles: | ||||||||||||||||
| * <ul> | ||||||||||||||||
| * <li>{@link Configuration.JunitOutputStyle#FLAT} - All files in one directory with encoded paths (default, backwards compatible)</li> | ||||||||||||||||
| * <li>{@link Configuration.JunitOutputStyle#HIERARCHICAL} - Files organized in subdirectories mirroring source structure</li> | ||||||||||||||||
| * </ul> | ||||||||||||||||
| */ | ||||||||||||||||
| public class JUnitXmlReporter extends Reporter { | ||||||||||||||||
| File outputPath; | ||||||||||||||||
| Configuration.JunitOutputStyle outputStyle; | ||||||||||||||||
|
|
||||||||||||||||
| public JUnitXmlReporter(PerRunResults runResults, String outputPath) { | ||||||||||||||||
| this(runResults, outputPath, Configuration.JunitOutputStyle.FLAT); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public JUnitXmlReporter(PerRunResults runResults, String outputPath, Configuration.JunitOutputStyle outputStyle) { | ||||||||||||||||
| super(runResults); | ||||||||||||||||
| this.outputPath = new File(outputPath); | ||||||||||||||||
| this.outputStyle = outputStyle != null ? outputStyle : Configuration.JunitOutputStyle.FLAT; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
|
|
@@ -52,11 +65,15 @@ protected void initReport() { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // tag::reportPageSummary[] | ||||||||||||||||
| @Override | ||||||||||||||||
| protected void reportPageSummary(SinglePageResults singlePageResults) { | ||||||||||||||||
| String name = filenameOrTitleOrRandom(singlePageResults); | ||||||||||||||||
| String sanitizedPath = name.replaceAll("[^A-Za-z0-9_-]+", "_"); | ||||||||||||||||
| File testOutputFile = new File(outputPath, "TEST-unit-html-" + sanitizedPath + ".xml"); | ||||||||||||||||
|
|
||||||||||||||||
| File testOutputFile = (outputStyle == Configuration.JunitOutputStyle.HIERARCHICAL) | ||||||||||||||||
| ? getHierarchicalOutputFile(name) | ||||||||||||||||
| : getFlatOutputFile(name); | ||||||||||||||||
| // end::reportPageSummary[] | ||||||||||||||||
|
|
||||||||||||||||
| XMLOutputFactory factory = XMLOutputFactory.newInstance(); | ||||||||||||||||
| try (FileWriter fileWriter = new FileWriter(testOutputFile)) { | ||||||||||||||||
|
|
@@ -96,6 +113,63 @@ protected void reportPageSummary(SinglePageResults singlePageResults) { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Creates output file using flat structure (all files in one directory). | ||||||||||||||||
| * Encodes the full path into the filename using underscores. | ||||||||||||||||
| * | ||||||||||||||||
| * @param name The source file path | ||||||||||||||||
| * @return The output file for the JUnit XML report | ||||||||||||||||
| */ | ||||||||||||||||
| private File getFlatOutputFile(String name) { | ||||||||||||||||
| String sanitizedPath = name.replaceAll("[^A-Za-z0-9_-]+", "_"); | ||||||||||||||||
| return new File(outputPath, "TEST-unit-html-" + sanitizedPath + ".xml"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Creates output file using hierarchical structure (subdirectories mirror source structure). | ||||||||||||||||
| * Solves filename length issues with deeply nested directories. | ||||||||||||||||
| * | ||||||||||||||||
| * @param name The source file path | ||||||||||||||||
| * @return The output file for the JUnit XML report | ||||||||||||||||
| */ | ||||||||||||||||
| private File getHierarchicalOutputFile(String name) { | ||||||||||||||||
| // Parse the path to extract directory structure and filename | ||||||||||||||||
| File sourcePath = new File(name); | ||||||||||||||||
| File parentDir = sourcePath.getParentFile(); | ||||||||||||||||
| String fileName = sourcePath.getName(); | ||||||||||||||||
|
|
||||||||||||||||
| // Create directory structure under outputPath to mirror the source file hierarchy | ||||||||||||||||
| File testOutputDir; | ||||||||||||||||
| if (parentDir != null) { | ||||||||||||||||
| // Normalize the path to handle relative references like ".." | ||||||||||||||||
| // This ensures we stay within the outputPath and don't try to escape it | ||||||||||||||||
| try { | ||||||||||||||||
| File tempPath = new File(outputPath, parentDir.getPath()); | ||||||||||||||||
| testOutputDir = tempPath.getCanonicalFile(); | ||||||||||||||||
|
|
||||||||||||||||
| // Verify the canonical path is still under outputPath | ||||||||||||||||
| if (!testOutputDir.getAbsolutePath().startsWith(outputPath.getCanonicalPath())) { | ||||||||||||||||
| // Path tries to escape outputPath, so just use outputPath directly | ||||||||||||||||
| testOutputDir = outputPath; | ||||||||||||||||
| } | ||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||
| // If normalization fails, fall back to outputPath | ||||||||||||||||
| testOutputDir = outputPath; | ||||||||||||||||
| } | ||||||||||||||||
| } else { | ||||||||||||||||
| testOutputDir = outputPath; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Ensure the directory exists | ||||||||||||||||
| if (!testOutputDir.exists() && !testOutputDir.mkdirs()) { | ||||||||||||||||
| throw new RuntimeException("Cannot create directory " + testOutputDir); //NOSONAR(S112) | ||||||||||||||||
|
||||||||||||||||
| throw new RuntimeException("Cannot create directory " + testOutputDir); //NOSONAR(S112) | |
| StringBuilder errorMsg = new StringBuilder("Cannot create directory: ") | |
| .append(testOutputDir.getAbsolutePath()); | |
| errorMsg.append(" (exists: ").append(testOutputDir.exists()) | |
| .append(", canWrite: ").append(testOutputDir.getParentFile() != null ? testOutputDir.getParentFile().canWrite() : "unknown") | |
| .append(")"); | |
| throw new RuntimeException(errorMsg.toString()); //NOSONAR(S112) |
Oops, something went wrong.
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.