-
Notifications
You must be signed in to change notification settings - Fork 5
/
clear_test.go
108 lines (93 loc) · 1.86 KB
/
clear_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
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/user"
"path/filepath"
"testing"
"time"
)
const (
maxFiles = 30
waitTime = 0
)
func TestClearTmpFolder(t *testing.T) {
u, err := user.Current()
if err != nil {
t.Fatal(err)
return
}
p := filepath.Join(u.HomeDir, "tmp/clear_test/")
err = os.MkdirAll(p, os.ModePerm)
if err != nil {
t.Fatal(err)
return
}
defer func() {
err = os.RemoveAll(p)
if err != nil {
t.Fatal(err)
}
}()
clear := clearTmpFolder(p)
n := populateTmpFolder(p)
time.Sleep(waitTime * time.Second)
elems, err := ioutil.ReadDir(p)
if err != nil {
t.Fatal(err)
return
}
fmt.Println(elems)
if len(elems) != n {
t.Fatal(fmt.Sprintf("Invalid number of elements after populate: %d expected, was %d", n, len(elems)))
return
}
clear()
elems, err = ioutil.ReadDir(p)
if err != nil {
t.Fatal(err)
return
}
fmt.Println(elems)
if len(elems) != 0 {
t.Fatal(fmt.Sprintf("Invalid number of elements after clear: 0 expected, was %d", len(elems)))
return
}
n = populateTmpFolder(p)
time.Sleep(waitTime * time.Second)
elems, err = ioutil.ReadDir(p)
if err != nil {
t.Fatal(err)
return
}
fmt.Println(elems)
if len(elems) != n {
t.Fatal(fmt.Sprintf("Invalid number of elements after populate: %d expected, was %d", n, len(elems)))
return
}
clear()
elems, err = ioutil.ReadDir(p)
if err != nil {
t.Fatal(err)
return
}
fmt.Println(elems)
if len(elems) != 0 {
t.Fatal(fmt.Sprintf("Invalid number of elements after clear: 0 expected, was %d", len(elems)))
return
}
}
func populateTmpFolder(path string) int {
s := rand.NewSource(time.Now().UnixNano())
n := int(s.Int63()) % maxFiles
for i := 0; i < n; i++ {
p := filepath.Join(path, fmt.Sprintf("folder%d", i))
err := os.Mkdir(p, os.ModePerm)
if err != nil {
panic(fmt.Sprintf("Could not create folder: %s", p))
}
}
return n
}