-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
244 lines (194 loc) · 6.45 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
)
var (
command = getCommand()
testPath = getCurrentDir()
)
func getCommand() []string {
if envCmd := os.Getenv("MEMSPARKLINE_COMMAND"); envCmd != "" {
return strings.Fields(envCmd)
}
return []string{"./memsparkline"}
}
func getCurrentDir() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Dir(filename)
}
func runMemsparkline(t *testing.T, args ...string) (string, string, error) {
// Start with command args, if any, then add the test-specific args.
allArgs := append([]string{}, command[1:]...)
allArgs = append(allArgs, args...)
cmd := exec.Command(command[0], allArgs...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
func getSleepCommand(duration float64) []string {
return []string{"test/sleep", strconv.FormatFloat(duration, 'f', -1, 64)}
}
func TestUsage(t *testing.T) {
_, stderr, _ := runMemsparkline(t)
if matched, _ := regexp.MatchString("^Usage", stderr); !matched {
t.Error("Expected usage information in stderr")
}
}
func TestVersion(t *testing.T) {
stdout, _, _ := runMemsparkline(t, "-v")
if matched, _ := regexp.MatchString(`\d+\.\d+\.\d+`, stdout); !matched {
t.Error("Expected version number in stdout")
}
}
func TestUnknownOptBeforeHelp(t *testing.T) {
_, _, err := runMemsparkline(t, "--foo", "--help")
if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 2 {
t.Errorf("Expected exit status 2, got %v", err)
}
}
func TestUnknownOptAfterHelp(t *testing.T) {
_, _, err := runMemsparkline(t, "--help", "--foo")
if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 2 {
t.Errorf("Expected exit status 2, got %v", err)
}
}
func TestBasic(t *testing.T) {
args := getSleepCommand(0.5)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?s).*avg:.*max:`, stderr); !matched {
t.Error("Expected 'avg:' and 'max:' in output")
}
}
func TestEndOfOptions(t *testing.T) {
args := append([]string{"--"}, getSleepCommand(0.1)...)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?s).*avg:.*max:`, stderr); !matched {
t.Error("Expected 'avg:' and 'max:' in output")
}
}
func TestEndOfOptionsHelp(t *testing.T) {
args := append([]string{"--"}, getSleepCommand(0.1)...)
args = append(args, "-h")
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?s).*avg:.*max:`, stderr); !matched {
t.Error("Expected 'avg:' and 'max:' in output")
}
}
func TestLength(t *testing.T) {
args := append([]string{"-l", "5", "-w", "10"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?m)\r[^ ]{5} \d+\.\d\r?\n avg`, stderr); !matched {
t.Error("Expected sparkline of specific length followed by summary")
}
}
func TestMemFormat(t *testing.T) {
args := append([]string{"-l", "5", "-w", "10", "-m", "%0.2f"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?m)\r[^ ]{5} \d+\.\d{2}\r?\n avg`, stderr); !matched {
t.Error("Expected sparkline with memory format with two decimal places")
}
}
func TestTimeFormat(t *testing.T) {
args := append([]string{"-l", "10", "-t", "%d:%05d:%06.3f"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString(`(?m)time: \d+:\d{5}:\d{2}\.\d{3}\r?\n`, stderr); !matched {
t.Error("Expected specific time format in summary")
}
}
func TestWait1(t *testing.T) {
args := append([]string{"-w", "2000"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if lines := strings.Count(stderr, "\n"); lines != 4 {
t.Errorf("Expected 4 lines in output, got %d", lines)
}
}
func TestWait2(t *testing.T) {
args := append([]string{"-n", "-w", "10"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if lines := strings.Count(stderr, "\n"); lines < 9 {
t.Errorf("Expected at least 9 lines in output, got %d", lines)
}
}
func TestSampleAndRecord(t *testing.T) {
args := append([]string{"-r", "500", "-s", "100"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if lines := strings.Count(stderr, "\n"); lines != 4 {
t.Errorf("Expected 4 lines in output, got %d", lines)
}
}
func TestQuiet(t *testing.T) {
args := append([]string{"-q"}, getSleepCommand(0.5)...)
_, stderr, _ := runMemsparkline(t, args...)
if matched, _ := regexp.MatchString("^ avg", stderr); !matched {
t.Error("Expected output to start with 'avg' in quiet mode")
}
}
func TestMissingBinary(t *testing.T) {
_, stderr, err := runMemsparkline(t, "no-such-binary-exists")
if err == nil {
t.Error("Expected error for missing binary")
}
if !strings.Contains(stderr, "failed to start command") {
t.Error("Expected 'failed to start command' in stderr")
}
}
func TestDoubleDash(t *testing.T) {
stdout, _, _ := runMemsparkline(t, "--", "ls", "-l")
if !strings.Contains(stdout, "\n") {
t.Error("Expected newline in output")
}
}
func TestTwoDoubleDashes(t *testing.T) {
args := append(command, "--", "ls", "-l")
stdout, _, _ := runMemsparkline(t, append([]string{"--"}, args...)...)
if !strings.Contains(stdout, "\n") {
t.Error("Expected newline in output")
}
}
func TestDump(t *testing.T) {
dumpPath := filepath.Join(testPath, "dump.log")
// Clean up any existing file so we don't append to it.
os.Remove(dumpPath)
args := append([]string{"-q", "-w", "100", "-d", dumpPath}, getSleepCommand(0.5)...)
runMemsparkline(t, args...)
content, err := os.ReadFile(dumpPath)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(string(content)), "\n")
if len(lines) == 0 {
t.Error("Expected non-empty dump file")
}
for _, line := range lines {
if matched, _ := regexp.MatchString(`\d+ \d+`, line); !matched {
t.Errorf("Invalid line format: %s", line)
}
}
}
func TestOutput(t *testing.T) {
outputPath := filepath.Join(testPath, "output.log")
// Clean up any existing file so we don't append to it.
os.Remove(outputPath)
args := append([]string{"-q", "-o", outputPath}, getSleepCommand(0.5)...)
for i := 0; i < 2; i++ {
runMemsparkline(t, args...)
}
content, err := os.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(string(content), "\n")
if len(lines) != 7 {
t.Errorf("Expected 7 lines in output file, got %d", len(lines))
}
}