diff --git a/cmd/oras/internal/display/status/console/console_test.go b/cmd/oras/internal/display/status/console/console_test.go index ad30cfae4..0e98cabe8 100644 --- a/cmd/oras/internal/display/status/console/console_test.go +++ b/cmd/oras/internal/display/status/console/console_test.go @@ -17,19 +17,35 @@ package console import ( containerd "github.com/containerd/console" + "os" "testing" "oras.land/oras/internal/testutils" ) -func givenConsole(t *testing.T) (Console, containerd.Console) { +func givenConsole(t *testing.T) (c Console, pty containerd.Console) { pty, _, err := containerd.NewPty() if err != nil { t.Fatal(err) } - return &console{ + + c = &console{ + Console: pty, + } + return c, pty +} + +func givenTestConsole(t *testing.T) (c Console, pty containerd.Console, tty *os.File) { + var err error + pty, tty, err = testutils.NewPty() + if err != nil { + t.Fatal(err) + } + + c = &console{ Console: pty, - }, pty + } + return c, pty, tty } func validateSize(t *testing.T, gotWidth, gotHeight, wantWidth, wantHeight int) { @@ -43,30 +59,24 @@ func validateSize(t *testing.T, gotWidth, gotHeight, wantWidth, wantHeight int) } func TestNewConsole(t *testing.T) { - _, device, err := testutils.NewPty() - if err != nil { - t.Fatal(err) + _, err := NewConsole(os.Stdin) + if err == nil { + t.Error("expected error creating bogus console") } - defer device.Close() +} - c, err := NewConsole(device) - if err != nil { - t.Fatal(err) - } +func TestConsole_Size(t *testing.T) { + c, _ := givenConsole(t) - s, err := c.Size() + size, err := c.Size() if err != nil { - t.Errorf("Unexpect error %v", err) - } - if s.Height != 10 { - t.Errorf("Expected height 10 got %d", s.Height) - } - if s.Width != 80 { - t.Errorf("Expected height 80 got %d", s.Width) + t.Fatalf("unexpected error getting size: %v", err) } + validateSize(t, int(size.Width), int(size.Height), MinWidth, MinHeight) + } -func TestConsole_Size(t *testing.T) { +func TestConsole_GetHeightWidth(t *testing.T) { c, pty := givenConsole(t) // minimal width and height @@ -87,14 +97,18 @@ func TestConsole_Size(t *testing.T) { _ = pty.Resize(containerd.WinSize{Width: 200, Height: 100}) gotHeight, gotWidth = c.GetHeightWidth() validateSize(t, gotWidth, gotHeight, 200, 100) -} - -func TestConsole_GetHeightWidth(t *testing.T) { } func TestConsole_NewRow(t *testing.T) { + c, pty, tty := givenTestConsole(t) + c.NewRow() + + err := testutils.MatchPty(pty, tty, "") + if err != nil { + t.Fatalf("NewRow output error: %v", err) + } } func TestConsole_OutputTo(t *testing.T) {