Skip to content

Commit

Permalink
chore: increase console test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
TerryHowe committed Aug 19, 2024
1 parent 8fa9a77 commit 4af9db7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/status/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ limitations under the License.
package console

import (
"os"

containerd "github.com/containerd/console"
"github.com/morikuni/aec"
"os"
)

const (
Expand Down
89 changes: 65 additions & 24 deletions cmd/oras/internal/display/status/console/console_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build freebsd || linux || netbsd || openbsd || solaris

/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,20 +18,36 @@ limitations under the License.
package console

import (
containerd "github.com/containerd/console"
"os"
"testing"

containerd "github.com/containerd/console"
"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) {
Expand All @@ -43,30 +61,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
Expand All @@ -87,20 +99,49 @@ 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, "^[8\r\n^[7")
if err != nil {
t.Fatalf("NewRow output error: %v", err)
}
}

func TestConsole_OutputTo(t *testing.T) {
c, pty, tty := givenTestConsole(t)

c.OutputTo(1, "test string")

err := testutils.MatchPty(pty, tty, "^[8^[[1Ftest string^[[0m\r\n^[[0K")
if err != nil {
t.Fatalf("OutputTo output error: %v", err)
}
}

func TestConsole_Restore(t *testing.T) {
c, pty, tty := givenTestConsole(t)

c.Restore()

err := testutils.MatchPty(pty, tty, "^[8^[[0G^[[2K^[[?25h")
if err != nil {
t.Fatalf("Restore output error: %v", err)
}
}

func TestConsole_Save(t *testing.T) {
c, pty, tty := givenTestConsole(t)

c.Save()

err := testutils.MatchPty(pty, tty, "^[[?25l^[7^[[0m")
if err != nil {
t.Fatalf("Save output error: %v", err)
}
}

0 comments on commit 4af9db7

Please sign in to comment.