-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirs_test.go
66 lines (59 loc) · 1.87 KB
/
dirs_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
package fs
import (
"context"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestHomeDir(t *testing.T) {
require.True(t, HomeDir().IsDir(), "home directory exists")
}
func Test_listDirMaxImpl(t *testing.T) {
ctx := context.Background()
errCtx, cancel := context.WithCancel(context.Background())
cancel()
list := func(files ...File) func(ctx context.Context, callback func(File) error) error {
return func(ctx context.Context, callback func(File) error) error {
if ctx.Err() != nil {
return ctx.Err()
}
for _, file := range files {
err := callback(file)
if err != nil {
return err
}
}
return nil
}
}
type args struct {
ctx context.Context
max int
listDir func(ctx context.Context, callback func(File) error) error
}
tests := []struct {
name string
args args
wantFiles []File
wantErr bool
}{
{name: "-1", args: args{ctx: ctx, max: -1, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2", "3"}},
{name: "-1 no files", args: args{ctx: ctx, max: -1, listDir: list()}, wantFiles: nil},
{name: "0", args: args{ctx: ctx, max: 0, listDir: list("1", "2", "3")}, wantFiles: nil},
{name: "1", args: args{ctx: ctx, max: 1, listDir: list("1", "2", "3")}, wantFiles: []File{"1"}},
{name: "2", args: args{ctx: ctx, max: 2, listDir: list("1", "2", "3")}, wantFiles: []File{"1", "2"}},
{name: "context error", args: args{ctx: errCtx, max: -1, listDir: list("1", "2", "3")}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotFiles, err := listDirMaxImpl(tt.args.ctx, tt.args.max, tt.args.listDir)
if (err != nil) != tt.wantErr {
t.Errorf("ListDirMaxImpl() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotFiles, tt.wantFiles) {
t.Errorf("ListDirMaxImpl() = %v, want %v", gotFiles, tt.wantFiles)
}
})
}
}