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

Avoid to rewrite the error #19

Merged
merged 4 commits into from
Oct 22, 2017
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
21 changes: 12 additions & 9 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,33 @@ function start (opts) {

if (opts.help) {
console.log(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8'))
stop(0)
return module.exports.stop()
}

assert(opts._.length === 1, 'Missing the file parameter')

return runFastify(opts)
}

function stop (code) {
process.exit(code)
function stop (error) {
if (error) {
console.log(error)
process.exit(1)
}
process.exit()
}

function runFastify (opts) {
var file = null
try {
file = require(path.resolve(process.cwd(), opts._[0]))
} catch (e) {
console.log(`Cannot find the specified file: '${opts._[0]}'`)
stop(1)
return module.exports.stop(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I can ovewrite it during tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like so much as solution, is not elegant. But for the moment could be ok. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too. But I have not sadly found another solution!

}

if (file.length !== 3) {
throw new Error('Plugin function should contain 3 arguments. Refer to ' +
'documentation for more information.')
return module.exports.stop(new Error('Plugin function should contain 3 arguments. Refer to ' +
'documentation for more information.'))
}

const options = {
Expand Down Expand Up @@ -117,8 +120,8 @@ function cli () {
}))
}

module.exports = { start, stop, runFastify, cli }

if (require.main === module) {
cli()
}

module.exports = { start, stop, runFastify, cli }
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"engines": {
"node": ">= 4.0"
},
"standard": {
"ignore": [
"test_data/parsing-error.js"
]
},
"dependencies": {
"minimist": "^1.2.0",
"pino-colada": "^1.4.0",
Expand Down
77 changes: 69 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,74 @@ test('should start fastify at given socket path', t => {
test('should only accept plugin functions with 3 arguments', t => {
t.plan(1)

try {
cli.start({
port: 3000,
_: ['./examples/incorrect-plugin.js']
})
t.fail('Incorrect arguments')
} catch (e) {
t.pass('Incorrect arguments')
const oldStop = cli.stop
t.tearDown(() => { cli.stop = oldStop })
cli.stop = function (err) {
t.equal(err.message, 'Plugin function should contain 3 arguments. Refer to documentation for more information.')
}

cli.start({
port: 3000,
_: ['./test_data/incorrect-plugin.js']
})
})

test('should throw on file not found', t => {
t.plan(1)

const oldStop = cli.stop
t.tearDown(() => { cli.stop = oldStop })
cli.stop = function (err) {
t.ok(/Cannot find module.*not-found/.test(err.message), err.message)
}

cli.start({
port: 3000,
_: ['./test_data/not-found.js']
})
})

test('should throw on package not found', t => {
t.plan(1)

const oldStop = cli.stop
t.tearDown(() => { cli.stop = oldStop })
cli.stop = function (err) {
t.ok(/Cannot find module.*unknown-package/.test(err.message), err.message)
}

cli.start({
port: 3000,
_: ['./test_data/package-not-found.js']
})
})

test('should throw on parsing error', t => {
t.plan(1)

const oldStop = cli.stop
t.tearDown(() => { cli.stop = oldStop })
cli.stop = function (err) {
t.equal(err.constructor, SyntaxError)
}

cli.start({
port: 3000,
_: ['./test_data/parsing-error.js']
})
})

test('should exit without error on help', t => {
t.plan(1)

const oldStop = cli.stop
t.tearDown(() => { cli.stop = oldStop })
cli.stop = function (err) {
t.equal(err, undefined)
}

cli.start({
port: 3000,
help: true
})
})
File renamed without changes.
3 changes: 3 additions & 0 deletions test_data/package-not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

require('unknown-package')
1 change: 1 addition & 0 deletions test_data/parsing-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'