Fix SpecsStrings.normalizeFileContents() to always trim lines - #27
Fix SpecsStrings.normalizeFileContents() to always trim lines#27joaobispo wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates SpecsStrings.normalizeFileContents() (in the core SpecsUtils utility library) so that it always trims each line, instead of trimming only when ignoreEmptyLines is enabled.
Changes:
- Refactored
normalizeFileContents(String, boolean)to alwaystrim()lines and conditionally drop empty lines. - Minor cleanup/normalization in imports and Javadoc spacing within
SpecsStrings.java.
Suppressed comments (1)
SpecsUtils/src/pt/up/fe/specs/util/SpecsStrings.java:1586
- Current implementation rebuilds the content by reading lines via StringLines.getLines() and joining them with "\n". Because StringLines ultimately uses BufferedReader.readLine(), it does not preserve trailing empty lines when the input ends with one or more newline characters. As a result, when ignoreEmptyLines is false this method can still drop a final empty line (e.g., "a\n\n" becomes "a\n"), which contradicts the "does not ignore empty lines" behavior.
normalizedString = StringLines.getLines(normalizedString).stream()
.map(String::trim)
// Remove empty lines
.filter(line -> !(ignoreEmptyLines && line.isEmpty()))
.collect(Collectors.joining("\n"));
| normalizedString = StringLines.getLines(normalizedString).stream() | ||
| .map(String::trim) | ||
| // Remove empty lines | ||
| .filter(line -> !(ignoreEmptyLines && line.isEmpty())) | ||
| .collect(Collectors.joining("\n")); |
There was a problem hiding this comment.
Added a NormalizeFileContents nested test class in SpecsStringsTest.java with 8 targeted tests covering:
- Trimming whitespace-only lines (both with and without
ignoreEmptyLines) - Preserving empty lines when
ignoreEmptyLines=false - Removing empty lines when
ignoreEmptyLines=true - Trailing empty line behavior (using double newlines, since
BufferedReader.readLine()absorbs a single trailing newline) - CRLF normalization
- Single-argument overload delegation
Committed in the latest push.
| .collect(Collectors.joining("\n")); | ||
| } | ||
| // Trim strings | ||
| normalizedString = StringLines.getLines(normalizedString).stream() |
There was a problem hiding this comment.
This meaningful changes the behaviour of this function. Check if all uses in the specs-feup org use this with ignoreEmptyLines on. If that is not the case, there should be a path without filtering the empty lines.
It's a 2 line change by not applying the filter to the stream, conditionally.
There was a problem hiding this comment.
Nevermind. Reread the new filter function. Go ahead and merge
SpecsStrings.normalizeFileContents() now always trims lines, instead of only trimming when ignoreLines is enabled.