-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwastebasket_test.go
207 lines (190 loc) · 4.75 KB
/
wastebasket_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
package wastebasket
import (
"fmt"
"os"
"strings"
"testing"
)
func generateManyFileNames(count int) []string {
fileNames := make([]string, 0, count)
for i := 1; i <= count; i++ {
fileNames = append(fileNames, fmt.Sprintf("%d.txt", i))
}
return fileNames
}
func Test_Trash(t *testing.T) {
type trashExpectation struct {
trasher func() error
expectedErr error
}
type testCase struct {
name string
testDataToCreate []string
trashExpectations []trashExpectation
expectedFiles []string
}
cases := []testCase{
{
name: "existent file, no edge cases",
testDataToCreate: []string{"test.txt"},
trashExpectations: []trashExpectation{
{trash("test.txt"), nil},
},
expectedFiles: nil,
},
{
name: "existent file, spaces in name",
testDataToCreate: []string{"te st.txt"},
trashExpectations: []trashExpectation{
{trash("te st.txt"), nil},
},
expectedFiles: nil,
},
{
name: "existent file, spaces in name and ./ when deleting",
testDataToCreate: []string{"te st.txt"},
trashExpectations: []trashExpectation{
{trash("./te st.txt"), nil},
},
expectedFiles: nil,
},
{
name: "non-existent file",
testDataToCreate: nil,
trashExpectations: []trashExpectation{
{trash("doesntexist.txt"), nil},
},
expectedFiles: nil,
},
{
name: "existent empty directory",
testDataToCreate: []string{"folder/"},
trashExpectations: []trashExpectation{
{trash("folder"), nil},
},
expectedFiles: nil,
},
{
name: "existent non empty directory",
testDataToCreate: []string{"folder/", "folder/file.txt"},
trashExpectations: []trashExpectation{
{trash("folder"), nil},
},
expectedFiles: nil,
},
{
name: "existent directory with spaces in name",
testDataToCreate: []string{"fol der/"},
trashExpectations: []trashExpectation{
{trash("fol der"), nil},
},
expectedFiles: nil,
},
{
name: "delete two files in one call",
testDataToCreate: []string{"a.txt", "b.txt"},
trashExpectations: []trashExpectation{
{trash("a.txt", "b.txt"), nil},
},
expectedFiles: nil,
},
{
name: "mix of folders and files",
testDataToCreate: []string{"a/", "b.txt", "c/", "c/file-in-c.txt", "c/another-file-in-c.txt"},
trashExpectations: []trashExpectation{
{trash("a", "b.txt", "c/file-in-c.txt", "c"), nil},
},
expectedFiles: nil,
},
{
name: "large amount of files",
testDataToCreate: generateManyFileNames(257),
trashExpectations: []trashExpectation{
{trash(generateManyFileNames(257)...), nil},
},
expectedFiles: nil,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
defer writeTestData(t, c.testDataToCreate...)()
for _, expectation := range c.trashExpectations {
err := expectation.trasher()
if err != expectation.expectedErr {
t.Errorf("unexpected error: %v != %v", err, expectation.expectedErr)
}
}
OUTER_LOOP:
for _, file := range c.testDataToCreate {
_, err := os.Stat(file)
for _, expectedFile := range c.expectedFiles {
if file == expectedFile {
if os.IsNotExist(err) {
t.Errorf("File %s doesn't exist, but was expected to", file)
}
continue OUTER_LOOP
}
}
if err == nil {
t.Errorf("file %s shouldn't exist, but does", file)
}
}
})
}
}
// TestEmpty tests emptying the systems trashbin
func Test_Empty(t *testing.T) {
error := Empty()
if error != nil {
t.Errorf("Error emptying trashbin. (%s)", error.Error())
}
//Can I find a way to see if this actually worked?
}
func assertExists(t *testing.T, path string) {
_, err := os.Stat(path)
if err != nil {
t.Errorf("path '%s' doesn't exist", path)
}
}
func assertNotExists(t *testing.T, path string) {
_, err := os.Stat(path)
if !os.IsNotExist(err) {
t.Errorf("path '%s' exists, but shouldn't", path)
}
}
func trash(paths ...string) func() error {
return func() error {
return Trash(paths...)
}
}
func writeTestData(t *testing.T, paths ...string) func() {
for _, path := range paths {
var err error
if strings.HasSuffix(path, "/") {
err = os.Mkdir(path, os.ModePerm)
} else {
err = os.WriteFile(path, []byte("test"), os.ModePerm)
}
if err != nil {
t.Errorf("Error writing test data. (%s)", err.Error())
}
}
return func() {
t.Log("Cleaning up test files")
for _, path := range paths {
os.RemoveAll(path)
}
for _, path := range paths {
_, err := os.Stat(path)
if os.IsNotExist(err) {
continue
}
if err == nil {
t.Errorf("error, file hasn't been deleted.")
} else {
t.Errorf("unexpected error cleaning up: %s", err)
}
}
t.Log("Done cleaning up test files")
}
}