forked from oliwur/directory_stat_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirstatCounter_test.go
103 lines (86 loc) · 2.6 KB
/
dirstatCounter_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
package main
import (
"io/ioutil"
"log"
"os"
"testing"
)
func setupTmpDir() string {
tmpDir, err := ioutil.TempDir("", "dirstat_test_")
if err != nil {
log.Fatal("could not create temp dir", err)
}
return tmpDir
}
func TestGetFileCountZero(t *testing.T) {
tmpDir := setupTmpDir()
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
log.Printf("could not remove tmpDir")
}
}()
t.Run("given a empty directory when counted not recursively then it should return 0", func(t *testing.T) {
count := getFileCount(tmpDir, false)
if count != 0 {
t.Fail()
t.Errorf("there are no documents in this folder. the count must be zero. it is %d!\n", count)
}
})
t.Run("given an empty dir when counted recursively then it should return 0", func(t *testing.T) {
count := getFileCount(tmpDir, true)
if count != 0 {
t.Fail()
t.Error("the value shoud be 0 but was", count)
}
})
}
func TestGetFileCountThree(t *testing.T) {
tmpDir := setupTmpDir()
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
log.Printf("could not remove tmpDir")
}
}()
_, _ = ioutil.TempFile(tmpDir, "file1")
_, _ = ioutil.TempFile(tmpDir, "file2")
_, _ = ioutil.TempFile(tmpDir, "file3")
t.Run("given a directory with 3 files when counted not recursively then it should return 3", func(t *testing.T) {
count := getFileCount(tmpDir, false)
if count != 3 {
t.Fail()
t.Errorf("there are no documents in this folder. the count must be zero. it is %d!\n", count)
}
})
t.Run("given a directory with 3 files when counted recursively then it should return 3", func(t *testing.T) {
count := getFileCount(tmpDir, true)
if count != 3 {
t.Fail()
t.Errorf("there are 3 files in the directory, it reported %d\n", count)
}
})
}
func TestGetFileCountNotExisting(t *testing.T) {
tmpDir := setupTmpDir()
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
log.Printf("could not remove tmpDir")
}
}()
t.Run("given the directory does not exist when counted non recursively then it should return -1 indicating an error", func(t *testing.T) {
count := getFileCount(tmpDir+"_this_dir_does_not_exist", false)
if count != -1 {
t.Fail()
t.Errorf("it should return -1, because the dir does not exist. it returned %d instead.\n", count)
}
})
t.Run("given the directory does not exist when counted recursively then it should return -1 indicating an error", func(t *testing.T) {
count := getFileCount(tmpDir+"_this_dir_does_not_exist", true)
if count != -1 {
t.Fail()
t.Errorf("it should return -1, because the dir does not exist. it returned %d instead.\n", count)
}
})
}