Use testify to simplify tests

This commit is contained in:
Julian Kornberger 2017-02-04 12:42:50 +01:00
parent a5c2cfe9ac
commit 3d4ee48a3f

View File

@ -6,6 +6,8 @@ import (
"net/textproto" "net/textproto"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -25,11 +27,10 @@ func testConn(t *testing.T, disableEPSV bool) {
if testing.Short() { if testing.Short() {
t.Skip("skipping test in short mode.") t.Skip("skipping test in short mode.")
} }
require := require.New(t)
c, err := DialTimeout("localhost:21", 5*time.Second) c, err := DialTimeout("localhost:21", 5*time.Second)
if err != nil { require.NoError(err)
t.Fatal(err)
}
if disableEPSV { if disableEPSV {
delete(c.features, "EPSV") delete(c.features, "EPSV")
@ -37,106 +38,61 @@ func testConn(t *testing.T, disableEPSV bool) {
} }
err = c.Login("anonymous", "anonymous") err = c.Login("anonymous", "anonymous")
if err != nil { require.NoError(err)
t.Fatal(err)
}
err = c.NoOp() err = c.NoOp()
if err != nil { require.NoError(err)
t.Error(err)
}
err = c.ChangeDir("incoming") err = c.ChangeDir("incoming")
if err != nil { require.NoError(err)
t.Error(err)
}
data := bytes.NewBufferString(testData) data := bytes.NewBufferString(testData)
err = c.Stor("test", data) err = c.Stor("test", data)
if err != nil { require.NoError(err)
t.Error(err)
}
_, err = c.List(".") _, err = c.List(".")
if err != nil { require.NoError(err)
t.Error(err)
}
err = c.Rename("test", "tset") err = c.Rename("test", "tset")
if err != nil { require.NoError(err)
t.Error(err)
}
r, err := c.Retr("tset") r, err := c.Retr("tset")
if err != nil { require.NoError(err)
t.Error(err) buf, err := ioutil.ReadAll(r)
} else { require.NoError(err)
buf, err := ioutil.ReadAll(r) require.Equal(testData, string(buf))
if err != nil { err = r.Close()
t.Error(err) require.NoError(err)
}
if string(buf) != testData {
t.Errorf("'%s'", buf)
}
r.Close()
}
r, err = c.RetrFrom("tset", 5) r, err = c.RetrFrom("tset", 5)
if err != nil { require.NoError(err)
t.Error(err) buf, err = ioutil.ReadAll(r)
} else { require.NoError(err)
buf, err := ioutil.ReadAll(r) require.Equal(testData[5:], string(buf))
if err != nil { r.Close()
t.Error(err)
}
expected := testData[5:]
if string(buf) != expected {
t.Errorf("read %q, expected %q", buf, expected)
}
r.Close()
}
err = c.Delete("tset") err = c.Delete("tset")
if err != nil { require.NoError(err)
t.Error(err)
}
err = c.MakeDir(testDir) err = c.MakeDir(testDir)
if err != nil { require.NoError(err)
t.Error(err)
}
err = c.ChangeDir(testDir) err = c.ChangeDir(testDir)
if err != nil { require.NoError(err)
t.Error(err)
}
dir, err := c.CurrentDir() dir, err := c.CurrentDir()
if err != nil { require.NoError(err)
t.Error(err) require.Equal("/incoming/"+testDir, dir)
} else {
if dir != "/incoming/"+testDir {
t.Error("Wrong dir: " + dir)
}
}
err = c.ChangeDirToParent() err = c.ChangeDirToParent()
if err != nil { require.NoError(err)
t.Error(err)
}
entries, err := c.NameList("/") entries, err := c.NameList("/")
if err != nil { require.NoError(err)
t.Error(err) require.EqualValues([]string{"/incoming"}, entries)
}
if len(entries) != 1 || entries[0] != "/incoming" {
t.Errorf("Unexpected entries: %v", entries)
}
err = c.RemoveDir(testDir) err = c.RemoveDir(testDir)
if err != nil { require.NoError(err)
t.Error(err)
}
err = c.Logout() err = c.Logout()
if err != nil { if err != nil {
@ -149,35 +105,31 @@ func testConn(t *testing.T, disableEPSV bool) {
} }
} }
c.Quit() err = c.Quit()
require.NoError(err)
err = c.NoOp() err = c.NoOp()
if err == nil { require.Error(err)
t.Error("Expected error") require.Regexp("write tcp .* use of closed network connection", err.Error())
}
} }
func TestConnIPv6(t *testing.T) { func TestConnIPv6(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping test in short mode.") t.Skip("skipping test in short mode.")
} }
require := require.New(t)
c, err := DialTimeout("[::1]:21", 5*time.Second) c, err := DialTimeout("[::1]:21", 5*time.Second)
if err != nil { require.NoError(err)
t.Fatal(err)
}
err = c.Login("anonymous", "anonymous") err = c.Login("anonymous", "anonymous")
if err != nil { require.NoError(err)
t.Fatal(err)
}
_, err = c.List(".") _, err = c.List(".")
if err != nil { require.NoError(err)
t.Error(err)
}
c.Quit() err = c.Quit()
require.NoError(err)
} }
// TestConnect tests the legacy Connect function // TestConnect tests the legacy Connect function
@ -187,38 +139,33 @@ func TestConnect(t *testing.T) {
} }
c, err := Connect("localhost:21") c, err := Connect("localhost:21")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
c.Quit() c.Quit()
} }
func TestTimeout(t *testing.T) { func TestDialTimeout(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping test in short mode.") t.Skip("skipping test in short mode.")
} }
require := require.New(t)
c, err := DialTimeout("localhost:2121", 1*time.Second) _, err := DialTimeout("localhost:2121", 1*time.Second)
if err == nil { require.Error(err)
t.Fatal("expected timeout, got nil error") require.Regexp("dial tcp .* connection refused", err.Error())
c.Quit()
}
} }
func TestWrongLogin(t *testing.T) { func TestWrongLogin(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping test in short mode.") t.Skip("skipping test in short mode.")
} }
require := require.New(t)
c, err := DialTimeout("localhost:21", 5*time.Second) c, err := DialTimeout("localhost:21", 5*time.Second)
if err != nil { require.NoError(err)
t.Fatal(err)
}
defer c.Quit() defer c.Quit()
err = c.Login("zoo2Shia", "fei5Yix9") err = c.Login("zoo2Shia", "fei5Yix9")
if err == nil { require.Error(err)
t.Fatal("expected error, got nil") require.Regexp("(Login incorrect|anonymous only)", err.Error())
}
} }