-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
159 lines (132 loc) Β· 3.4 KB
/
command_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
package main
import (
"bytes"
"flag"
"os"
"path/filepath"
"testing"
)
func TestCommand(t *testing.T) {
// ensure only specific pager tests execute with custom pager
t.Setenv("PAGER", "")
t.Run("from-stdin-pipe", func(t *testing.T) {
args := os.Args
t.Cleanup(func() { os.Args = args })
// override os.Args which contains flags from the test binary
os.Args = []string{
"coverdiff",
}
flag.Parse()
var stdout bytes.Buffer
stdin := bytes.NewBufferString(readFile(t, "testdata/coverage.out"))
if err := command(stdin, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "testdata/coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
t.Run("from-stdin-dash", func(t *testing.T) {
os.Args[1] = "-"
flag.Parse()
var stdout bytes.Buffer
stdin := bytes.NewBufferString(readFile(t, "testdata/coverage.out"))
if err := command(stdin, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "testdata/coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
t.Run("from-file", func(t *testing.T) {
os.Args[1] = filepath.Join(projectPath, "testdata/coverage.out")
flag.Parse()
var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "testdata/coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
t.Run("from-file-in-subdirectory", func(t *testing.T) {
if err := os.Chdir("testdata"); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.Chdir(projectPath); err != nil {
t.Fatal(err)
}
})
os.Args[1] = filepath.Join(projectPath, "testdata/coverage.out")
flag.Parse()
var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
t.Run("outside-go-module", func(t *testing.T) {
changeDir(t, "..")
var stdout bytes.Buffer
err := command(nil, &stdout)
got := err.Error()
want := "findModuleDir: no go.mod file found"
if got != want {
t.Errorf("got %s, want: %s", got, want)
}
})
t.Run("run-go-tests", func(t *testing.T) {
os.Args[1] = "test"
os.Args[2] = "./size"
flag.Parse()
changeDir(t, "testdata")
var stdout bytes.Buffer
if err := command(nil, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
t.Run("print-with-pager", func(t *testing.T) {
args := os.Args
t.Setenv("PAGER", "less")
t.Cleanup(func() { os.Args = args })
// override os.Args which contains flags from the test binary
os.Args = []string{
"coverdiff",
}
flag.Parse()
var stdout bytes.Buffer
stdin := bytes.NewBufferString(readFile(t, "testdata/coverage.out"))
if err := command(stdin, &stdout); err != nil {
t.Fatal(err)
}
got := stdout.String()
want := readFile(t, "testdata/coverdiff.out")
if got != want {
t.Errorf("got len=%d, want len=%d", len(got), len(want))
}
})
}
func readFile(tb testing.TB, name string) string {
tb.Helper()
b, err := os.ReadFile(name)
if err != nil {
tb.Fatal(err)
}
return string(b)
}