-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
279 lines (259 loc) · 6.08 KB
/
manager_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"sync/atomic"
"testing"
)
var (
folderID int64
)
// createFilesForTest creates files in a given directory
// used for testing
func createFilesForTest(t *testing.T, dir string, fs []string) {
for _, fn := range fs {
fn = filepath.Join(dir, fn)
parent := path.Dir(fn)
_ = os.Mkdir(parent, os.ModePerm)
f, err := os.Create(fn)
if err != nil {
t.Fatalf("cannot create file %s: %v", fn, err)
}
if err := f.Close(); err != nil {
t.Fatalf("cannot close file %s: %v", fn, err)
}
}
}
// getFilesInDir get the list of files in a given directory
// used for testing
func getFilesInDir(t *testing.T, dir string) []string {
fis, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("cannot read %s: %v", dir, err)
}
fns := make([]string, 0)
for _, fi := range fis {
fns = append(fns, fi.Name())
}
return fns
}
func getManager(t *testing.T) (m *Manager, homeDir string) {
m, err := NewManager()
if err != nil {
t.Fatalf("cannot create manager: %v", err)
}
homeDir = "/tmp/test_tnote_" + string(atomic.AddInt64(&folderID, 1))
if err := m.setHomeDir(homeDir); err != nil {
t.Fatalf("cannot set homeDir %s: %v", homeDir, err)
}
return
}
func TestCreatingManager(t *testing.T) {
manager, err := NewManager()
if err != nil {
t.Fatalf("expected no error, but found %v", err)
}
if manager == nil {
t.Fatalf("expected non-nil value, but received nil")
}
}
func TestList(t *testing.T) {
tests := []struct {
name string
has []string
expected []string
}{
{
name: "testing simple files",
has: []string{"file1.md", "file2.md"},
expected: []string{"file1", "file2"},
},
{
name: "testing files with dashes",
has: []string{"file-with-dash.md"},
expected: []string{"file-with-dash"},
},
{
name: "testing files with underscore",
has: []string{"file_with_underscore.md"},
expected: []string{"file_with_underscore"},
},
{
name: "testing files with two dots",
has: []string{"file_with_two_mds.md.md"},
expected: []string{"file_with_two_mds.md"},
},
{
name: "testing files in folders",
has: []string{"folder/file1.md", "file2.md"},
expected: []string{"file2", "folder/file1"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
m, dir := getManager(t)
defer func() { _ = os.RemoveAll(dir) }()
createFilesForTest(t, dir, test.has)
got, err := m.List()
if err != nil {
t.Errorf("failed to list files: %v", err)
}
if !reflect.DeepEqual(got, test.expected) {
t.Errorf("got %v, but expected %v", got, test.expected)
}
})
}
}
func TestCreate(t *testing.T) {
tests := []struct {
name string
has []string
noteid string
expected []string
err bool
}{
{
name: "testing creating simple file",
has: []string{},
noteid: "file",
expected: []string{"file.md"},
err: false,
},
{
name: "testing simple file with .md",
has: []string{},
noteid: "file.md",
expected: []string{"file.md.md"},
err: false,
},
{
name: "testing simple file with underscore",
has: []string{},
noteid: "file_with_underscore",
expected: []string{"file_with_underscore.md"},
err: false,
},
{
name: "fail to create duplicate file",
has: []string{"file.md"},
noteid: "file",
expected: []string{"file.md"},
err: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
m, dir := getManager(t)
defer func() { _ = os.RemoveAll(dir) }()
createFilesForTest(t, dir, test.has)
if err := m.Create(test.noteid); (err != nil) != test.err {
t.Fatalf("got err=%v, exptected err=%v", err, test.err)
}
if test.err {
return
}
fns := getFilesInDir(t, dir)
if !reflect.DeepEqual(fns, test.expected) {
t.Fatalf("have %v in dir, but expected %v", fns, test.expected)
}
})
}
}
func TestRemove(t *testing.T) {
tests := []struct {
name string
has []string
noteid string
expected []string
err bool
}{
{
name: "should remove simple file",
has: []string{"file.md", "extrafile.md"},
noteid: "file",
expected: []string{"extrafile.md"},
err: false,
},
{
name: "should remove simple file even if there is a similarly named file",
has: []string{"file.md", "file_.md", "extrafile.md"},
noteid: "file",
expected: []string{"extrafile.md", "file_.md"},
err: false,
},
{
name: "should fails to remove non-existing file",
has: []string{"extrafile.md"},
noteid: "file",
expected: nil,
err: true,
},
}
for _, test := range tests {
m, dir := getManager(t)
defer func() { _ = os.RemoveAll(dir) }()
createFilesForTest(t, dir, test.has)
if err := m.Remove(test.noteid); (err != nil) != test.err {
t.Fatalf("got err=%v, expected err=%v", err, test.err)
}
if test.err {
return
}
fns := getFilesInDir(t, dir)
if !reflect.DeepEqual(fns, test.expected) {
t.Fatalf("got %v, but expected %v", fns, test.expected)
}
}
}
func TestExists(t *testing.T) {
tests := []struct {
name string
has []string
noteid string
expect bool
err bool
}{
{
name: "file should exists",
has: []string{"file.md"},
noteid: "file",
expect: true,
err: false,
},
{
name: "file should not exists",
has: []string{"file.md"},
noteid: "none_existing_note",
expect: false,
err: false,
},
{
name: "file should not exists event if extension is provided",
has: []string{"file.md"},
noteid: "file.md",
expect: false,
err: false,
},
}
for _, test := range tests {
m, dir := getManager(t)
defer func() { _ = os.RemoveAll(dir) }()
createFilesForTest(t, dir, test.has)
got, err := m.Exists(test.noteid)
if err != nil {
if !test.err {
t.Fatalf("got err: %v", err)
}
return
}
if test.err {
t.Fatalf("got no error, but expected err")
}
if got != test.expect {
t.Fatalf("got %v, expected %v", got, test.expect)
}
}
}