From 90f3bcd891e7f0a5381fcdf02651943b10f134e1 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Thu, 5 Dec 2013 09:45:53 +0000 Subject: [PATCH] Test help --- lib/help/index.js | 22 ++++++++++++---------- lib/nodemon.js | 3 ++- test/fixtures/help.txt | 1 + test/help/help.test.js | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/help.txt create mode 100644 test/help/help.test.js diff --git a/lib/help/index.js b/lib/help/index.js index 5d98c5c..1f099f9 100644 --- a/lib/help/index.js +++ b/lib/help/index.js @@ -1,6 +1,5 @@ var fs = require('fs'), - path = require('path'), - utils = require('../utils'); + path = require('path'); module.exports = help; @@ -8,14 +7,17 @@ function help(item) { if (!item) { item = 'help'; } else if (item === true) { // if used with -h or --help and no args - item = 'help' + item = 'help'; } - fs.readFile(path.join(__dirname, '..', '..', 'doc', 'cli', item + '.txt'), 'utf8', function (err, body) { - if (err) { - return utils.log.error(item + ' help can\'t be found'); - } - console.log(body); - process.exit(0); - }); + // cleanse the filename to only contain letters + // aka: /\W/g but figured this was eaiser to read + item = item.replace(/[^a-z]/gi, ''); + + try { + var body = fs.readFileSync(path.join(__dirname, '..', '..', 'doc', 'cli', item + '.txt'), 'utf8'); + return body; + } catch (e) { + return '"' + item + '" help can\'t be found'; + } } \ No newline at end of file diff --git a/lib/nodemon.js b/lib/nodemon.js index a2ded54..b87fa27 100644 --- a/lib/nodemon.js +++ b/lib/nodemon.js @@ -32,7 +32,8 @@ function nodemon(settings) { } if (settings.help) { - return help(settings.help); + console.log(help(settings.help)); + process.exit(0); } if (settings.version) { diff --git a/test/fixtures/help.txt b/test/fixtures/help.txt new file mode 100644 index 0000000..89df9d0 --- /dev/null +++ b/test/fixtures/help.txt @@ -0,0 +1 @@ +This file should not be read. \ No newline at end of file diff --git a/test/help/help.test.js b/test/help/help.test.js new file mode 100644 index 0000000..308b0d7 --- /dev/null +++ b/test/help/help.test.js @@ -0,0 +1,20 @@ +/*global describe:true, it: true */ +var help = require('../../lib/help'), + assert = require('assert'); + +describe('help', function () { + it('should load index by default', function () { + var page = help(); + assert(page.indexOf('Usage: nodemon') !== -1, 'shows default help page'); + }); + + it('should load specific help topic', function () { + var page = help('authors'); + assert(page.indexOf('Remy Sharp') !== -1, 'shows specific topic'); + }); + + it('should not expose files', function () { + var page = help('../../test/fixtures/help'); + assert(page.indexOf('" help can\'t be found') !== -1, 'shows help cannot be found'); + }); +}); \ No newline at end of file