-
Notifications
You must be signed in to change notification settings - Fork 2
/
mock_client_test.go
91 lines (72 loc) · 2.26 KB
/
mock_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package go_ftp_test
import (
"errors"
"io"
"io/fs"
"strings"
"testing"
ftp "github.com/moov-io/go-ftp"
"github.com/stretchr/testify/require"
)
func TestMockClient(t *testing.T) {
client := ftp.NewMockClient(t)
require.NoError(t, client.Ping())
defer require.NoError(t, client.Close())
_, err := client.Open("/missing.txt")
require.Error(t, err)
err = client.Delete("/missing.txt")
require.Error(t, err)
body := io.NopCloser(strings.NewReader("contents"))
err = client.UploadFile("/exists.txt", body)
require.NoError(t, err)
client.Err = errors.New("bad error")
err = client.UploadFile("/exists.txt", body)
require.Error(t, err)
// reset mock client err
client.Err = nil
paths, err := client.ListFiles("/")
require.NoError(t, err)
require.Len(t, paths, 1)
require.Equal(t, "/exists.txt", paths[0])
}
func TestMockClient_ListAndOpenFiles(t *testing.T) {
client := ftp.NewMockClient(t)
require.NoError(t, client.Ping())
defer require.NoError(t, client.Close())
require.NoError(t, client.UploadFile("/path/f1.txt", io.NopCloser(strings.NewReader("foo"))))
require.NoError(t, client.UploadFile("/path/f2.txt", io.NopCloser(strings.NewReader("foo"))))
foundFiles, err := client.ListFiles("/path/")
require.NoError(t, err)
require.Len(t, foundFiles, 2)
for _, file := range foundFiles {
found, err := client.Open(file)
require.NoError(t, err)
require.NotNil(t, found)
contents, err := io.ReadAll(found.Contents)
require.NoError(t, err)
require.Equal(t, "foo", string(contents))
require.NoError(t, found.Close())
// Consume the file with Reader
found, err = client.Reader(file)
require.NoError(t, err)
require.NotNil(t, found)
contents, err = io.ReadAll(found.Contents)
require.NoError(t, err)
require.Equal(t, "foo", string(contents))
require.NoError(t, found.Close())
}
// Walk the directory and list files
var walkedFiles []string
err = client.Walk("/path/", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
walkedFiles = append(walkedFiles, path)
return nil
})
require.NoError(t, err)
require.Contains(t, walkedFiles, "f1.txt", "f2.txt")
}