From 9d741b0e2a2f2b2b37f774845d19a48d2ce91719 Mon Sep 17 00:00:00 2001 From: Alberto Carretero Date: Fri, 15 Dec 2023 12:04:26 +0100 Subject: [PATCH] change PrefixEachLine to add newline at the end --- internal/testutil/reindent.go | 9 +++------ internal/testutil/reindent_test.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/internal/testutil/reindent.go b/internal/testutil/reindent.go index ebe1712d..9cd069e9 100644 --- a/internal/testutil/reindent.go +++ b/internal/testutil/reindent.go @@ -43,12 +43,9 @@ func Reindent(in string) []byte { func PrefixEachLine(text string, prefix string) string { var result strings.Builder lines := strings.Split(text, "\n") - for i, line := range lines { - newLine := prefix + line - if i < len(lines)-1 { - newLine += "\n" - } - _, err := result.WriteString(newLine) + for _, line := range lines { + prefixed := prefix + line + "\n" + _, err := result.WriteString(prefixed) if err != nil { panic(err) } diff --git a/internal/testutil/reindent_test.go b/internal/testutil/reindent_test.go index d96c9e1d..4a1ebf21 100644 --- a/internal/testutil/reindent_test.go +++ b/internal/testutil/reindent_test.go @@ -3,8 +3,9 @@ package testutil_test import ( "strings" - "github.com/canonical/chisel/internal/testutil" . "gopkg.in/check.v1" + + "github.com/canonical/chisel/internal/testutil" ) type reindentTest struct { @@ -78,26 +79,26 @@ type prefixEachLineTest struct { var prefixEachLineTests = []prefixEachLineTest{{ raw: "a\n\tb\n \t\tc\td\n\t ", prefix: "foo", - result: "fooa\nfoo\tb\nfoo \t\tc\td\nfoo\t ", + result: "fooa\nfoo\tb\nfoo \t\tc\td\nfoo\t \n", }, { raw: "foo", prefix: "pref", - result: "preffoo", + result: "preffoo\n", }, { raw: "", prefix: "", - result: "", + result: "\n", }, { raw: "\n", prefix: "\t", - result: "\t\n\t", + result: "\t\n\t\n", }} func (s *S) TestPrefixEachLine(c *C) { for _, test := range prefixEachLineTests { c.Logf("Test: %#v", test) - indented := testutil.PrefixEachLine(test.raw, test.prefix) - c.Assert(string(indented), Equals, test.result) + prefixed := testutil.PrefixEachLine(test.raw, test.prefix) + c.Assert(prefixed, Equals, test.result) } }