Skip to content

Commit

Permalink
Merge pull request #2933 from buildkite/fix_logger_fmt_functions
Browse files Browse the repository at this point in the history
fix for layout issues with log messages
  • Loading branch information
wolfeidau authored Aug 15, 2024
2 parents 952eaef + 8cd6fda commit 5d69f8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/job/shell/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ func (wl *WriterLogger) Commentf(format string, v ...any) {

func (wl *WriterLogger) Errorf(format string, v ...any) {
if wl.Ansi {
wl.Printf(ansiColor("🚨 Error: "+format+"\n^^^ +++", "31"), v...)
wl.Printf(ansiColor("🚨 Error: "+format, "31")+"\n^^^ +++", v...)
} else {
wl.Printf("🚨 Error: "+format+"\n^^^ +++", v...)
}
}

func (wl *WriterLogger) Warningf(format string, v ...any) {
if wl.Ansi {
wl.Printf(ansiColor("⚠️ Warning: "+format+"\n^^^ +++", "33"), v...)
wl.Printf(ansiColor("⚠️ Warning: "+format, "33")+"\n^^^ +++", v...)
} else {
wl.Printf("⚠️ Warning: "+format+"\n^^^ +++", v...)
}
Expand Down
32 changes: 32 additions & 0 deletions internal/job/shell/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ func TestAnsiLogger(t *testing.T) {
}
}

func TestAnsiWithColorsLogger(t *testing.T) {
got := &bytes.Buffer{}
l := shell.NewWriterLogger(got, true, nil)

l.Headerf("Testing header: %q", "llamas")
l.Printf("Testing print: %q", "llamas")
l.Commentf("Testing comment: %q", "llamas")
l.Errorf("Testing error: %q", "llamas")
l.Warningf("Testing warning: %q", "llamas")
l.Promptf("Testing prompt: %q", "llamas")

want := &bytes.Buffer{}

fmt.Fprintln(want, `~~~ Testing header: "llamas"`)
fmt.Fprintln(want, `Testing print: "llamas"`)
fmt.Fprintln(want, `# Testing comment: "llamas"`)
fmt.Fprintln(want, `🚨 Error: Testing error: "llamas"`)
fmt.Fprintln(want, "^^^ +++")
fmt.Fprintln(want, `⚠️ Warning: Testing warning: "llamas"`)
fmt.Fprintln(want, "^^^ +++")

if runtime.GOOS == "windows" {
fmt.Fprintln(want, `> Testing prompt: "llamas"`)
} else {
fmt.Fprintln(want, `$ Testing prompt: "llamas"`)
}

if diff := cmp.Diff(got.String(), want.String()); diff != "" {
t.Fatalf("shell.WriterLogger output buffer diff (-got +want):\n%s", diff)
}
}

func TestLoggerStreamer(t *testing.T) {
got := &bytes.Buffer{}
l := shell.NewWriterLogger(got, false, nil)
Expand Down

0 comments on commit 5d69f8b

Please sign in to comment.