Commit a661a196 authored by Jan Winkelmann's avatar Jan Winkelmann

add/fix tests and formatting

parent 89b520b8
package cli
import (
"bytes"
//"io"
"testing"
"github.com/ipfs/go-ipfs-cmds"
"gx/ipfs/QmWdiBLZ22juGtuNceNbvvHV11zKzCaoQFMP76x2w1XDFZ/go-ipfs-cmdkit"
)
type writeCloser struct {
*bytes.Buffer
}
func (wc writeCloser) Close() error { return nil }
type tcSetError struct {
stdout, stderr *bytes.Buffer
exStdout, exStderr string
exExit int
f func(re ResponseEmitter, t *testing.T)
}
func (tc tcSetError) Run(t *testing.T) {
req, err := cmds.NewEmptyRequest()
if err != nil {
t.Fatal(err)
}
cmdsre, exitCh := NewResponseEmitter(tc.stdout, tc.stderr, nil, req)
re := cmdsre.(ResponseEmitter)
go tc.f(re, t)
if exitCode := <-exitCh; exitCode != tc.exExit {
t.Fatalf("expected exit code %d, got %d", tc.exExit, exitCode)
}
if tc.stdout.String() != tc.exStdout {
t.Fatalf(`expected stdout string "%s" but got "%s"`, tc.exStdout, tc.stdout.String())
}
if tc.stderr.String() != tc.exStderr {
t.Fatalf(`expected stderr string "%s" but got "%s"`, tc.exStderr, tc.stderr.String())
}
t.Logf("stdout:\n---\n%s---\n", tc.stdout.Bytes())
t.Logf("stderr:\n---\n%s---\n", tc.stderr.Bytes())
}
func TestSetError(t *testing.T) {
tcs := []tcSetError{
tcSetError{
stdout: bytes.NewBuffer(nil),
stderr: bytes.NewBuffer(nil),
exStdout: "a\n",
exStderr: "Error: some error\n",
exExit: 1,
f: func(re ResponseEmitter, t *testing.T) {
re.Emit("a")
re.SetError("some error", cmdsutil.ErrFatal)
re.Emit("b")
},
},
tcSetError{
stdout: bytes.NewBuffer(nil),
stderr: bytes.NewBuffer(nil),
exStdout: "a\nb\n",
exStderr: "Error: some error\n",
exExit: 1,
f: func(re ResponseEmitter, t *testing.T) {
defer re.Close()
re.Emit("a")
re.SetError("some error", cmdsutil.ErrNormal)
re.Emit("b")
},
},
tcSetError{
stdout: bytes.NewBuffer(nil),
stderr: bytes.NewBuffer(nil),
exStdout: "a\nb\n",
exStderr: "Error: some error\n",
exExit: 3,
f: func(re ResponseEmitter, t *testing.T) {
re.Emit("a")
re.SetError("some error", cmdsutil.ErrNormal)
re.Emit("b")
re.Exit(3)
},
},
}
for i, tc := range tcs {
t.Log(i)
tc.Run(t)
}
}
......@@ -71,7 +71,6 @@ var ErrIncorrectType = errors.New("The command returned a value with a different
// Call invokes the command for the given Request
func (c *Command) Call(req Request, re ResponseEmitter) (err error) {
// we need the named return parameter so we can change the value from defer()
defer re.Close()
cmd, err := c.Get(req.Path())
......
......@@ -41,7 +41,6 @@ func NewClient(address string) Client {
}
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
if req.Context() == nil {
log.Warningf("no context set in request")
if err := req.SetRootContext(context.TODO()); err != nil {
......
......@@ -141,11 +141,11 @@ func TestPipePair(t *testing.T) {
if err != nil {
t.Fatal(err)
}
str, ok := v.(string)
str, ok := v.(*string)
if !ok {
t.Fatalf("expected type %T but got %T", expect, v)
}
if str != expect {
if *str != expect {
t.Fatalf("expected value %#v but got %#v", expect, v)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment