many: Added test, fixed an error

This commit is contained in:
Jonatan Nilsson 2015-02-02 15:41:06 +00:00
parent 431d114505
commit 6857661b22
4 changed files with 52 additions and 6 deletions

View file

@ -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;

View file

@ -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"
}
}

34
test/base.test.js Normal file
View file

@ -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());
});
});
});

3
test/nothing.js Normal file
View file

@ -0,0 +1,3 @@
'use strict';
module.exports = function() {};