pkg/term: refactor TestEscapeProxyRead
- use subtests to make it clearer what the individual test-cases are, and to prevent tests from depending on values set by the previous test(s). - remove redundant messages in assert (gotest.tools already prints a useful message if assertions fail). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
5c0d487416
commit
556d26c07d
1 changed files with 130 additions and 94 deletions
|
@ -2,7 +2,6 @@ package term // import "github.com/docker/docker/pkg/term"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
|
@ -10,106 +9,143 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEscapeProxyRead(t *testing.T) {
|
func TestEscapeProxyRead(t *testing.T) {
|
||||||
escapeKeys, _ := ToBytes("")
|
t.Run("no escape keys, keys a", func(t *testing.T) {
|
||||||
keys, _ := ToBytes("a")
|
escapeKeys, _ := ToBytes("")
|
||||||
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
keys, _ := ToBytes("a")
|
||||||
buf := make([]byte, len(keys))
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
nr, err := reader.Read(buf)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
|
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
|
||||||
keys, _ = ToBytes("a,b,c")
|
buf := make([]byte, len(keys))
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
nr, err := reader.Read(buf)
|
||||||
buf = make([]byte, len(keys))
|
assert.NilError(t, err)
|
||||||
nr, err = reader.Read(buf)
|
assert.Equal(t, nr, len(keys))
|
||||||
assert.NilError(t, err)
|
assert.DeepEqual(t, keys, buf)
|
||||||
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
|
})
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
|
||||||
keys, _ = ToBytes("")
|
t.Run("no escape keys, keys a,b,c", func(t *testing.T) {
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
escapeKeys, _ := ToBytes("")
|
||||||
buf = make([]byte, len(keys))
|
keys, _ := ToBytes("a,b,c")
|
||||||
nr, err = reader.Read(buf)
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read")
|
|
||||||
assert.Equal(t, nr, 0, "nr should be zero")
|
|
||||||
assert.Check(t, is.Len(keys, 0))
|
|
||||||
assert.Check(t, is.Len(buf, 0))
|
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("DEL")
|
buf := make([]byte, len(keys))
|
||||||
keys, _ = ToBytes("a,b,c,+")
|
nr, err := reader.Read(buf)
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
assert.NilError(t, err)
|
||||||
buf = make([]byte, len(keys))
|
assert.Equal(t, nr, len(keys))
|
||||||
nr, err = reader.Read(buf)
|
assert.DeepEqual(t, keys, buf)
|
||||||
assert.NilError(t, err)
|
})
|
||||||
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr %d should be equal to the number of %d", nr, len(keys)))
|
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
|
||||||
keys, _ = ToBytes("")
|
t.Run("no escape keys, no keys", func(t *testing.T) {
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
escapeKeys, _ := ToBytes("")
|
||||||
buf = make([]byte, len(keys))
|
keys, _ := ToBytes("")
|
||||||
nr, err = reader.Read(buf)
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read")
|
|
||||||
assert.Equal(t, nr, 0, "nr should be zero")
|
|
||||||
assert.Check(t, is.Len(keys, 0))
|
|
||||||
assert.Check(t, is.Len(buf, 0))
|
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("ctrl-x,ctrl-@")
|
buf := make([]byte, len(keys))
|
||||||
keys, _ = ToBytes("DEL")
|
nr, err := reader.Read(buf)
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read")
|
||||||
buf = make([]byte, len(keys))
|
assert.Equal(t, nr, 0)
|
||||||
nr, err = reader.Read(buf)
|
assert.Check(t, is.Len(keys, 0))
|
||||||
assert.NilError(t, err)
|
assert.Check(t, is.Len(buf, 0))
|
||||||
assert.Equal(t, nr, 1, fmt.Sprintf("nr %d should be equal to the number of 1", nr))
|
})
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("ctrl-c")
|
t.Run("DEL escape key, keys a,b,c,+", func(t *testing.T) {
|
||||||
keys, _ = ToBytes("ctrl-c")
|
escapeKeys, _ := ToBytes("DEL")
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
keys, _ := ToBytes("a,b,c,+")
|
||||||
buf = make([]byte, len(keys))
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
nr, err = reader.Read(buf)
|
|
||||||
assert.Error(t, err, "read escape sequence")
|
|
||||||
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
buf := make([]byte, len(keys))
|
||||||
keys, _ = ToBytes("ctrl-c,ctrl-z")
|
nr, err := reader.Read(buf)
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
assert.NilError(t, err)
|
||||||
buf = make([]byte, 1)
|
assert.Equal(t, nr, len(keys))
|
||||||
nr, err = reader.Read(buf)
|
assert.DeepEqual(t, keys, buf)
|
||||||
assert.NilError(t, err)
|
})
|
||||||
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
|
||||||
assert.DeepEqual(t, keys[0:1], buf)
|
|
||||||
nr, err = reader.Read(buf)
|
|
||||||
assert.Error(t, err, "read escape sequence")
|
|
||||||
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
|
||||||
assert.DeepEqual(t, keys[1:], buf)
|
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
t.Run("DEL escape key, no keys", func(t *testing.T) {
|
||||||
keys, _ = ToBytes("ctrl-c,DEL,+")
|
escapeKeys, _ := ToBytes("DEL")
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
keys, _ := ToBytes("")
|
||||||
buf = make([]byte, 1)
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
nr, err = reader.Read(buf)
|
|
||||||
assert.NilError(t, err)
|
buf := make([]byte, len(keys))
|
||||||
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
nr, err := reader.Read(buf)
|
||||||
assert.DeepEqual(t, keys[0:1], buf)
|
assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read")
|
||||||
buf = make([]byte, len(keys))
|
assert.Equal(t, nr, 0)
|
||||||
nr, err = reader.Read(buf)
|
assert.Check(t, is.Len(keys, 0))
|
||||||
assert.NilError(t, err)
|
assert.Check(t, is.Len(buf, 0))
|
||||||
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr should be equal to %d", len(keys)))
|
})
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
|
t.Run("ctrl-x,ctrl-@ escape key, keys DEL", func(t *testing.T) {
|
||||||
|
escapeKeys, _ := ToBytes("ctrl-x,ctrl-@")
|
||||||
|
keys, _ := ToBytes("DEL")
|
||||||
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
|
|
||||||
|
buf := make([]byte, len(keys))
|
||||||
|
nr, err := reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, 1)
|
||||||
|
assert.DeepEqual(t, keys, buf)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ctrl-c escape key, keys ctrl-c", func(t *testing.T) {
|
||||||
|
escapeKeys, _ := ToBytes("ctrl-c")
|
||||||
|
keys, _ := ToBytes("ctrl-c")
|
||||||
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
|
|
||||||
|
buf := make([]byte, len(keys))
|
||||||
|
nr, err := reader.Read(buf)
|
||||||
|
assert.Error(t, err, "read escape sequence")
|
||||||
|
assert.Equal(t, nr, 0)
|
||||||
|
assert.DeepEqual(t, keys, buf)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ctrl-c,ctrl-z escape key, keys ctrl-c,ctrl-z", func(t *testing.T) {
|
||||||
|
escapeKeys, _ := ToBytes("ctrl-c,ctrl-z")
|
||||||
|
keys, _ := ToBytes("ctrl-c,ctrl-z")
|
||||||
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
|
|
||||||
|
buf := make([]byte, 1)
|
||||||
|
nr, err := reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, 0)
|
||||||
|
assert.DeepEqual(t, keys[0:1], buf)
|
||||||
|
|
||||||
|
nr, err = reader.Read(buf)
|
||||||
|
assert.Error(t, err, "read escape sequence")
|
||||||
|
assert.Equal(t, nr, 0)
|
||||||
|
assert.DeepEqual(t, keys[1:], buf)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ctrl-c,ctrl-z escape key, keys ctrl-c,DEL,+", func(t *testing.T) {
|
||||||
|
escapeKeys, _ := ToBytes("ctrl-c,ctrl-z")
|
||||||
|
keys, _ := ToBytes("ctrl-c,DEL,+")
|
||||||
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
|
|
||||||
|
buf := make([]byte, 1)
|
||||||
|
nr, err := reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, 0)
|
||||||
|
assert.DeepEqual(t, keys[0:1], buf)
|
||||||
|
|
||||||
|
buf = make([]byte, len(keys))
|
||||||
|
nr, err = reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, len(keys))
|
||||||
|
assert.DeepEqual(t, keys, buf)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ctrl-c,ctrl-z escape key, keys ctrl-c,DEL", func(t *testing.T) {
|
||||||
|
escapeKeys, _ := ToBytes("ctrl-c,ctrl-z")
|
||||||
|
keys, _ := ToBytes("ctrl-c,DEL")
|
||||||
|
reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
||||||
|
|
||||||
|
buf := make([]byte, 1)
|
||||||
|
nr, err := reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, 0)
|
||||||
|
assert.DeepEqual(t, keys[0:1], buf)
|
||||||
|
|
||||||
|
buf = make([]byte, len(keys))
|
||||||
|
nr, err = reader.Read(buf)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, nr, len(keys))
|
||||||
|
assert.DeepEqual(t, keys, buf)
|
||||||
|
})
|
||||||
|
|
||||||
escapeKeys, _ = ToBytes("ctrl-c,ctrl-z")
|
|
||||||
keys, _ = ToBytes("ctrl-c,DEL")
|
|
||||||
reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys)
|
|
||||||
buf = make([]byte, 1)
|
|
||||||
nr, err = reader.Read(buf)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.Equal(t, nr, 0, "nr should be equal to 0")
|
|
||||||
assert.DeepEqual(t, keys[0:1], buf)
|
|
||||||
buf = make([]byte, len(keys))
|
|
||||||
nr, err = reader.Read(buf)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.Equal(t, nr, len(keys), fmt.Sprintf("nr should be equal to %d", len(keys)))
|
|
||||||
assert.DeepEqual(t, keys, buf)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue