Skip to content

Fix SpecsStrings.normalizeFileContents() to always trim lines - #27

Open
joaobispo wants to merge 3 commits into
stagingfrom
fix/specs-strings-normalization
Open

Fix SpecsStrings.normalizeFileContents() to always trim lines#27
joaobispo wants to merge 3 commits into
stagingfrom
fix/specs-strings-normalization

Conversation

@joaobispo

Copy link
Copy Markdown
Member

SpecsStrings.normalizeFileContents() now always trims lines, instead of only trimming when ignoreLines is enabled.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 always trim() 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"));

Comment on lines +1582 to +1586
normalizedString = StringLines.getLines(normalizedString).stream()
.map(String::trim)
// Remove empty lines
.filter(line -> !(ignoreEmptyLines && line.isEmpty()))
.collect(Collectors.joining("\n"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nevermind. Reread the new filter function. Go ahead and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants