[lxc-devel] [lxd/master] Don't depend on testify in the cmd package

freeekanayaka on Github lxc-bot at linuxcontainers.org
Wed May 24 14:34:19 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 417 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170524/a2a0b0d6/attachment.bin>
-------------- next part --------------
From c1c03b34c5b322cb4cb6239262c86b343dba33a6 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Wed, 24 May 2017 16:33:03 +0200
Subject: [PATCH] Don't depend on testify in the cmd package

Move out the assertions to the test package instead.

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 shared/cmd/context_test.go | 32 +++++++++++++++++++++-----------
 shared/cmd/testing.go      | 25 +++++++++++--------------
 2 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/shared/cmd/context_test.go b/shared/cmd/context_test.go
index 443541e46..1e4b0c6bc 100644
--- a/shared/cmd/context_test.go
+++ b/shared/cmd/context_test.go
@@ -8,12 +8,22 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
+// AssertOutEqual checks that the given text matches the the out stream.
+func AssertOutEqual(t *testing.T, stream *cmd.MemoryStreams, expected string) {
+	assert.Equal(t, expected, stream.Out(), "Unexpected output stream")
+}
+
+// AssertErrEqual checks that the given text matches the the err stream.
+func AssertErrEqual(t *testing.T, stream *cmd.MemoryStreams, expected string) {
+	assert.Equal(t, expected, stream.Err(), "Unexpected error stream")
+}
+
 // Output prints the given message on standard output
 func TestOutput(t *testing.T) {
 	streams := cmd.NewMemoryStreams("")
 	context := cmd.NewMemoryContext(streams)
 	context.Output("Hello %s", "world")
-	streams.AssertOutEqual(t, "Hello world")
+	AssertOutEqual(t, streams, "Hello world")
 }
 
 // AskBool returns a boolean result depending on the user input.
@@ -39,8 +49,8 @@ func TestAskBool(t *testing.T) {
 		result := context.AskBool(c.question, c.defaultAnswer)
 
 		assert.Equal(t, c.result, result, "Unexpected answer result")
-		streams.AssertOutEqual(t, c.output)
-		streams.AssertErrEqual(t, c.error)
+		AssertOutEqual(t, streams, c.output)
+		AssertErrEqual(t, streams, c.error)
 	}
 }
 
@@ -65,8 +75,8 @@ func TestAskChoice(t *testing.T) {
 		result := context.AskChoice(c.question, c.choices, c.defaultAnswer)
 
 		assert.Equal(t, c.result, result, "Unexpected answer result")
-		streams.AssertOutEqual(t, c.output)
-		streams.AssertErrEqual(t, c.error)
+		AssertOutEqual(t, streams, c.output)
+		AssertErrEqual(t, streams, c.error)
 	}
 }
 
@@ -95,8 +105,8 @@ func TestAskInt(t *testing.T) {
 		result := context.AskInt(c.question, c.min, c.max, c.defaultAnswer)
 
 		assert.Equal(t, c.result, result, "Unexpected answer result")
-		streams.AssertOutEqual(t, c.output)
-		streams.AssertErrEqual(t, c.error)
+		AssertOutEqual(t, streams, c.output)
+		AssertErrEqual(t, streams, c.error)
 	}
 }
 
@@ -126,8 +136,8 @@ func TestAskString(t *testing.T) {
 		result := context.AskString(c.question, c.defaultAnswer, c.validate)
 
 		assert.Equal(t, c.result, result, "Unexpected answer result")
-		streams.AssertOutEqual(t, c.output)
-		streams.AssertErrEqual(t, c.error)
+		AssertOutEqual(t, streams, c.output)
+		AssertErrEqual(t, streams, c.error)
 	}
 }
 
@@ -150,8 +160,8 @@ func TestAskPassword(t *testing.T) {
 		result := context.AskPassword(c.question, c.reader)
 
 		assert.Equal(t, c.result, result, "Unexpected answer result")
-		streams.AssertOutEqual(t, c.output)
-		streams.AssertErrEqual(t, c.error)
+		AssertOutEqual(t, streams, c.output)
+		AssertErrEqual(t, streams, c.error)
 	}
 }
 
diff --git a/shared/cmd/testing.go b/shared/cmd/testing.go
index a51813e4a..bb5eca24f 100644
--- a/shared/cmd/testing.go
+++ b/shared/cmd/testing.go
@@ -1,4 +1,4 @@
-// Utilities for testing cmd-related code.
+// In-memory streams, useful for testing cmd-related code.
 
 package cmd
 
@@ -6,9 +6,6 @@ import (
 	"bytes"
 	"io/ioutil"
 	"strings"
-	"testing"
-
-	"github.com/stretchr/testify/assert"
 )
 
 // MemoryStreams provide an in-memory version of the system
@@ -35,6 +32,16 @@ func (s *MemoryStreams) InputRead() string {
 	return string(bytes)
 }
 
+// Out returns the current content of the out stream.
+func (s *MemoryStreams) Out() string {
+	return s.out.String()
+}
+
+// Err returns the current content of the err stream.
+func (s *MemoryStreams) Err() string {
+	return s.err.String()
+}
+
 // InputReset replaces the data in the input stream.
 func (s *MemoryStreams) InputReset(input string) {
 	// XXX This is what the stdlib strings.Reader.Reset() does, however
@@ -63,16 +70,6 @@ func (s *MemoryStreams) InputAppendBoolAnswer(answer bool) {
 	s.InputAppendLine(line)
 }
 
-// AssertOutEqual checks that the given text matches the the out stream.
-func (s *MemoryStreams) AssertOutEqual(t *testing.T, expected string) {
-	assert.Equal(t, expected, s.out.String(), "Unexpected output stream")
-}
-
-// AssertErrEqual checks that the given text matches the the err stream.
-func (s *MemoryStreams) AssertErrEqual(t *testing.T, expected string) {
-	assert.Equal(t, expected, s.err.String(), "Unexpected error stream")
-}
-
 // NewMemoryContext creates a new command Context using the given in-memory
 // streams.
 func NewMemoryContext(streams *MemoryStreams) *Context {


More information about the lxc-devel mailing list