-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
192 lines (153 loc) · 4.53 KB
/
main_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
package main
import (
"github.com/spectronp/sizr/data"
"github.com/spectronp/sizr/tests"
"github.com/spectronp/sizr/types"
"github.com/spectronp/sizr/utils"
"github.com/spectronp/sizr/vars"
"fmt"
"io"
"log"
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
var mockData data.Data
func TestMain(m *testing.M) {
VERSION = "v0.1"
vars.BASEDIR = os.Getenv("BASEDIR")
helpBytes, err := os.ReadFile("help.txt")
if err != nil {
log.Println("Error on reading help.txt")
panic(err)
}
HELP_MESSAGE = string(helpBytes)
vars.DB_FILE = "/tmp/sizr_db"
defer os.Remove("/tmp/sizr_db")
if err := utils.SaveJson(map[string]any{}, vars.DB_FILE); err != nil {
panic(err)
}
mockData, _ = data.NewData(helpers.MockRunner) // NOTE: if Data is broken, this data will break this tests too, maybe use mockgen ? ( data is used more along the file )
m.Run()
}
func TestListTree(t *testing.T) {
firstPack := types.Package{
Name: "exp3",
IsExplicit: true,
Size: 10240,
Version: "0.0.0",
Deps: []string{"dep11", "dep12"},
}
depsToIgnore := map[string]bool{
"dep13": true,
}
expectedPackages := map[string]bool{"dep13": true, "dep11": true, "dep12": true, "dep15": true, "rev2": true, "rev3": true, "dep5": true, "dep6": true, "dep9": true, "dep10": true}
listTree(firstPack, depsToIgnore, &mockData)
if !cmp.Equal(expectedPackages, depsToIgnore) {
fmt.Println(cmp.Diff(expectedPackages, depsToIgnore))
t.Error("listTree function returned something different from expected")
}
}
func TestSumSize(t *testing.T) {
start := mockData.GetPackage("exp1")
ignorePackagesNames := []string{"rev1", "rev2", "rev3", "rev4"}
ignoredPackages := make(map[string]bool)
for _, ignoredName := range ignorePackagesNames {
ignoredPackages[ignoredName] = true
}
alreadyCounted := map[string]bool{}
expectedSize := uint(51200)
actualSize := sumSize(start, ignoredPackages, alreadyCounted, &mockData)
if expectedSize != actualSize {
t.Errorf("Expected size %d, got size %d", expectedSize, actualSize)
}
}
func TestCalcSize(t *testing.T) {
expectedSize := 51200
actualSize := calcSize("exp1", &mockData)
if actualSize != uint(expectedSize) {
t.Errorf("Expected size %d, got size %d", expectedSize, actualSize)
}
}
func TestOrderBySum(t *testing.T) {
expectedPackages := []PackageNameWithSum{
{Name: "exp1", Size: 51200},
{Name: "exp3", Size: 40960},
{Name: "exp2", Size: 20480},
}
actualPackages := orderBySumSize(&mockData)
if !cmp.Equal(expectedPackages, actualPackages) {
fmt.Println(cmp.Diff(expectedPackages, actualPackages))
t.Errorf("The received packages are different from expected")
}
}
// E2E Tests
func runApp(args []string) (int, string) {
args = append([]string{"sizr"}, args...)
pipeReader, pipeWriter, err := os.Pipe()
if err != nil {
panic(err)
}
stdOut := os.Stdout
stdErr := os.Stderr
os.Stdout = pipeWriter
os.Stderr = pipeWriter
var returnCode int
go func() {
returnCode = Run(args)
pipeWriter.Close()
}()
output, err := io.ReadAll(pipeReader)
if err != nil {
panic("Panic at io.ReadAll()")
}
os.Stdout = stdOut
os.Stderr = stdErr
return returnCode, string(output)
}
func TestVersionOutput(t *testing.T) {
args := []string{"--version"}
expectedOutput := fmt.Sprintf("sizr %s\n", VERSION)
returnCode, output := runApp(args)
if returnCode != 0 {
t.Errorf("Expected return code 0, got %d", returnCode)
}
if output != expectedOutput {
t.Errorf("expected output: %s, got output: %s", expectedOutput, output)
}
}
func TestHelpOutput(t *testing.T) {
args := []string{"--help"}
expectedOutput := HELP_MESSAGE
returnCode, output := runApp(args)
if returnCode != 0 {
t.Errorf("Expected return code 0, got %d", returnCode)
}
if output != expectedOutput {
fmt.Print(cmp.Diff(expectedOutput, output))
t.Error("Output is different from expected")
}
}
func TestListReport(t *testing.T) {
returnCode, output := runApp([]string{})
if returnCode != 0 {
t.Errorf("Expected return code 0, got %d", returnCode)
}
expectedOutput := "exp1 50 KiB\nexp3 40 KiB\nexp2 20 KiB\n"
if output != expectedOutput {
fmt.Print(cmp.Diff(expectedOutput, output))
t.Error("Output is different from the expected")
}
}
func TestLimitReport(t *testing.T) {
args := []string{"--limit", "2"}
expectedOutput := "exp1 50 KiB\nexp3 40 KiB\n"
returnCode, output := runApp(args)
if returnCode != 0 {
t.Errorf("Expected return code 0, got %d", returnCode)
}
if output != expectedOutput {
fmt.Print(cmp.Diff(expectedOutput, output))
t.Error("Output is different from expected")
}
}