-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
368 lines (345 loc) · 9.58 KB
/
util_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"bytes"
"fmt"
"log"
"sort"
"strings"
"testing"
"time"
"math/rand"
)
func Test_compare_string_file_elements(t *testing.T) {
base := make([]string, 0)
base = append(base, "one")
data, err := compare_string_file_elements(base, base)
if err != nil {
t.Fatal(err)
}
if len(data) > 0 && data[0] != "" {
t.Fatal("data is borked")
}
}
func Test_should_deal_with_bad_data(t *testing.T) {
a := []byte("1234 bar no newline")
b := []byte("1234 bar no newline")
if !bytes.Contains([]byte("1234\n"), []byte("\n")) {
t.Fatal("Error, compare doesnt work")
}
result, err := compare_file_elements(a, b)
// only if we dont get an error do we fail the test
if err == nil {
t.Fatal(err)
}
if len(result) > 0 {
t.Fatal("Expected no differences")
}
}
func Test_compare_file_strings(t *testing.T) {
one := []byte{'a',' ','b',' ','c', '\n'}
two := []byte{'a',' ','b',' ','c', '\n'}
difference, err := compare_file_elements(one,two)
if err != nil {
t.Fatal(err)
}
if len(difference) != 0 {
log.Printf("difference = %+v\n", difference)
t.Fatal("Difference expected to be 0, got ", len(difference))
}
one = []byte{'a',' ','b',' ','c', '\n'}
two = []byte{'a',' ','b',' ','c', '\n'}
difference, err = compare_file_elements(one,two)
if err != nil {
t.Fatal(err)
}
if len(difference) != 0 {
t.Fatal("Difference expected to be 0")
}
}
func Test_compare_file_elements_simple(t *testing.T) {
a := []byte("1234 bar\n")
b := []byte("1234 bar\n")
data, err := compare_file_elements(a, b)
if err != nil {
t.Fatal(err)
}
if len(data) != 0 {
fmt.Printf("Expected nil data being returned")
t.Fatal()
}
}
func Test_compare_file_elements(t *testing.T) {
// as file is missing in b it wont be returned!
a := []byte("1234 bar\n1345 foo\n")
b := []byte("1234 bar\n")
data, err := compare_file_elements(a, b)
if err != nil {
t.Fatal(err)
}
if len(data) != 0 {
fmt.Printf("Expected nil data being returned")
log.Printf("data = %+v\n", data)
t.Fatal()
}
}
func Test_compare_file_elements_new_file(t *testing.T) {
// new file in seccond set of files
a := []byte("1234 bar\n")
b := []byte("1234 bar\n1345 foo\n")
data, err := compare_file_elements(a, b)
expected := "1345"
if err != nil {
t.Fatal(err)
}
if len(data) != 1 {
log.Println("Expected 1 element being returned")
log.Printf("Got = %+v\n", len(data))
log.Printf("data = %+v\n", data)
t.Fatal()
}
if data[0] != expected {
fmt.Printf("Expected %s, got %s\n)", expected, data[0])
t.Error("Expected ", expected, "got", data[0])
}
}
func Test_compare_file_elements2(t *testing.T) {
a := []byte("1234 bar \n")
b := []byte("1234 bar \n1345 foo\n")
data, _ := compare_file_elements(a, b)
expected := "1345"
if data[0] != expected {
fmt.Printf("Expected %s, got %s ::\n)", expected, data[0])
t.Error()
}
}
func Test_compare_file_elements3(t *testing.T) {
a := []byte("1234 bar \n2222 nope")
b := []byte("1234 bar \n1345 foo\n2222 nope")
data, err := compare_file_elements(a, b)
if err != nil {
t.Fatal(err)
}
expected := "1345"
if data[0] != expected {
fmt.Printf("Expected %s, got %s ::\n)", expected, data[0])
t.Error()
}
}
func Test_compare_file_many(t *testing.T) {
a := []byte("1234 bar \n2222 nope\n3333 nope")
b := []byte("1234 bar \n1345 foo\n2222 nope\n3334 yup")
data, err := compare_file_elements(a, b)
if err != nil {
t.Fatal(err)
}
expected := "1345"
if data[0] != expected {
fmt.Printf("Expected %s, got %s ::\n)", expected, data[0])
t.Error()
}
}
func assert_equal(t *testing.T, expected string, data string) {
if data != expected {
fmt.Printf("Expected '%s', got '%s'\n)", expected, data)
t.Error()
}
}
func Test_create_fileset(t *testing.T) {
var mock_dirlist []HackFile
mock_dirlist = append(mock_dirlist, HackFile{"foo", "p", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"bar", "p", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"foo", "/q", time.Now(), 1000, "ab34"})
compare := string(mock_dirlist[0].name)
assert_equal(t, "foo", compare)
}
func Test_make_list(t *testing.T) {
var mock_dirlist []HackFile
mock_dirlist = append(mock_dirlist, HackFile{"foo", "p", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"bar", "p", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"foo", "/q", time.Now(), 1000, "ab34"})
foo := testable_make_list(mock_dirlist)
compare := "bar"
if strings.Contains(foo[0], compare) {
log.Printf("foo = %+v\n", foo)
t.Error("Couldnt make backup list")
}
}
func Test_make_big_list(t *testing.T) {
files := discover_files("~/")
backupset := testable_make_list(files)
if backupset == nil {
t.Fatal("Couldnt make list from home directory")
}
}
func Test_compare_identical(t *testing.T) {
files := discover_files("~/")
backupset := testable_make_list(files)
files = discover_files("~/")
backupset2 := testable_make_list(files)
//fmt.Printf("backupset = %+v\n", backupset)
//fmt.Printf("backupset = %+v\n", backupset2)
target_files, err := compare_string_file_elements(backupset, backupset2)
if err != nil {
t.Fatal(err)
}
if len(target_files) > 0 && target_files[0] != "" {
t.Fatal("Error, sets should be the same")
}
// save lists to disk then compare
var path string
path, err = save_backupset_disk(files)
if err != nil {
log.Printf("path = %+v\n", path)
t.Fatal(err)
}
}
func Test_sort_list(t *testing.T) {
mock_dirlist := make([]HackFile, 0)
mock_dirlist = append(mock_dirlist, HackFile{"foo", "p", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"bar", "/a/a", time.Now(), 1000, "ab34"})
mock_dirlist = append(mock_dirlist, HackFile{"foo", "/a/b", time.Now(), 1000, "ab34"})
// sort the slice
sort.Sort(ByPath(mock_dirlist))
compare := "bar"
if mock_dirlist[0].name != compare {
fmt.Printf("compare = %+v\n", compare)
fmt.Printf("mock_dirlist = %+v\n", mock_dirlist)
t.Error("didnt sort")
}
}
func Test_make_filename(t *testing.T) {
data := make_filename()
if len(data) != 28 {
t.Log(len(data))
t.Fatal("problem with the date generator")
}
}
func Test_runtwobackups(t *testing.T) {
backup := make([]HackFile, 0)
for i := 0; i < 3; i++ {
backup = append(backup,mock_file())
}
backupset := testable_make_list(backup)
//alter := rand.Intn(len(backup))
newfile := mock_file()
backup = append(backup, newfile)
backupset2 := testable_make_list(backup)
result, err := compare_string_file_elements(backupset,backupset2)
if err != nil {
t.Fatal(err)
}
if len(backup) != 4 {
log.Printf("backup = %+v\n", backup)
t.Fatal("backup list should be 4")
}
if len(result) != 1 {
fmt.Printf("backupset = %+v\n", backupset)
fmt.Printf("backupset2 = %+v\n", backupset2)
fmt.Printf("result = %+v\n", result)
t.Fatal("Expected 1 file got", len(result))
}
}
func Test_runtwobackups_sort(t *testing.T) {
backup := make([]HackFile, 0)
backup = append(backup,mock_file())
backupset := testable_make_list(backup)
last := "a"
for _, value := range backupset {
if strings.Split(value," ")[0] < last {
log.Printf("backupset = %+v\n", backupset)
log.Printf("strings.Split(value, ) = %+v\n", strings.Split(value, " ")[0])
t.Fatal("List of length 1 is out of order?")
}
last = value
}
for i := 0; i < 5; i++ {
backup = append(backup,mock_file())
}
backupset = testable_make_list(backup)
last = "path/File-0a"
var compare []string
for _, value := range backupset {
compare = strings.Split(value," ")
if compare[0] < last {
log.Printf("backupset = %+v\n", backupset)
log.Printf("last = %+v\n", last)
log.Printf("compare = %+v\n", compare[0])
t.Fatal("sorted values are out of order")
}
last = compare[0]
}
}
func Test_runtwobackups_with_alter(t *testing.T) {
backup := make([]HackFile, 0)
for i := 0; i < 3; i++ {
backup = append(backup,mock_file())
}
backupset := testable_make_list(backup)
alter := rand.Intn(len(backup))
newfile := mock_file()
// line bellow will alter the array
backup[alter] = newfile
backupset2 := testable_make_list(backup)
result, err := compare_string_file_elements(backupset,backupset2)
if err != nil {
t.Fatal(err)
}
if len(result) != 1 {
fmt.Println("----------------")
fmt.Printf("backupset = %+v\n", backupset)
fmt.Printf("backupset2 = %+v\n", backupset2)
fmt.Printf("result = %+v\n", result)
t.Fatal("Expected 1 file got", len(result))
}
}
func Test_runtwobackups_with_delete(t *testing.T) {
backup := make([]HackFile, 0)
for i := 0; i < 5; i++ {
backup = append(backup,mock_file())
}
backupset := testable_make_list(backup)
alter := rand.Intn(len(backup))
alter = 1
backup = append(backup[:alter],backup[alter +1 :]...)
if len(backup) != 4 {
t.Fatal("Delete isnt working")
}
backupset2 := testable_make_list(backup)
result, err := compare_string_file_elements(backupset,backupset2)
if err != nil {
t.Fatal(err)
}
if len(result) != 0 {
fmt.Printf("backupset = %+v\n", backupset)
fmt.Printf("backupset2 = %+v\n", backupset2)
fmt.Printf("result = %+v\n", result)
t.Fatal("Expected 0 files got", len(result))
}
}
func Test_just_use_hash(t *testing.T) {
backupset := make(map[string]HackFile)
backupset["foo"] = mock_file()
if len(backupset) != 1 {
t.Fatal("length expected 1")
}
_, exists := backupset["bar"]
if exists != false {
t.Fatal("Bad guess")
}
var mock HackFile
for i:= 0; i <5 ; i++ {
mock = mock_file()
backupset[mock.path] = mock
}
}
func mock_file() HackFile {
pick := rand.Intn(100)
d := fmt.Sprintf("%d", pick)
name := "File-" + d + "xx"
return HackFile{name, "path", time.Now(), 1234, "hash"}
}
/*
test ideas
run two backups then run the clean up
make a loop and do it 100 times! backup and clean...
*/