forked from dundee/gdu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_test.go
116 lines (89 loc) · 2.99 KB
/
export_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
package report
import (
"bytes"
"os"
"testing"
log "github.com/sirupsen/logrus"
"github.com/dundee/gdu/v5/internal/testdir"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/stretchr/testify/assert"
)
func init() {
log.SetLevel(log.WarnLevel)
}
func TestAnalyzePath(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
output := bytes.NewBuffer(make([]byte, 10))
reportOutput := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, false, false)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
err = ui.StartUILoop()
assert.Nil(t, err)
assert.Contains(t, reportOutput.String(), `"name":"nested"`)
}
func TestAnalyzePathWithProgress(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
output := bytes.NewBuffer(make([]byte, 10))
reportOutput := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, true, true)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err := ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
err = ui.StartUILoop()
assert.Nil(t, err)
assert.Contains(t, reportOutput.String(), `"name":"nested"`)
}
func TestShowDevices(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
reportOutput := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, false, true)
err := ui.ListDevices(device.Getter)
assert.Contains(t, err.Error(), "not supported")
}
func TestReadAnalysisWhileExporting(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
reportOutput := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, false, true)
err := ui.ReadAnalysis(output)
assert.Contains(t, err.Error(), "not possible while exporting")
}
func TestExportToFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()
reportOutput, err := os.OpenFile("output.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
assert.Nil(t, err)
defer func() {
os.Remove("output.json")
}()
output := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, false, true)
ui.SetIgnoreDirPaths([]string{"/xxx"})
err = ui.AnalyzePath("test_dir", nil)
assert.Nil(t, err)
err = ui.StartUILoop()
assert.Nil(t, err)
reportOutput, err = os.OpenFile("output.json", os.O_RDONLY, 0644)
assert.Nil(t, err)
_, err = reportOutput.Seek(0, 0)
assert.Nil(t, err)
buff := make([]byte, 200)
_, err = reportOutput.Read(buff)
assert.Nil(t, err)
assert.Contains(t, string(buff), `"name":"nested"`)
}
func TestFormatSize(t *testing.T) {
output := bytes.NewBuffer(make([]byte, 10))
reportOutput := bytes.NewBuffer(make([]byte, 10))
ui := CreateExportUI(output, reportOutput, false, true)
assert.Contains(t, ui.formatSize(1), "B")
assert.Contains(t, ui.formatSize(1<<10+1), "KiB")
assert.Contains(t, ui.formatSize(1<<20+1), "MiB")
assert.Contains(t, ui.formatSize(1<<30+1), "GiB")
assert.Contains(t, ui.formatSize(1<<40+1), "TiB")
assert.Contains(t, ui.formatSize(1<<50+1), "PiB")
assert.Contains(t, ui.formatSize(1<<60+1), "EiB")
}