Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Increase print test coverage #1411

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions cmd/oras/internal/output/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,52 @@ func (mw *mockWriter) String() string {
return mw.written
}

func TestPrint_Error(t *testing.T) {
func TestPrinter_Println(t *testing.T) {
mockWriter := &mockWriter{}
printer := NewPrinter(mockWriter, false)
printer.Println("boom")
err := printer.Println("boom")
if mockWriter.errorCount != 1 {
t.Error("Expected one errors actual <" + strconv.Itoa(mockWriter.errorCount) + ">")
}
if err != nil {
t.Error("Expected error to be ignored")
}
}

func TestPrint_NoError(t *testing.T) {
mockWriter := &mockWriter{}
printer := NewPrinter(mockWriter, false)
func TestPrinter_PrintVerbose_noError(t *testing.T) {
builder := &strings.Builder{}
printer := NewPrinter(builder, false)

expected := "blah blah"
printer.Println(expected)
actual := strings.TrimSpace(mockWriter.String())
expected := "normal\n"
err := printer.Println("normal")
if err != nil {
t.Error("Expected no error got <" + err.Error() + ">")
}
err = printer.PrintVerbose("verbose")
if err != nil {
t.Error("Expected no error got <" + err.Error() + ">")
}
actual := builder.String()
if expected != actual {
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">")
}
if mockWriter.errorCount != 0 {
t.Error("Expected no errors actual <" + strconv.Itoa(mockWriter.errorCount) + ">")
}

func TestPrinter_PrintVerbose(t *testing.T) {
builder := &strings.Builder{}
printer := NewPrinter(builder, true)

expected := "normal\nverbose\n"
err := printer.Println("normal")
if err != nil {
t.Error("Expected no error got <" + err.Error() + ">")
}
err = printer.PrintVerbose("verbose")
if err != nil {
t.Error("Expected no error got <" + err.Error() + ">")
}
actual := builder.String()
if expected != actual {
t.Error("Expected <" + expected + "> not equal to actual <" + actual + ">")
}
}
Loading