UNPKG

2.53 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 child = spawn(process.argv0, [bin])
15 child.on('error', t.threw)
16 child.stdout.on('data', (data) => {
17 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
18 })
19 child.stdin.write(logLine)
20 t.tearDown(() => child.kill())
21 })
22
23 t.test('flips epoch and level', (t) => {
24 t.plan(1)
25 const child = spawn(process.argv0, [bin, '-l'])
26 child.on('error', t.threw)
27 child.stdout.on('data', (data) => {
28 t.is(data.toString(), `INFO [${epoch}] (42 on foo): hello world\n`)
29 })
30 child.stdin.write(logLine)
31 t.tearDown(() => child.kill())
32 })
33
34 t.test('translates time to default format', (t) => {
35 t.plan(1)
36 const child = spawn(process.argv0, [bin, '-t'])
37 child.on('error', t.threw)
38 child.stdout.on('data', (data) => {
39 t.is(data.toString(), `[2018-03-30 17:35:28.992 +0000] INFO (42 on foo): hello world\n`)
40 })
41 child.stdin.write(logLine)
42 t.tearDown(() => child.kill())
43 })
44
45 t.test('does search', (t) => {
46 t.plan(1)
47 const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'])
48 child.on('error', t.threw)
49 child.stdout.on('data', (data) => {
50 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
51 })
52 child.stdin.write(logLine)
53 t.tearDown(() => child.kill())
54 })
55
56 t.test('does search but finds only 1 out of 2', (t) => {
57 t.plan(1)
58 const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'])
59 child.on('error', t.threw)
60 child.stdout.on('data', (data) => {
61 t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
62 })
63 child.stdin.write(logLine.replace('hello world', 'hello universe'))
64 child.stdin.write(logLine)
65 t.tearDown(() => child.kill())
66 })
67
68 t.test('does ignore multiple keys', (t) => {
69 t.plan(1)
70 const child = spawn(process.argv0, [bin, '-i', 'pid,hostname'])
71 child.on('error', t.threw)
72 child.stdout.on('data', (data) => {
73 t.is(data.toString(), `[1522431328992] INFO : hello world\n`)
74 })
75 child.stdin.write(logLine)
76 t.tearDown(() => child.kill())
77 })
78
79 t.end()
80})