Skip to content

Commit

Permalink
test: add "list and read" checks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Aug 22, 2023
1 parent 2cbbd22 commit 628c601
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/fs"
"math/rand"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -111,6 +112,41 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, files, []string{"outbox/archive/empty2.txt", "outbox/archive/three.txt"})
})

t.Run("list and read", func(t *testing.T) {
filenames, err := client.ListFiles("/outbox/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{
"/outbox/with-empty/EMPTY1.txt", "/outbox/with-empty/empty_file2.txt",
"/outbox/with-empty/data.txt", "/outbox/with-empty/data2.txt",
})

// read each file and get back expected contents
var contents []string
for i := range filenames {
var file *sftp.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 walkedFiles []string
err = client.Walk(".", func(path string, info fs.DirEntry, err error) error {
Expand All @@ -124,6 +160,8 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, walkedFiles, []string{
"outbox/one.txt", "outbox/two.txt", "outbox/empty.txt",
"outbox/archive/empty2.txt", "outbox/archive/three.txt",
"outbox/with-empty/EMPTY1.txt", "outbox/with-empty/empty_file2.txt",
"outbox/with-empty/data.txt", "outbox/with-empty/data2.txt",
})
})

Expand All @@ -140,6 +178,8 @@ func TestClient(t *testing.T) {
require.ElementsMatch(t, walkedFiles, []string{
"/outbox/one.txt", "/outbox/two.txt", "/outbox/empty.txt",
"/outbox/archive/empty2.txt", "/outbox/archive/three.txt",
"/outbox/with-empty/EMPTY1.txt", "/outbox/with-empty/empty_file2.txt",
"/outbox/with-empty/data.txt", "/outbox/with-empty/data2.txt",
})
})

Expand Down
Empty file.
1 change: 1 addition & 0 deletions testdata/outbox/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/outbox/with-empty/data2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
also data
Empty file.

0 comments on commit 628c601

Please sign in to comment.