From 79e5950b2f21b5fae8a305039df6e404c3980772 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 27 Aug 2019 14:58:04 +0200 Subject: [PATCH] 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 (cherry picked from commit 556d26c07d068d92fd896428ab4ac890554239d7) Signed-off-by: Sebastiaan van Stijn --- pkg/term/proxy_test.go | 224 ++++++++++++++++++++++++----------------- 1 file changed, 130 insertions(+), 94 deletions(-) diff --git a/pkg/term/proxy_test.go b/pkg/term/proxy_test.go index df588fe15b..8f8e4cb6d4 100644 --- a/pkg/term/proxy_test.go +++ b/pkg/term/proxy_test.go @@ -2,7 +2,6 @@ package term // import "github.com/docker/docker/pkg/term" import ( "bytes" - "fmt" "testing" "gotest.tools/assert" @@ -10,106 +9,143 @@ import ( ) func TestEscapeProxyRead(t *testing.T) { - escapeKeys, _ := ToBytes("") - keys, _ := ToBytes("a") - reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - buf := make([]byte, len(keys)) - 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) + t.Run("no escape keys, keys a", func(t *testing.T) { + escapeKeys, _ := ToBytes("") + keys, _ := ToBytes("a") + reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - keys, _ = ToBytes("a,b,c") - reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - buf = make([]byte, len(keys)) - 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) + 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) + }) - keys, _ = ToBytes("") - reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - buf = make([]byte, len(keys)) - nr, err = reader.Read(buf) - 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)) + t.Run("no escape keys, keys a,b,c", func(t *testing.T) { + escapeKeys, _ := ToBytes("") + keys, _ := ToBytes("a,b,c") + reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - escapeKeys, _ = ToBytes("DEL") - keys, _ = ToBytes("a,b,c,+") - reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - buf = make([]byte, len(keys)) - 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) + 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) + }) - keys, _ = ToBytes("") - reader = NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - buf = make([]byte, len(keys)) - nr, err = reader.Read(buf) - 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)) + t.Run("no escape keys, no keys", func(t *testing.T) { + escapeKeys, _ := ToBytes("") + keys, _ := ToBytes("") + reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - 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, fmt.Sprintf("nr %d should be equal to the number of 1", nr)) - assert.DeepEqual(t, keys, buf) + buf := make([]byte, len(keys)) + nr, err := reader.Read(buf) + assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read") + assert.Equal(t, nr, 0) + assert.Check(t, is.Len(keys, 0)) + assert.Check(t, is.Len(buf, 0)) + }) - 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, "nr should be equal to 0") - assert.DeepEqual(t, keys, buf) + t.Run("DEL escape key, keys a,b,c,+", func(t *testing.T) { + escapeKeys, _ := ToBytes("DEL") + keys, _ := ToBytes("a,b,c,+") + reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) - 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, "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) + 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) + t.Run("DEL escape key, no keys", func(t *testing.T) { + escapeKeys, _ := ToBytes("DEL") + keys, _ := ToBytes("") + reader := NewEscapeProxy(bytes.NewReader(keys), escapeKeys) + + buf := make([]byte, len(keys)) + nr, err := reader.Read(buf) + assert.Assert(t, is.ErrorContains(err, ""), "Should throw error when no keys are to read") + assert.Equal(t, nr, 0) + assert.Check(t, is.Len(keys, 0)) + assert.Check(t, is.Len(buf, 0)) + }) + + 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) }