From 6857661b22942b61e105a13414c07b370c205e92 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Mon, 2 Feb 2015 15:41:06 +0000 Subject: [PATCH] many: Added test, fixed an error --- lib/spserver.js | 13 +++++++++---- package.json | 8 ++++++-- test/base.test.js | 34 ++++++++++++++++++++++++++++++++++ test/nothing.js | 3 +++ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 test/base.test.js create mode 100644 test/nothing.js diff --git a/lib/spserver.js b/lib/spserver.js index aea9ffb..7326180 100644 --- a/lib/spserver.js +++ b/lib/spserver.js @@ -4,6 +4,7 @@ var fs = require('fs'); var http = require('http'); var _ = require('lodash'); var nStatic = require('node-static'); +var root = require('app-root-path'); var config = require('./config'); @@ -12,7 +13,7 @@ var logger = require('./logger'); var env = config.get('NODE_ENV'); var fileServer = new nStatic.Server(config.get('serve') || config.get(env + ':serve')); -module.exports = function(settings) { +var spserver = function(settings) { if (!settings) { settings = config.get(); } @@ -58,18 +59,20 @@ module.exports = function(settings) { settings.serve || settings[env].serve); }; +spserver.generateBase = generateBase; + function generateBase(file, settings) { if (!file) { return null; } if (_.endsWith(file, 'js')) { - return require(file); + return require(root.resolve(file)); } - var contents = fs.readFileSync(file); + var contents = fs.readFileSync(root.resolve(file)); - if (settings.template || settings[env].template) { + if (settings.template || settings[env] && settings[env].template) { contents = _.template(contents)(settings); } @@ -78,3 +81,5 @@ function generateBase(file, settings) { res.end(contents); }; } + +module.exports = spserver; diff --git a/package.json b/package.json index 477e8c0..31d2a43 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Node static page server for running quick MVVM file server", "main": "lib/spserver.js", "scripts": { - "test": "mocha --reporter spec ./test/*.test.js" + "test": "mocha --reporter spec test/base.test.js" }, "repository": { "type": "git", @@ -21,10 +21,14 @@ }, "homepage": "https://github.com/TheThing/spserver", "dependencies": { + "app-root-path": "^1.0.0", "bunyan": "^1.3.3", "lodash": "^3.0.1", "nconf": "^0.7.1", "node-static": "^0.7.6" }, - "bin": "./bin.js" + "bin": "./bin.js", + "devDependencies": { + "sinon": "^1.12.2" + } } diff --git a/test/base.test.js b/test/base.test.js new file mode 100644 index 0000000..4e8702e --- /dev/null +++ b/test/base.test.js @@ -0,0 +1,34 @@ +'use strict'; + +var fs = require('fs'); +var assert = require('assert'); +var sinon = require('sinon'); + +describe('spserver', function() { + + var spserver = require('../lib/spserver'); + + describe('#generateBase()', function() { + it('should return null when file is empty', function() { + assert.strictEqual(null, spserver.generateBase()); + assert.strictEqual(null, spserver.generateBase(null, {})); + assert.strictEqual(null, spserver.generateBase('')); + assert.strictEqual(null, spserver.generateBase('', {})); + }); + + it('should read file contents if string', function() { + var stub = sinon.stub(fs, 'readFileSync').returns('bla'); + + spserver.generateBase('asdf', {}); + assert(stub.called); + stub.restore(); + }); + + it('should return function if file is javascript', function() { + var nothing = require('./nothing'); + var test = spserver.generateBase('test/nothing.js', {}); + + assert.strictEqual(nothing.toString(), test.toString()); + }); + }); +}); diff --git a/test/nothing.js b/test/nothing.js new file mode 100644 index 0000000..d5c6bc1 --- /dev/null +++ b/test/nothing.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = function() {};