forked from dundee/gdu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_test.go
111 lines (79 loc) · 2.89 KB
/
import_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
package report
import (
"bytes"
"errors"
"testing"
"github.com/dundee/gdu/v5/pkg/analyze"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func init() {
log.SetLevel(log.WarnLevel)
}
func TestReadAnalysis(t *testing.T) {
buff := bytes.NewBuffer([]byte(`
[1,2,{"progname":"gdu","progver":"development","timestamp":1626806293},
[{"name":"/home/xxx","mtime":1629333600},
{"name":"gdu.json","asize":33805233,"dsize":33808384},
{"name":"sock","notreg":true},
[{"name":"app"},
{"name":"app.go","asize":4638,"dsize":8192},
{"name":"app_linux_test.go","asize":1410,"dsize":4096},
{"name":"app_linux_test2.go","ino":1234,"hlnkc":true,"asize":1410,"dsize":4096},
{"name":"app_test.go","asize":4974,"dsize":8192}],
{"name":"main.go","asize":3205,"dsize":4096,"mtime":1629333600}]]
`))
dir, err := ReadAnalysis(buff)
assert.Nil(t, err)
assert.Equal(t, "xxx", dir.GetName())
assert.Equal(t, "/home/xxx", dir.GetPath())
assert.Equal(t, 2021, dir.GetMtime().Year())
assert.Equal(t, 2021, dir.Files[3].GetMtime().Year())
alt2 := dir.Files[2].(*analyze.Dir).Files[2].(*analyze.File)
assert.Equal(t, "app_linux_test2.go", alt2.Name)
assert.Equal(t, uint64(1234), alt2.Mli)
assert.Equal(t, 'H', alt2.Flag)
}
func TestReadAnalysisWithEmptyInput(t *testing.T) {
buff := bytes.NewBuffer([]byte(``))
_, err := ReadAnalysis(buff)
assert.Equal(t, "unexpected end of JSON input", err.Error())
}
func TestReadAnalysisWithEmptyDict(t *testing.T) {
buff := bytes.NewBuffer([]byte(`{}`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "JSON file does not contain top level array", err.Error())
}
func TestReadFromBrokenInput(t *testing.T) {
_, err := ReadAnalysis(&BrokenInput{})
assert.Equal(t, "IO error", err.Error())
}
func TestReadAnalysisWithEmptyArray(t *testing.T) {
buff := bytes.NewBuffer([]byte(`[]`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "Top level array must have at least 4 items", err.Error())
}
func TestReadAnalysisWithWrongContent(t *testing.T) {
buff := bytes.NewBuffer([]byte(`[1,2,3,4]`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "Array of maps not found in the top level array on 4th position", err.Error())
}
func TestReadAnalysisWithEmptyDirContent(t *testing.T) {
buff := bytes.NewBuffer([]byte(`[1,2,3,[{}]]`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "Directory name is not a string", err.Error())
}
func TestReadAnalysisWithWrongDirItem(t *testing.T) {
buff := bytes.NewBuffer([]byte(`[1,2,3,[1, 2, 3]]`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "Directory item is not a map", err.Error())
}
func TestReadAnalysisWithWrongSubdirItem(t *testing.T) {
buff := bytes.NewBuffer([]byte(`[1,2,3,[{"name":"xxx"}, [1,2,3]]]`))
_, err := ReadAnalysis(buff)
assert.Equal(t, "Directory item is not a map", err.Error())
}
type BrokenInput struct{}
func (i *BrokenInput) Read(p []byte) (n int, err error) {
return 0, errors.New("IO error")
}