Skip to content

Commit

Permalink
fix: broken tty tests
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
  • Loading branch information
TerryHowe committed Dec 4, 2024
1 parent 31eaf93 commit 4532d32
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
14 changes: 10 additions & 4 deletions cmd/oras/internal/display/status/tty_console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ func TestTTYCopyHandler_OnCopySkipped(t *testing.T) {
t.Errorf("OnCopySkipped() should not return an error: %v", err)
}

ch.StopTracking()
if err = testutils.MatchPty(pty, slave, "Exists", mockFetcher.OciImage.MediaType, strconv.FormatInt(mockFetcher.OciImage.Size, 10), "100%"); err != nil {
if err = ch.StopTracking(); err != nil {
t.Errorf("StopTracking() should not return an error: %v", err)
}
if err = testutils.MatchPty(pty, slave, "Exists", "oci-image", strconv.FormatInt(mockFetcher.OciImage.Size, 10), "100.00%"); err != nil {
t.Fatal(err)
}
}
Expand All @@ -144,12 +146,14 @@ func TestTTYCopyHandler_PostCopy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer ch.StopTracking()

if ch.PostCopy(ctx, bogus) == nil {
t.Error("PostCopy() should return an error")
}

if err = ch.StopTracking(); err != nil {
t.Errorf("StopTracking() should not return an error: %v", err)
}
if err = testutils.MatchPty(pty, slave, "\x1b[?25l\x1b7\x1b[0m"); err != nil {
t.Fatal(err)
}
Expand All @@ -166,12 +170,14 @@ func TestTTYCopyHandler_PreCopy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer ch.StopTracking()

if err = ch.PreCopy(ctx, mockFetcher.OciImage); err != nil {
t.Errorf("PreCopy() should not return an error: %v", err)
}

if err = ch.StopTracking(); err != nil {
t.Errorf("StopTracking() should not return an error: %v", err)
}
if err = testutils.MatchPty(pty, slave, "\x1b[?25l\x1b7\x1b[0m"); err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ func doCopy(ctx context.Context, copyHandler status.CopyHandler, src oras.ReadOn
if err != nil {
return desc, err

Check warning on line 175 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L175

Added line #L175 was not covered by tests
}
defer copyHandler.StopTracking()
defer func() {
stopErr := copyHandler.StopTracking()
if err == nil {
err = stopErr
}
}()
extendedCopyOptions.OnCopySkipped = copyHandler.OnCopySkipped
extendedCopyOptions.PreCopy = copyHandler.PreCopy
extendedCopyOptions.PostCopy = copyHandler.PostCopy
Expand Down
15 changes: 3 additions & 12 deletions cmd/oras/root/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import (
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/testutils"
)

Expand Down Expand Up @@ -131,9 +129,7 @@ func Test_doCopy(t *testing.T) {
opts.TTY = slave
opts.From.Reference = memDesc.Digest.String()
dst := memory.New()
builder := &strings.Builder{}
printer := output.NewPrinter(builder, os.Stderr)
handler := status.NewTextCopyHandler(printer, dst)
handler := status.NewTTYCopyHandler(opts.TTY)
// test
_, err = doCopy(context.Background(), handler, memStore, dst, &opts)
if err != nil {
Expand All @@ -155,9 +151,7 @@ func Test_doCopy_skipped(t *testing.T) {
var opts copyOptions
opts.TTY = slave
opts.From.Reference = memDesc.Digest.String()
builder := &strings.Builder{}
printer := output.NewPrinter(builder, os.Stderr)
handler := status.NewTextCopyHandler(printer, memStore)
handler := status.NewTTYCopyHandler(opts.TTY)

// test
_, err = doCopy(context.Background(), handler, memStore, memStore, &opts)
Expand Down Expand Up @@ -191,10 +185,7 @@ func Test_doCopy_mounted(t *testing.T) {
t.Fatal(err)
}
to.PlainHTTP = true
builder := &strings.Builder{}
printer := output.NewPrinter(builder, os.Stderr)
printer.Verbose = true
handler := status.NewTextCopyHandler(printer, to)
handler := status.NewTTYCopyHandler(opts.TTY)

// test
_, err = doCopy(context.Background(), handler, from, to, &opts)
Expand Down

0 comments on commit 4532d32

Please sign in to comment.