From 76e4bde1c75510e5c51d057daf89e80da48e883a Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 11 Aug 2021 20:38:09 +0000 Subject: [PATCH 1/2] chore: Add line returns to clog output --- pkg/clog/clog.go | 4 ++-- pkg/clog/clog_test.go | 8 ++++---- pkg/clog/errgroup_test.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/clog/clog.go b/pkg/clog/clog.go index 0a523e1f..0c88a29b 100644 --- a/pkg/clog/clog.go +++ b/pkg/clog/clog.go @@ -35,12 +35,12 @@ type CLIError struct { // String formats the CLI message for consumption by a human. func (m CLIMessage) String() string { var str strings.Builder - str.WriteString(fmt.Sprintf("%s: %s\n", + str.WriteString(fmt.Sprintf("%s: %s\r\n", color.New(m.Color).Sprint(m.Level), color.New(color.Bold).Sprint(m.Header)), ) for _, line := range m.Lines { - str.WriteString(fmt.Sprintf(" %s %s\n", color.New(m.Color).Sprint("|"), line)) + str.WriteString(fmt.Sprintf(" %s %s\r\n", color.New(m.Color).Sprint("|"), line)) } return str.String() } diff --git a/pkg/clog/clog_test.go b/pkg/clog/clog_test.go index 51eab07e..6967eb3c 100644 --- a/pkg/clog/clog_test.go +++ b/pkg/clog/clog_test.go @@ -25,7 +25,7 @@ func TestError(t *testing.T) { output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) - assert.Equal(t, "output is as expected", "error: fake error\n\n", string(output)) + assert.Equal(t, "output is as expected", "error: fake error\r\n\n", string(output)) }) t.Run("plain-error", func(t *testing.T) { @@ -41,7 +41,7 @@ func TestError(t *testing.T) { output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) - assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\n\n", string(output)) + assert.Equal(t, "output is as expected", "fatal: wrap 1: base error\r\n\n", string(output)) }) t.Run("message", func(t *testing.T) { @@ -58,7 +58,7 @@ func TestError(t *testing.T) { output, err := ioutil.ReadAll(&buf) assert.Success(t, "read all stderr output", err) - assert.Equal(t, "output is as expected", f.level+": testing\n | hint: maybe do \"this\"\n | \n | cause: what happened was \"this\"\n", string(output)) + assert.Equal(t, "output is as expected", f.level+": testing\r\n | hint: maybe do \"this\"\r\n | \r\n | cause: what happened was \"this\"\r\n", string(output)) } }) @@ -78,7 +78,7 @@ func TestError(t *testing.T) { assert.Equal(t, "output is as expected", - "error: fake header\n | next line\n | \n | tip: content of fake tip\n\n", + "error: fake header\r\n | next line\r\n | \r\n | tip: content of fake tip\r\n\n", string(output), ) }) diff --git a/pkg/clog/errgroup_test.go b/pkg/clog/errgroup_test.go index b632921d..0d209d81 100644 --- a/pkg/clog/errgroup_test.go +++ b/pkg/clog/errgroup_test.go @@ -37,7 +37,7 @@ func TestErrGroup(t *testing.T) { err := egroup.Wait() assert.ErrorContains(t, "error group wait", err, "2 failures emitted") - assert.True(t, "log buf contains", strings.Contains(buf.String(), "fatal: whoops\n\n")) - assert.True(t, "log buf contains", strings.Contains(buf.String(), "error: rich error\n | second line\n\n")) + assert.True(t, "log buf contains", strings.Contains(buf.String(), "fatal: whoops\r\n\n")) + assert.True(t, "log buf contains", strings.Contains(buf.String(), "error: rich error\r\n | second line\r\n\n")) }) } From c9ba330ef5876724c3f1f331ad7e124d806eaa83 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Thu, 12 Aug 2021 07:18:46 +0000 Subject: [PATCH 2/2] Use Fprintf --- pkg/clog/clog.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/clog/clog.go b/pkg/clog/clog.go index 0c88a29b..ebdce686 100644 --- a/pkg/clog/clog.go +++ b/pkg/clog/clog.go @@ -35,12 +35,11 @@ type CLIError struct { // String formats the CLI message for consumption by a human. func (m CLIMessage) String() string { var str strings.Builder - str.WriteString(fmt.Sprintf("%s: %s\r\n", + fmt.Fprintf(&str, "%s: %s\r\n", color.New(m.Color).Sprint(m.Level), - color.New(color.Bold).Sprint(m.Header)), - ) + color.New(color.Bold).Sprint(m.Header)) for _, line := range m.Lines { - str.WriteString(fmt.Sprintf(" %s %s\r\n", color.New(m.Color).Sprint("|"), line)) + fmt.Fprintf(&str, " %s %s\r\n", color.New(m.Color).Sprint("|"), line) } return str.String() }