UNPKG

3.21 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const spawn = require('child_process').spawn
5const test = require('tap').test
6
7const bin = require.resolve(path.join(__dirname, '..', 'bin.js'))
8const epoch = 1522431328992
9const logLine = '{"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo","v":1}\n'
10
11test('cli', (t) => {
12 t.test('does basic reformatting', (t) => {
13 t.plan(1)
14 const env = { TERM: 'dumb' }
15 const child = spawn(process.argv0, [bin], { env })
16 child.on('error', t.threw)
17 child.stdout.on('data', (data) => {
18 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
19 })
20 child.stdin.write(logLine)
21 t.tearDown(() => child.kill())
22 })
23
24 t.test('flips epoch and level', (t) => {
25 t.plan(1)
26 const env = { TERM: 'dumb' }
27 const child = spawn(process.argv0, [bin, '-l'], { env })
28 child.on('error', t.threw)
29 child.stdout.on('data', (data) => {
30 t.is(data.toString(), `INFO [${epoch}] (42 on foo): hello world\n`)
31 })
32 child.stdin.write(logLine)
33 t.tearDown(() => child.kill())
34 })
35
36 t.test('translates time to default format', (t) => {
37 t.plan(1)
38 const env = { TERM: 'dumb' }
39 const child = spawn(process.argv0, [bin, '-t'], { env })
40 child.on('error', t.threw)
41 child.stdout.on('data', (data) => {
42 t.is(data.toString(), '[2018-03-30 17:35:28.992 +0000] INFO (42 on foo): hello world\n')
43 })
44 child.stdin.write(logLine)
45 t.tearDown(() => child.kill())
46 })
47
48 t.test('does search', (t) => {
49 t.plan(1)
50 const env = { TERM: 'dumb' }
51 const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'], { env })
52 child.on('error', t.threw)
53 child.stdout.on('data', (data) => {
54 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
55 })
56 child.stdin.write(logLine)
57 t.tearDown(() => child.kill())
58 })
59
60 t.test('does search but finds only 1 out of 2', (t) => {
61 t.plan(1)
62 const env = { TERM: 'dumb' }
63 const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'], { env })
64 child.on('error', t.threw)
65 child.stdout.on('data', (data) => {
66 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
67 })
68 child.stdin.write(logLine.replace('hello world', 'hello universe'))
69 child.stdin.write(logLine)
70 t.tearDown(() => child.kill())
71 })
72
73 t.test('does ignore multiple keys', (t) => {
74 t.plan(1)
75 const env = { TERM: 'dumb' }
76 const child = spawn(process.argv0, [bin, '-i', 'pid,hostname'], { env })
77 child.on('error', t.threw)
78 child.stdout.on('data', (data) => {
79 t.is(data.toString(), '[1522431328992] INFO : hello world\n')
80 })
81 child.stdin.write(logLine)
82 t.tearDown(() => child.kill())
83 })
84
85 t.test('passes through stringified date as string', (t) => {
86 t.plan(1)
87 const env = { TERM: 'dumb' }
88 const child = spawn(process.argv0, [bin], { env })
89 child.on('error', t.threw)
90
91 const date = JSON.stringify(new Date(epoch))
92
93 child.stdout.on('data', (data) => {
94 t.is(data.toString(), date + '\n')
95 })
96
97 child.stdin.write(date)
98 child.stdin.write('\n')
99
100 t.tearDown(() => child.kill())
101 })
102
103 t.end()
104})