Skip to content

fix: take trailing newlines into account in hasContent (#2993) - #4344

Open
arimu1 wants to merge 1 commit into
assertj:mainfrom
arimu1:fix/2993-hasContent-trailing-newline
Open

arimu1 wants to merge 1 commit into
assertj:mainfrom
arimu1:fix/2993-hasContent-trailing-newline

Conversation

@arimu1

@arimu1 arimu1 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Check List:

Description

Team decision on #2993: keep 3.x behavior, but in 4.0 take newlines into account for hasContent.

Diff used BufferedReader.readLine(), which drops the distinction between content with and without a single trailing line terminator ("a" vs "a\n"). That made:

assertThat(path).hasContent("Hello, world!")
                .hasContent("Hello, world!\n"); // both passed

Change: read full text and split with split("\\R", -1) so a final terminator is kept as a trailing empty line. Line-ending style (\n / \r / \r\n) stays normalized; only presence/absence of a trailing terminator (and other real line differences) matters.

Javadoc: Path / File / InputStream hasContent no longer claim newlines are ignored; document exact comparison and point to content() / asString() + isEqualToIgnoringNewlines when that is desired.

Tests: trailing-newline pass/fail cases on Paths_assertHasTextualContent, Diff File/String and InputStream/String; existing File→String fixtures updated so expected strings include the trailing separator written by TextFileWriter / resource files.

Verified locally (JDK 25+/Homebrew OpenJDK 26, ./mvnw):

  • Diff_diff_File_*, Files_assertHasContent_*, size/hasContent unit tests
  • Paths_assertHasTextualContent_Test, Diff_diff_InputStream_*, InputStreamAssert_hasContent_Test

BREAKING CHANGE: Path/File/InputStream hasContent (and related Diff-based
content comparisons) no longer ignore a single trailing newline. Lines are
split with trailing empty segments preserved so "a" and "a\n" differ, while
line-ending style (\n/\r/\r\n) remains normalized.

This branch has not been deployed

No deployments
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.

AbstractPathAssert.hasContent ignores a single trailing newline

1 participant