Use assert package to simplify tests

This commit is contained in:
Julien Laffaye 2022-08-17 19:24:40 -04:00
parent 560423fa8a
commit 45482d097e
No known key found for this signature in database
GPG Key ID: 890C3E5C169AE841
2 changed files with 47 additions and 120 deletions

View File

@ -5,8 +5,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"net/textproto"
"strings"
"syscall" "syscall"
"testing" "testing"
"time" "time"
@ -28,174 +26,111 @@ func TestConnEPSV(t *testing.T) {
} }
func testConn(t *testing.T, disableEPSV bool) { func testConn(t *testing.T, disableEPSV bool) {
assert := assert.New(t)
mock, c := openConn(t, "127.0.0.1", DialWithTimeout(5*time.Second), DialWithDisabledEPSV(disableEPSV)) mock, c := openConn(t, "127.0.0.1", DialWithTimeout(5*time.Second), DialWithDisabledEPSV(disableEPSV))
err := c.Login("anonymous", "anonymous") err := c.Login("anonymous", "anonymous")
if err != nil { assert.NoError(err)
t.Fatal(err)
}
err = c.NoOp() err = c.NoOp()
if err != nil { assert.NoError(err)
t.Error(err)
}
err = c.ChangeDir("incoming") err = c.ChangeDir("incoming")
if err != nil { assert.NoError(err)
t.Error(err)
}
dir, err := c.CurrentDir() dir, err := c.CurrentDir()
if err != nil { if assert.NoError(err) {
t.Error(err) assert.Equal("/incoming", dir)
} else {
if dir != "/incoming" {
t.Error("Wrong dir: " + dir)
}
} }
data := bytes.NewBufferString(testData) data := bytes.NewBufferString(testData)
err = c.Stor("test", data) err = c.Stor("test", data)
if err != nil { assert.NoError(err)
t.Error(err)
}
_, err = c.List(".") _, err = c.List(".")
if err != nil { assert.NoError(err)
t.Error(err)
}
err = c.Rename("test", "tset") err = c.Rename("test", "tset")
if err != nil { assert.NoError(err)
t.Error(err)
}
// Read without deadline // Read without deadline
r, err := c.Retr("tset") r, err := c.Retr("tset")
if err != nil { if assert.NoError(err) {
t.Error(err) buf, err := ioutil.ReadAll(r)
} else { if assert.NoError(err) {
buf, errRead := ioutil.ReadAll(r) assert.Equal(testData, string(buf))
if err != nil {
t.Error(errRead)
}
if string(buf) != testData {
t.Errorf("'%s'", buf)
} }
r.Close() r.Close()
r.Close() // test we can close two times r.Close() // test we can close two times
} }
// Read with deadline // Read with deadline
r, err = c.Retr("tset") r, err = c.Retr("tset")
if err != nil { if assert.NoError(err) {
t.Error(err)
} else {
if err := r.SetDeadline(time.Now()); err != nil { if err := r.SetDeadline(time.Now()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = ioutil.ReadAll(r) _, err = ioutil.ReadAll(r)
if err == nil { assert.ErrorContains(err, "i/o timeout")
t.Error("deadline should have caused error")
} else if !strings.HasSuffix(err.Error(), "i/o timeout") {
t.Error(err)
}
r.Close() r.Close()
} }
// Read with offset // Read with offset
r, err = c.RetrFrom("tset", 5) r, err = c.RetrFrom("tset", 5)
if err != nil { if assert.NoError(err) {
t.Error(err) buf, err := ioutil.ReadAll(r)
} else { if assert.NoError(err) {
buf, errRead := ioutil.ReadAll(r) expected := testData[5:]
if errRead != nil { assert.Equal(expected, string(buf))
t.Error(errRead)
}
expected := testData[5:]
if string(buf) != expected {
t.Errorf("read %q, expected %q", buf, expected)
} }
r.Close() r.Close()
} }
data2 := bytes.NewBufferString(testData) data2 := bytes.NewBufferString(testData)
err = c.Append("tset", data2) err = c.Append("tset", data2)
if err != nil { assert.NoError(err)
t.Error(err)
}
// Read without deadline, after append // Read without deadline, after append
r, err = c.Retr("tset") r, err = c.Retr("tset")
if err != nil { if assert.NoError(err) {
t.Error(err) buf, err := ioutil.ReadAll(r)
} else { if assert.NoError(err) {
buf, errRead := ioutil.ReadAll(r) assert.Equal(testData+testData, string(buf))
if err != nil {
t.Error(errRead)
}
if string(buf) != testData+testData {
t.Errorf("'%s'", buf)
} }
r.Close() r.Close()
} }
fileSize, err := c.FileSize("magic-file") fileSize, err := c.FileSize("magic-file")
if err != nil { assert.NoError(err)
t.Error(err) assert.Equal(int64(42), fileSize)
}
if fileSize != 42 {
t.Errorf("file size %q, expected %q", fileSize, 42)
}
_, err = c.FileSize("not-found") _, err = c.FileSize("not-found")
if err == nil { assert.Error(err)
t.Fatal("expected error, got nil")
}
err = c.Delete("tset") err = c.Delete("tset")
if err != nil { assert.NoError(err)
t.Error(err)
}
err = c.MakeDir(testDir) err = c.MakeDir(testDir)
if err != nil { assert.NoError(err)
t.Error(err)
}
err = c.ChangeDir(testDir) err = c.ChangeDir(testDir)
if err != nil { assert.NoError(err)
t.Error(err)
}
err = c.ChangeDirToParent() err = c.ChangeDirToParent()
if err != nil { assert.NoError(err)
t.Error(err)
}
entries, err := c.NameList("/") entries, err := c.NameList("/")
if err != nil { assert.NoError(err)
t.Error(err) assert.Equal([]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 { assert.NoError(err)
t.Error(err)
}
err = c.Logout() err = c.Logout()
if err != nil { assert.NoError(err)
if protoErr := err.(*textproto.Error); protoErr != nil {
if protoErr.Code != StatusNotImplemented {
t.Error(err)
}
} else {
t.Error(err)
}
}
if err = c.Quit(); err != nil { if err = c.Quit(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -205,9 +140,7 @@ func testConn(t *testing.T, disableEPSV bool) {
mock.Wait() mock.Wait()
err = c.NoOp() err = c.NoOp()
if err == nil { assert.Error(err, "should error on closed conn")
t.Error("Expected error")
}
} }
// TestConnect tests the legacy Connect function // TestConnect tests the legacy Connect function

View File

@ -6,12 +6,14 @@ import (
"io" "io"
"net" "net"
"net/textproto" "net/textproto"
"reflect"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type ftpMock struct { type ftpMock struct {
@ -386,20 +388,14 @@ func openConn(t *testing.T, addr string, options ...DialOption) (*ftpMock, *Serv
func openConnExt(t *testing.T, addr, modtime string, options ...DialOption) (*ftpMock, *ServerConn) { func openConnExt(t *testing.T, addr, modtime string, options ...DialOption) (*ftpMock, *ServerConn) {
mock, err := newFtpMockExt(t, addr, modtime) mock, err := newFtpMockExt(t, addr, modtime)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer mock.Close() defer mock.Close()
c, err := Dial(mock.Addr(), options...) c, err := Dial(mock.Addr(), options...)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
err = c.Login("anonymous", "anonymous") err = c.Login("anonymous", "anonymous")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
return mock, c return mock, c
} }
@ -417,9 +413,7 @@ func closeConn(t *testing.T, mock *ftpMock, c *ServerConn, commands []string) {
// Wait for the connection to close // Wait for the connection to close
mock.Wait() mock.Wait()
if !reflect.DeepEqual(mock.commands, expected) { assert.Equal(t, expected, mock.commands, "unexpected sequence of commands")
t.Fatal("unexpected sequence of commands:", mock.commands, "expected:", expected)
}
} }
func TestConn4(t *testing.T) { func TestConn4(t *testing.T) {