diff options
Diffstat (limited to 'internal/output')
| -rw-r--r-- | internal/output/console.go | 23 | ||||
| -rw-r--r-- | internal/output/console_test.go | 17 | ||||
| -rw-r--r-- | internal/output/output.go | 2 |
3 files changed, 25 insertions, 17 deletions
diff --git a/internal/output/console.go b/internal/output/console.go index d2946ed..1a4efad 100644 --- a/internal/output/console.go +++ b/internal/output/console.go @@ -18,12 +18,19 @@ func NewConsoleOutputter() ConsoleOutputter { func (c ConsoleOutputter) Output(message string) error { _, err := fmt.Fprintln(c.stdout, message) - return err + if err != nil { + return fmt.Errorf("unable to print message %s: %w", message, err) + } + return nil } -func (c ConsoleOutputter) Error(message string) error { - _, err := fmt.Fprintln(c.stderr, message) - return err +func (c ConsoleOutputter) Error(err error) error { + msg := err.Error() + _, printErr := fmt.Fprintln(c.stderr, msg) + if printErr != nil { + return fmt.Errorf("unable to print message %s: %w", msg, printErr) + } + return nil } func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { @@ -32,13 +39,13 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { for _, header := range headers { _, err := fmt.Fprintf(w, "%s\t", header) if err != nil { - return err + return fmt.Errorf("unable to print table header %s: %w", header, err) } } if len(headers) > 0 { _, err := fmt.Fprint(w, "\n") if err != nil { - return err + return fmt.Errorf("unable to print newline after table header: %w", err) } } @@ -46,12 +53,12 @@ func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { for _, cell := range row { _, err := fmt.Fprintf(w, "%s\t", cell) if err != nil { - return err + return fmt.Errorf("unable to print table cell %s: %w", cell, err) } } _, err := fmt.Fprint(w, "\n") if err != nil { - return err + return fmt.Errorf("unable to print newline for table row %s: %w", row, err) } } diff --git a/internal/output/console_test.go b/internal/output/console_test.go index be369ab..492d0a2 100644 --- a/internal/output/console_test.go +++ b/internal/output/console_test.go @@ -2,10 +2,11 @@ package output import ( "bytes" + "errors" "io" "testing" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/check" ) func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { @@ -19,8 +20,8 @@ func TestConsoleOutputter_Output(t *testing.T) { consoleOutputter.Output("test string") - assert.Equal(t, stdout.String(), "test string\n") - assert.Equal(t, stderr.String(), "") + check.Equal(t, stdout.String(), "test string\n") + check.Equal(t, stderr.String(), "") } func TestConsoleOutputter_Error(t *testing.T) { @@ -28,10 +29,10 @@ func TestConsoleOutputter_Error(t *testing.T) { var stderr bytes.Buffer consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) - consoleOutputter.Error("test string") + consoleOutputter.Error(errors.New("test string")) - assert.Equal(t, stdout.String(), "") - assert.Equal(t, stderr.String(), "test string\n") + check.Equal(t, stdout.String(), "") + check.Equal(t, stderr.String(), "test string\n") } func TestConsoleOutputter_Table(t *testing.T) { @@ -70,7 +71,7 @@ func TestConsoleOutputter_Table(t *testing.T) { consoleOutputter.Table(tc.headers, tc.rows) - assert.Equal(t, stdout.String(), tc.expectedStdout) - assert.Equal(t, stderr.String(), "") + check.Equal(t, stdout.String(), tc.expectedStdout) + check.Equal(t, stderr.String(), "") } } diff --git a/internal/output/output.go b/internal/output/output.go index c51553b..9a9fb77 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -2,6 +2,6 @@ package output type Outputter interface { Output(message string) error - Error(message string) error + Error(err error) error Table(header []string, rows [][]string) error } |