-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli_test.go
451 lines (381 loc) · 11.6 KB
/
cli_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"testing"
"time"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
const (
logPrefix = "./logcat/test/logcat."
)
func newCli() *CLI {
inStream, outStream, errStream := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer)
return &CLI{
inStream: inStream,
outStream: outStream,
errStream: errStream,
}
}
func TestRun_formatDefault(t *testing.T) {
fp, err := os.Open(logPrefix + "threadtime.txt")
if err != nil {
t.Errorf("os.Open: %v", err)
}
defer fp.Close()
cli := newCli()
cli.inStream = bufio.NewReader(fp)
args := strings.Split("./logcatf", " ")
//args := []string{"./logcatf", "-o", "SourceFile:135", "-c", "sleep 0.3; say hello"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
}
func TestRun_formatEscapedCharactor(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 930 I my_app : message")
out := new(bytes.Buffer)
cli.outStream = out
args := strings.Split(`./logcatf %time\t%tag\t%priority\n%message`, " ")
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
expect := "12-28 18:54:07.180\tmy_app\tI\nmessage\n"
str := out.String()
if str != expect {
t.Errorf("expected \"%s\" to eq \"%s\"", str, expect)
}
}
func TestRun_formatError_UnavailableKeyword(t *testing.T) {
err := new(bytes.Buffer)
cli := newCli()
cli.errStream = err
args := strings.Split(`./logcatf "%time\t%level"`, " ")
status := cli.Run(args)
if status != ExitCodeError {
t.Errorf("expected %d to eq %d", status, ExitCodeError)
}
expect := fmt.Sprintf(Message["msgUnavailableKeyword"], "%level")
str := err.String()
if !strings.Contains(str, expect) {
t.Errorf("expected \"%s\" to eq \"%s\"", str, expect)
}
}
func TestRun_formatError_DuplicatedKeyword(t *testing.T) {
err := new(bytes.Buffer)
cli := newCli()
cli.errStream = err
args := strings.Split(`./logcatf "%t\t%p\t%t\t%priority"`, " ")
status := cli.Run(args)
if status != ExitCodeError {
t.Errorf("expected %d to eq %d", status, ExitCodeError)
}
expect1 := fmt.Sprintf(Message["msgDuplicatedKeyword"], "%time", "%t")
expect2 := fmt.Sprintf(Message["msgDuplicatedKeyword"], "%priority", "%p")
str := err.String()
if !strings.Contains(str, expect1) {
t.Errorf("expected \"%s\" contains \"%s\"", str, expect1)
}
if !strings.Contains(str, expect2) {
t.Errorf("expected \"%s\" contains \"%s\"", str, expect2)
}
}
func TestRun_parameterError_exec_mismatch(t *testing.T) {
err := new(bytes.Buffer)
cli := newCli()
cli.errStream = err
args := []string{"./logcatf",
"-o", "my_app.*first", "-c", "echo 1st $message",
"-o", "my_app.*third",
}
status := cli.Run(args)
if status != ExitCodeError {
t.Errorf("expected %d to eq %d", status, ExitCodeError)
}
expect := fmt.Sprintf(Message["msgmsgCommandNumMismatch"])
str := err.String()
if !strings.Contains(str, expect) {
t.Errorf("expect: \"%s\"\nresult: \"%s\"", expect, str)
}
}
func TestRun_formatShort(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
out := new(bytes.Buffer)
cli.outStream = out
args := []string{"./logcatf", "%t %i %I %p %a: %m"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
expect := "12-28 18:54:07.180 930 931 I my_app: message\n"
str := out.String()
if str != expect {
t.Errorf("expected \"%s\" to eq \"%s\"", str, expect)
}
}
func TestRun_toCsv_default(t *testing.T) {
input := "00-00 00:00:00.000 930 931 I my_app : hello\n" +
"10-00 00:00:00.000 930 931 I my_app : world.\n"
expect := "00-00 00:00:00.000,I,my_app,930,931,hello\n" +
"10-00 00:00:00.000,I,my_app,930,931,world.\n"
args := []string{"./logcatf", "--to-csv"}
cli := newCli()
cli.inStream = strings.NewReader(input)
out := new(bytes.Buffer)
cli.outStream = out
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
res := out.String()
if res != expect {
t.Errorf("\nexpect: %s\nresult: %s", expect, res)
}
}
func TestRun_toCsv(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : hello, world.")
out := new(bytes.Buffer)
cli.outStream = out
args := []string{"./logcatf", "%t %m", "--to-csv"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
expect := "12-28 18:54:07.180,\"hello, world.\""
str := out.String()
if !strings.Contains(str, expect) {
t.Errorf("\nexpect: %s\nresult: %s", expect, str)
}
}
func TestRun_execCommand(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
out := new(bytes.Buffer)
cli.errStream = out
args := []string{"./logcatf", "%t %i %I %p %a: %m", "-o", "my_app.*message", "-c", "echo test"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
<-time.After(time.Second / 100)
expect := "test"
str := out.String()
if !strings.Contains(str, expect) {
t.Errorf("expected \"%s\" to eq \"%s\"", str, expect)
}
}
func TestRun_execCommand_async(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 0 1 I tag: first line\n12-28 18:54:07.180 0 1 I tag: second line")
out := new(bytes.Buffer)
cli.outStream = out
args := []string{"./logcatf", "%t %i %I %p %a: %m", "-o", "first line", "-c", "sleep 0.2; echo finish"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
<-time.After(time.Second / 10)
expect := "second line"
if !strings.Contains(out.String(), expect) {
t.Error("command did not works asynchronously")
}
}
func TestRun_execCommand_outputParsed(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
err := new(bytes.Buffer)
cli.errStream = err
args := []string{"./logcatf", "-o", "my_app.*message", "-c", "echo parsed: $time"}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
<-time.After(time.Second / 10)
expect := "parsed: 12-28 18:54:07.180"
str := err.String()
if !strings.Contains(str, expect) {
t.Errorf("$time was not expanded.")
}
}
func TestRun_execCommand_multiple(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("" +
"12-28 18:54:07.180 930 931 I my_app : first\n" +
"12-28 18:54:07.180 930 931 I my_app : second\n" +
"12-28 18:54:07.180 930 931 I my_app : third\n")
//err := new(bytes.Buffer)
// TODO couldn't test this.
// checked that exec works correctly, using os.Stdout.
//cli.errStream = err
//cli.errStream = os.Stdout
args := []string{"./logcatf",
"-o", "my_app.*first", "-c", "echo 1st $message",
"-o", "my_app.*third", "-c", "echo 3rd $message",
}
status := cli.Run(args)
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
}
// <-time.After(time.Second / 10)
// expect := "1st first_line\n2nd second_line"
// str := err.String()
// if !strings.Contains(str, expect) {
// t.Errorf("\nresult: %s\nexpect: %s", err.String(), expect)
// }
}
func TestRun_encode_shiftjis(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 0 1 I logcat_messages:テスト")
out := new(bytes.Buffer)
cli.outStream = out
args := strings.Split(`./logcatf "%m" --encode shift-jis`, " ")
status := cli.Run(args)
if status == ExitCodeError {
t.Errorf("expected %d to eq %d", status, ExitCodeError)
}
str := out.String()
expect := toShiftJis("logcat_messages:テスト")
if !strings.Contains(str, expect) {
t.Errorf("expected \"%s\" to eq \"%s\"", str, expect)
}
}
func toShiftJis(str string) string {
buf := new(bytes.Buffer)
w := transform.NewWriter(buf, japanese.ShiftJIS.NewEncoder())
w.Write([]byte(str))
return buf.String()
}
func TestRun_color_priority(t *testing.T) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : logcat_messages:テスト")
out := new(bytes.Buffer)
cli.outStream = out
args := []string{"./logcatf", "%p [bold]%m", "--color", "--color-i", "blue"}
status := cli.Run(args)
if status == ExitCodeError {
t.Errorf("expected %d to eq %d", status, ExitCodeError)
}
expect := "\033[34mI \033[1mlogcat_messages:テスト"
str := out.String()
if !strings.Contains(str, expect) {
t.Errorf("expect: \"%s\"\nresult: \"%s\"", expect, str)
}
}
func BenchmarkDefault(b *testing.B) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
args := []string{"./logcatf", "%t %i %I %p %a: %m"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.Run(args)
}
}
func BenchmarkDefault_initialize(b *testing.B) {
cli := newCli()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.initialize([]string{"./logcatf", "%t %i %I %p %a: %m", "-o=\".\"", "-c=\"echo test\""})
}
}
func BenchmarkDefault_initialize_colorizer(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmtc = Colorizer{}
config := ColorConfig{
"V": "[red]",
"D": "[red]",
"I": "[red]",
"W": "[red]",
"E": "[red]",
"F": "[red]",
}
fmtc.Init(true, config)
}
}
func BenchmarkDefault_initialize_formatter(b *testing.B) {
cli := newCli()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.initFormatter(false, "%t %i %I %p %a: %m")
}
}
func BenchmarkDefault_initialize_writer(b *testing.B) {
cli := newCli()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.initWriter(false, "")
}
}
func BenchmarkDefault_parseLine(b *testing.B) {
cli := newCli()
line := "12-28 18:54:07.180 930 931 I my_app : message"
cli.initialize([]string{"./logcatf", "%t %i %I %p %a: %m"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.parseLine(line)
}
}
func BenchmarkDefault_execute(b *testing.B) {
cli := newCli()
line := "12-28 18:54:07.180 930 931 I my_app : message"
cli.initialize([]string{"./logcatf", "%t %i %I %p %a: %m", "-o=\".\"", "-c=\"echo test\""})
item := cli.parseLine(line)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.execute(line, item)
}
}
func BenchmarkDefault_execute_empty(b *testing.B) {
cli := newCli()
line := "12-28 18:54:07.180 930 931 I my_app : message"
cli.initialize([]string{"./logcatf", "%t %i %I %p %a: %m"})
item := cli.parseLine(line)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.execute(line, item)
}
}
func BenchmarkEncodeShiftJis(b *testing.B) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
args := []string{"./logcatf", "%t %i %I %p %a: %m", "--encode", "shift-jis"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.Run(args)
}
}
func BenchmarkToCsv(b *testing.B) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
args := []string{"./logcatf", "%t %i %I %p %a: %m", "--to-csv"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.Run(args)
}
}
func BenchmarkExecCommand(b *testing.B) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
args := []string{"./logcatf", "-o", "my_app.*message", "-c", "echo test"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.Run(args)
}
}
func BenchmarkColor(b *testing.B) {
cli := newCli()
cli.inStream = strings.NewReader("12-28 18:54:07.180 930 931 I my_app : message")
args := []string{"./logcatf", "--color"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cli.Run(args)
}
}