forked from verless/verless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_test.go
More file actions
179 lines (159 loc) · 5.15 KB
/
fs_test.go
File metadata and controls
179 lines (159 loc) · 5.15 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package fs
import (
"path/filepath"
"strings"
"testing"
"github.com/spf13/afero"
"github.com/verless/verless/test"
)
func TestIsSafeToRemove(t *testing.T) {
// Create new in-memory FS
tempFS := afero.NewMemMapFs()
// Normal file, should be safe to remove
_, _ = tempFS.Create("file.go")
// Empty directory, should be safe to remove
_ = tempFS.Mkdir("directory", 0755)
type args struct {
targetFs afero.Fs
path string
force bool
}
tests := []struct {
name string
args args
want bool
}{
// Add test entries here
{"Remove file", args{tempFS, "file.go", false}, false},
{"Remove directory", args{tempFS, "directory", false}, false},
{"Remove non-existent file", args{tempFS, "notexist", false}, true},
{"Force remove", args{tempFS, "file.go", true}, true},
}
for _, tt := range tests {
got := IsSafeToRemove(tt.args.targetFs, tt.args.path, tt.args.force)
test.Equals(t, got, tt.want)
}
}
func TestRmdir(t *testing.T) {
// Create new in-memory FS
tempFS := afero.NewMemMapFs()
// Single files, to be deleted
_, _ = tempFS.Create("file.go")
// Empty directory, to be deleted
_ = tempFS.Mkdir("emptydir", 0755)
// Nested directory, to test structure preservation
_ = tempFS.MkdirAll("first/second", 0755)
_, _ = tempFS.Create("first/second/third.go")
type args struct {
fs afero.Fs
path string
}
tests := []struct {
name string
args args
wantErr bool
}{
// Add test entries here
{"Remove empty directory", args{tempFS, "emptydir"}, false},
{"Remove file", args{tempFS, "file.go"}, false},
{"Remove directory recursively", args{tempFS, "first"}, false},
{"Try on non-existent file", args{tempFS, "notexist.go"}, false},
}
for _, tt := range tests {
err := Rmdir(tt.args.fs, tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("Rmdir() error = %v, wantErr %v", err, tt.wantErr)
test.ExpectedError(t, nil, err)
}
}
}
func TestCopyFromOS(t *testing.T) {
// Create blank in-memory FS.
tempFS := afero.NewMemMapFs()
// One directory for source files, one for destination files.
_ = tempFS.Mkdir("src", 0755)
_, _ = tempFS.Create("src/srcfile1.go")
_ = tempFS.Mkdir("dest", 0755)
// One nested directory for source files being copied with structure.
_ = tempFS.MkdirAll("nestdir/levelone", 0755)
_, _ = tempFS.Create("nestdir/levelone/srcfile2.go")
// One file in the root directory to copy into the destination.
_, _ = tempFS.Create("srcfile3.go")
// One empty directory, one with no read permissions.
_ = tempFS.Mkdir("empty", 0755)
_ = tempFS.Mkdir("restricted", 0000)
_, _ = tempFS.Create("restricted/cantread.go")
type args struct {
targetFs afero.Fs
src string
dest string
fileOnly bool
}
tests := []struct {
name string
args args
wantErr bool
}{
// Add test entries here
{"Copy from src to dest dir", args{tempFS, "src", "dest", true}, false},
{"Copy with source structure", args{tempFS, "nestdir", "dest", false}, false},
{"Copy file from parent of dest", args{tempFS, "srcfile3.go", "dest", false}, false},
{"Destination directory does not exist", args{tempFS, "nestdir", "newdest", false}, false},
{"Source directory is empty", args{tempFS, "empty", "dest", false}, false},
{"Source file does not exist", args{tempFS, "nofile.go", "dest", true}, false},
{"Source directory has no read perms", args{tempFS, "restricted", "dest", false}, false},
}
for _, tt := range tests {
err := CopyFromOS(tt.args.targetFs, tt.args.src, tt.args.dest, tt.args.fileOnly)
if (err != nil) != tt.wantErr {
t.Errorf("CopyFromOS() error = %v, wantErr %v", err, tt.wantErr)
}
}
}
func TestStreamFiles(t *testing.T) {
// Filter functions kept here for independence from fs.go
// MarkdownOnly is a filter that only lets pass Markdown files.
MarkdownOnly = func(file string) bool {
return filepath.Ext(file) == ".md"
}
// NoUnderscores is a predefined filter that doesn't let pass
// files starting with an underscore.
NoUnderscores = func(file string) bool {
filename := filepath.Base(file)
return !strings.HasPrefix(filename, "_")
}
// Filters:
noFilter := []func(string) bool{}
markFilter := []func(string) bool{MarkdownOnly}
underscoreFilter := []func(string) bool{NoUnderscores}
allFilter := []func(string) bool{MarkdownOnly, NoUnderscores}
type args struct {
path string
files chan string
filters []func(file string) bool
}
tests := []struct {
name string
args args
wantErr bool
}{
// Add test entries here
{"No filters", args{"../example", make(chan string), noFilter}, false},
{"Markdown filters", args{"../example", make(chan string), markFilter}, false},
{"Underscore filters", args{"../example", make(chan string), underscoreFilter}, false},
{"All filters", args{"../example", make(chan string), allFilter}, false},
{"Path does not exist", args{"notexist", make(chan string), allFilter}, false},
}
for _, tt := range tests {
go func() {
// Drain all data that goes into channel
for file := range tt.args.files {
_ = file
}
}()
err := StreamFiles(tt.args.path, tt.args.files, tt.args.filters...)
if (err != nil) != tt.wantErr {
t.Errorf("StreamFiles() error = %v, wantErr %v", err, tt.wantErr)
}
}
}