From b3a4199303967a064dd8e7e2bdaa3d3d75ad7448 Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Tue, 7 May 2024 04:52:28 -0600 Subject: [PATCH] Add print test coverage Signed-off-by: Terry Howe --- cmd/oras/internal/display/status/print.go | 8 +-- .../internal/display/status/print_test.go | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 cmd/oras/internal/display/status/print_test.go diff --git a/cmd/oras/internal/display/status/print.go b/cmd/oras/internal/display/status/print.go index 09a74f3d9..b79e0726f 100644 --- a/cmd/oras/internal/display/status/print.go +++ b/cmd/oras/internal/display/status/print.go @@ -47,11 +47,9 @@ func (p *Printer) Println(a ...any) { p.lock.Lock() defer p.lock.Unlock() _, err := fmt.Fprintln(p.out, a...) - if err != nil { - if p.errored == nil { - p.errored = fmt.Errorf("display output error: %w", err) - _, _ = fmt.Fprint(os.Stderr, p.errored) - } + if err != nil && p.errored == nil { + p.errored = fmt.Errorf("display output error: %w", err) + _, _ = fmt.Fprint(os.Stderr, p.errored) } } diff --git a/cmd/oras/internal/display/status/print_test.go b/cmd/oras/internal/display/status/print_test.go new file mode 100644 index 000000000..93e8393a8 --- /dev/null +++ b/cmd/oras/internal/display/status/print_test.go @@ -0,0 +1,63 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package status + +import ( + "fmt" + "io" + "strings" + "testing" +) + +type mockWriter struct{} + +func (mw *mockWriter) Write(p []byte) (n int, err error) { + return 0, fmt.Errorf("Boom: " + string(p)) +} + +func TestPrint_Error(t *testing.T) { + foo := &mockWriter{} + printer := NewPrinter(foo) + printer.Println("one") + if printer.errored == nil { + t.Error("Print should error") + } + if printer.errored.Error() == "Boom: one" { + t.Error("Unexpected error text: " + printer.errored.Error()) + } + + printer.Println("two") + if printer.errored == nil { + t.Error("Print should still error") + } + if printer.errored.Error() == "Boom: one" { + t.Error("Unexpected error text second time: " + printer.errored.Error()) + } +} + +func TestPrint_NoError(t *testing.T) { + var ser strings.Builder + printer := NewPrinter(io.Writer(&ser)) + expected := "blah blah" + printer.Println(expected) + if printer.errored != nil { + t.Error("Print should not error") + } + actual := strings.TrimSpace(ser.String()) + if expected != actual { + t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">") + } +}