ftp/walker_test.go

212 lines
3.5 KiB
Go
Raw Normal View History

package ftp
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
2020-07-08 08:33:57 +02:00
"github.com/stretchr/testify/require"
)
func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("root")
assert.Equal(t, "root/", w.root)
assert.Equal(t, &c, &w.serverConn)
}
func TestFieldsReturnCorrectData(t *testing.T) {
w := Walker{
2020-04-25 18:27:59 +02:00
cur: &item{
path: "/root/",
2020-04-25 18:27:59 +02:00
err: fmt.Errorf("this is an error"),
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
},
}
2020-04-25 18:27:59 +02:00
assert.Equal(t, "this is an error", w.Err().Error())
assert.Equal(t, "/root/", w.Path())
assert.Equal(t, EntryTypeFolder, w.Stat().Type)
}
func TestSkipDirIsCorrectlySet(t *testing.T) {
w := Walker{}
w.SkipDir()
assert.Equal(t, false, w.descend)
}
func TestNoDescendDoesNotAddToStack(t *testing.T) {
2020-04-25 18:27:59 +02:00
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("/root")
w.cur = &item{
path: "/root/",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
}
2020-04-25 18:27:59 +02:00
w.stack = []*item{
2020-07-08 08:33:57 +02:00
{
path: "file",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "file",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
}
w.SkipDir()
result := w.Next()
assert.Equal(t, true, result, "Result should return true")
2019-05-02 10:29:41 +02:00
assert.Equal(t, 0, len(w.stack))
assert.Equal(t, true, w.descend)
}
func TestEmptyStackReturnsFalse(t *testing.T) {
2020-07-08 08:33:57 +02:00
assert, require := assert.New(t), require.New(t)
2020-04-25 18:27:59 +02:00
mock, err := newFtpMock(t, "127.0.0.1")
2020-10-20 20:16:16 +02:00
require.Nil(err)
2020-04-25 18:27:59 +02:00
defer mock.Close()
c, cErr := Connect(mock.Addr())
2020-10-20 20:16:16 +02:00
require.Nil(cErr)
2020-04-25 18:27:59 +02:00
w := c.Walk("/root")
w.cur = &item{
path: "/root/",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "root",
Size: 123,
Time: time.Now(),
Type: EntryTypeFolder,
},
}
2020-04-25 18:27:59 +02:00
w.stack = []*item{}
w.SkipDir()
result := w.Next()
2020-07-08 08:33:57 +02:00
assert.Equal(false, result, "Result should return false")
}
func TestCurAndStackSetCorrectly(t *testing.T) {
2020-07-08 08:33:57 +02:00
assert, require := assert.New(t), require.New(t)
2020-04-25 18:27:59 +02:00
mock, err := newFtpMock(t, "127.0.0.1")
2020-10-20 20:16:16 +02:00
require.Nil(err)
2020-04-25 18:27:59 +02:00
defer mock.Close()
c, cErr := Connect(mock.Addr())
2020-10-20 20:16:16 +02:00
require.Nil(cErr)
2020-04-25 18:27:59 +02:00
w := c.Walk("/root")
w.cur = &item{
path: "root/file1",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "file1",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
}
2020-04-25 18:27:59 +02:00
w.stack = []*item{
2020-07-08 08:33:57 +02:00
{
path: "file",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "file",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
2020-07-08 08:33:57 +02:00
{
path: "root/file1",
err: nil,
2020-04-25 18:27:59 +02:00
entry: &Entry{
Name: "file1",
Size: 123,
Time: time.Now(),
Type: EntryTypeFile,
},
},
}
result := w.Next()
2020-10-21 22:10:46 +02:00
assert.Equal(true, result, "Result should return true")
result = w.Next()
2020-07-08 08:33:57 +02:00
assert.Equal(true, result, "Result should return true")
assert.Equal(0, len(w.stack))
assert.Equal("file", w.cur.entry.Name)
}
2020-06-15 02:53:47 +02:00
func TestCurInit(t *testing.T) {
mock, err := newFtpMock(t, "127.0.0.1")
if err != nil {
t.Fatal(err)
}
defer mock.Close()
c, cErr := Connect(mock.Addr())
if cErr != nil {
t.Fatal(err)
}
w := c.Walk("/root")
result := w.Next()
// mock fs has one file 'lo'
assert.Equal(t, true, result, "Result should return false")
assert.Equal(t, 0, len(w.stack))
assert.Equal(t, "/root/lo", w.Path())
}