From 96c39548b40534917c81a4c7c8a338c3a7a8190f Mon Sep 17 00:00:00 2001 From: Simon Alling Date: Fri, 17 Dec 2021 17:28:50 +0100 Subject: [PATCH] Fix line count off-by-one error Signed-off-by: Simon Alling --- internal/test/test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/test/test.go b/internal/test/test.go index ea01d5bc7..5164fedce 100644 --- a/internal/test/test.go +++ b/internal/test/test.go @@ -91,8 +91,8 @@ func AssertGoldenStringWithCustomLineValidation(t TestingT, checkLine func(expec if err != nil { t.Fatalf("%v", err) } - expectedLines := strings.Split(string(normalize(expectedOutput)), "\n") - actualLines := strings.Split(string(normalize([]byte(actualOutput))), "\n") + expectedLines := lines(expectedOutput) + actualLines := lines([]byte(actualOutput)) expectedLineCount := len(expectedLines) actualLineCount := len(actualLines) for i := 0; i < max(expectedLineCount, actualLineCount); i++ { @@ -118,6 +118,10 @@ func AssertGoldenStringWithCustomLineValidation(t TestingT, checkLine func(expec } } +func lines(raw []byte) []string { + return strings.Split(strings.TrimSuffix(string(normalize(raw)), "\n"), "\n") // We first remove the final newline (if any), so that e.g. the 2-line string "a\nb\n" is mapped to ["a", "b"] and not ["a", "b", ""]. +} + func path(filename string) string { if filepath.IsAbs(filename) { return filename