Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add "list and read" checks #8

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func readResponse(resp *ftp.Response) (io.ReadCloser, error) {
//
// See https://github.com/moovfinancial/paygate/issues/494
if n == 0 && err == nil {
return nil, nil
return io.NopCloser(&buf), nil
}
if err != nil {
return nil, fmt.Errorf("n=%d error=%v", n, err)
Expand Down
39 changes: 39 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package go_ftp_test

import (
"bytes"
"fmt"
"io"
"io/fs"
"math/rand"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -96,6 +98,41 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, filenames, []string{"/archive/old.txt", "/archive/empty2.txt"})
})

t.Run("list and read", func(t *testing.T) {
filenames, err := client.ListFiles("/with-empty")
require.NoError(t, err)

// randomize filename order
rand.Shuffle(len(filenames), func(i, j int) {
filenames[i], filenames[j] = filenames[j], filenames[i]
})
require.ElementsMatch(t, filenames, []string{
"/with-empty/EMPTY1.txt", "/with-empty/empty_file2.txt",
"/with-empty/data.txt", "/with-empty/data2.txt",
})

// read each file and get back expected contents
var contents []string
for i := range filenames {
var file *go_ftp.File
if i/2 == 0 {
file, err = client.Open(filenames[i])
} else {
file, err = client.Reader(filenames[i])
}
require.NoError(t, err, fmt.Sprintf("filenames[%d]", i))
require.NotNil(t, file, fmt.Sprintf("filenames[%d]", i))
require.NotNil(t, file.Contents, fmt.Sprintf("filenames[%d]", i))

bs, err := io.ReadAll(file.Contents)
require.NoError(t, err)

contents = append(contents, string(bs))
}

require.ElementsMatch(t, contents, []string{"", "", "also data\n", "has data\n"})
})

t.Run("walk", func(t *testing.T) {
var found []string
err := client.Walk(".", func(path string, d fs.DirEntry, err error) error {
Expand All @@ -106,6 +143,8 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, found, []string{
"first.txt", "second.txt", "empty.txt",
"archive/old.txt", "archive/empty2.txt",
"with-empty/EMPTY1.txt", "with-empty/empty_file2.txt",
"with-empty/data.txt", "with-empty/data2.txt",
})
})

Expand Down
Empty file.
1 change: 1 addition & 0 deletions testdata/ftp-server/with-empty/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
has data
1 change: 1 addition & 0 deletions testdata/ftp-server/with-empty/data2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
also data
Empty file.
Loading