/
cli.test.js
97 lines (83 loc) 路 2.85 KB
/
cli.test.js
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
'use strict'
const path = require('path')
const spawn = require('child_process').spawn
const test = require('tap').test
const bin = require.resolve(path.join(__dirname, '..', 'bin.js'))
const epoch = 1522431328992
const logLine = '{"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo","v":1}\n'
test('cli', (t) => {
t.test('does basic reformatting', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('flips epoch and level', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-l'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `INFO [${epoch}] (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('translates time to default format', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-t'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[2018-03-30 17:35:28.992 +0000] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('does search', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('does search but finds only 1 out of 2', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine.replace('hello world', 'hello universe'))
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('does ignore multiple keys', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-i', 'pid,hostname'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[1522431328992] INFO : hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})
t.test('passes through stringified date as string', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin])
child.on('error', t.threw)
const date = JSON.stringify(new Date(epoch))
child.stdout.on('data', (data) => {
t.is(data.toString(), date + '\n')
})
child.stdin.write(date)
child.stdin.write('\n')
t.tearDown(() => child.kill())
})
t.end()
})