Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cli args usage #69

Merged
merged 6 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"scripts": {
"test": "tap test/*.js",
"test:end2end": "npm test && tap --no-check-coverage test/end-to-end/*.js",
"test:end2end": "npm test && tap --no-check-coverage -t 0 test/end-to-end/*.js",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"prepack": "npm shrinkwrap",
Expand Down
7 changes: 4 additions & 3 deletions pino-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function cli () {
.option('-e, --errors', 'output insertion errors into stderr', false)
.parse(process.argv)

const cliOptions = program.opts()
const mongoUrl = (program.args[0] || transport.defaultOption.uri)

function handleConnection (e, mClient) {
Expand All @@ -38,14 +39,14 @@ function cli () {

const db = mClient.db(dbName)
const emitter = carrier.carry(process.stdin)
const collection = db.collection(program.collection)
const insert = makeInsert(program.errors, program.stdout)
const collection = db.collection(cliOptions.collection)
const insert = makeInsert(cliOptions.errors, cliOptions.stdout)

emitter.on('line', (line) => {
insert(collection, log(line))
})

process.on('SIGINT', () => {
process.once('SIGINT', () => {
mClient.close(process.exit)
})
}
Expand Down
46 changes: 46 additions & 0 deletions test/end-to-end/pipe-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const t = require('tap')
const { spawn } = require('child_process')
const { promisify } = require('util')
const { MongoClient } = require('mongodb')
const { once } = require('events')

const mongoUrl = 'mongodb://one:two@localhost:27017/newdb?authSource=admin'
const setTimeout = promisify(global.setTimeout)

t.test('must log to a custom collection', async (t) => {
const customCollection = 'custom-collection'
const childProcess = spawn('node', [
'../../pino-mongodb.js',
mongoUrl,
'-c',
customCollection
], {
cwd: __dirname,
stdio: ['pipe', null, null]
})

const client = new MongoClient(mongoUrl)
await client.connect()
t.teardown(client.close.bind(client))
const db = client.db()
const collection = db.collection(customCollection)

const rowsBefore = await collection.countDocuments()
t.pass(`rows count ${rowsBefore}`)

childProcess.stdin.write('hello pino-mongo 1\n')
childProcess.stdin.write(`${JSON.stringify({ hello: 'pino' })}\n`)
childProcess.stdin.write('hello pino-mongo 2\n')

await setTimeout(5000)
childProcess.kill('SIGINT')
try {
await once(childProcess, 'close')
const rowsAfter = await collection.countDocuments()
t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows')
} catch (error) {
t.error(error)
}
})