Skip to content

Commit

Permalink
added app defaults. bumped dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
noamokman committed Sep 25, 2015
1 parent e69f499 commit 900e027
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 12 deletions.
12 changes: 9 additions & 3 deletions gulp/tasks/jscs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ module.exports = function (gulp) {

gulp.task('jscs:lib', function () {
return gulp.src(config.paths.lib)
.pipe(jscs());
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});

gulp.task('jscs:test', function () {
return gulp.src(config.paths.test)
.pipe(jscs());
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});

gulp.task('jscs:gulp', function () {
return gulp.src(config.paths.gulp)
.pipe(jscs());
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});
};
73 changes: 70 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var SettingsFile = require('nachos-settings-file');
var nachosHome = require('nachos-home');
var debug = require('debug')('nachosConfig');
var util = require('util');
var Q = require('q');

/**
* Creating a settings file with nachos defaults
Expand All @@ -13,11 +14,13 @@ var util = require('util');
function NachosConfig() {
var defaults = {
packages: nachosHome('packages'),
server: 'http://nachosjs.herokuapp.com'
server: 'http://nachosjs.herokuapp.com',
defaults: {
exts: {}
}
};

debug('creating new settings file with these defaults');
debug(defaults);
debug('creating new settings file with these defaults: %j', defaults);

SettingsFile.call(this, 'nachos', {
globalDefaults: defaults
Expand All @@ -26,6 +29,70 @@ function NachosConfig() {

util.inherits(NachosConfig, SettingsFile);

/**
* Expose the constructor
* @type {NachosConfig}
*/
NachosConfig.prototype.NachosConfig = NachosConfig;

/**
* Get the default app command that opens the given extension
*
* @param {String} ext The file extension
* @returns {Q.promise} Promise to the default app command
*/
NachosConfig.prototype.getDefaultApp = function (ext) {
if (!ext) {
return Q.reject(new TypeError('extension must be provided'));
}

if (typeof ext !== 'string') {
return Q.reject(new TypeError('extension must be a string'));
}

debug('getting command for %s extension', ext);

return this.get()
.then(function (config) {
var command = config.defaults.exts[ext];

debug('got command %s for %s extension', command, ext);

return command;
});
};

/**
* Set the default app command that opens the given extension
*
* @param {String} ext The file extension
* @param {String} command The execution command
* @returns {Q.promise} Promise to save
*/
NachosConfig.prototype.setDefaultApp = function (ext, command) {
if (!ext) {
return Q.reject(new TypeError('extension must be provided'));
}

if (typeof ext !== 'string') {
return Q.reject(new TypeError('extension must be a string'));
}

if (!command) {
return Q.reject(new TypeError('command must be provided'));
}

if (typeof command !== 'string') {
return Q.reject(new TypeError('command must be a string'));
}

debug('setting command %s for %s extension', command, ext);

var content = {defaults: {exts: {}}};

content.defaults.exts[ext] = command;

return this.set(content);
};

module.exports = new NachosConfig();
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@
},
"homepage": "https://github.com/nachos/nachos-config",
"devDependencies": {
"chai": "^3.2.0",
"git-validate": "^2.0.3",
"chai": "^3.3.0",
"chai-as-promised": "^5.1.0",
"git-validate": "^2.1.0",
"gulp": "^3.9.0",
"gulp-bump": "^0.3.1",
"gulp-bump": "^1.0.0",
"gulp-coveralls": "^0.1.4",
"gulp-istanbul": "^0.10.0",
"gulp-jscs": "^2.0.0",
"gulp-jscs": "^3.0.0",
"gulp-jshint": "^1.11.2",
"gulp-mocha": "^2.1.3",
"jscs-nachos": "^1.0.1",
"jshint-stylish": "^2.0.1",
"lazypipe": "^1.0.1",
"run-sequence": "^1.1.2"
"mockery": "^1.4.0",
"run-sequence": "^1.1.4",
"sinon": "^1.17.0",
"sinon-chai": "^2.8.0"
},
"dependencies": {
"debug": "^2.2.0",
"nachos-home": "^1.0.1",
"nachos-settings-file": "^1.0.3"
"nachos-settings-file": "^1.0.3",
"q": "^1.4.1"
}
}
164 changes: 164 additions & 0 deletions test/defaults.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use strict';

var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var mockery = require('mockery');
var Q = require('q');

chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));

describe('nachos-config', function () {
describe('app defaults', function () {
describe('get', function () {
var nachosConfig;
var configFile = {
defaults: {
exts: {
txt: 'lol.exe %1'
}
}
};

beforeEach(function () {
var nachosSettingsFileMock = function () {
};

nachosSettingsFileMock.prototype.get = sinon.stub().returns(Q.resolve(configFile));

mockery.registerMock('nachos-settings-file', nachosSettingsFileMock);
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});

nachosConfig = require('../lib');
});

afterEach(function () {
mockery.deregisterMock('nachos-settings-file');
mockery.disable();
});

describe('without parameters', function () {
it('should reject with type error', function () {
return expect(nachosConfig.getDefaultApp()).to.eventually.be.rejectedWith(TypeError, 'extension must be provided');
});
});

describe('with invalid parameters', function () {
it('should reject as function', function () {
return expect(nachosConfig.getDefaultApp(function () {
})).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as object', function () {
return expect(nachosConfig.getDefaultApp({})).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as number', function () {
return expect(nachosConfig.getDefaultApp(5)).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as boolean', function () {
return expect(nachosConfig.getDefaultApp(true)).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});
});

describe('with valid parameters', function () {
it('should get a command', function () {
return expect(nachosConfig.getDefaultApp('txt')).to.eventually.equal(configFile.defaults.exts.txt);
});

it('should be empty', function () {
return expect(nachosConfig.getDefaultApp('mkv')).to.eventually.be.empty;
});
});
});

describe('set', function () {
var nachosConfig;
var nachosSettingsFileMock;

beforeEach(function () {
nachosSettingsFileMock = function () {};

nachosSettingsFileMock.prototype.get = sinon.stub().returns(Q.resolve({ defaults: { exts: {}}}));
nachosSettingsFileMock.prototype.set = sinon.stub().returns(Q.resolve());

mockery.registerMock('nachos-settings-file', nachosSettingsFileMock);
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});

nachosConfig = require('../lib');
});

afterEach(function () {
mockery.deregisterMock('nachos-settings-file');
mockery.disable();
});

describe('without parameters', function () {
it('should reject without extension', function () {
return expect(nachosConfig.setDefaultApp()).to.eventually.be.rejectedWith(TypeError, 'extension must be provided');
});

it('should reject without command', function () {
return expect(nachosConfig.setDefaultApp('txt')).to.eventually.be.rejectedWith(TypeError, 'command must be provided');
});
});

describe('with invalid parameters', function () {
describe('invalid extension', function () {
it('should reject as function', function () {
return expect(nachosConfig.setDefaultApp(function () {})).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as object', function () {
return expect(nachosConfig.setDefaultApp({})).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as number', function () {
return expect(nachosConfig.setDefaultApp(5)).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});

it('should reject app as boolean', function () {
return expect(nachosConfig.setDefaultApp(true)).to.eventually.be.rejectedWith(TypeError, 'extension must be a string');
});
});

describe('invalid command', function () {
it('should reject as function', function () {
return expect(nachosConfig.setDefaultApp('txt', function () {})).to.eventually.be.rejectedWith(TypeError, 'command must be a string');
});

it('should reject app as object', function () {
return expect(nachosConfig.setDefaultApp('txt', {})).to.eventually.be.rejectedWith(TypeError, 'command must be a string');
});

it('should reject app as number', function () {
return expect(nachosConfig.setDefaultApp('txt', 5)).to.eventually.be.rejectedWith(TypeError, 'command must be a string');
});

it('should reject app as boolean', function () {
return expect(nachosConfig.setDefaultApp('txt', true)).to.eventually.be.rejectedWith(TypeError, 'command must be a string');
});
});
});

describe('with valid parameters', function () {
it('should set a command', function () {
return nachosConfig.setDefaultApp('txt', 'app')
.then(function () {
expect(nachosConfig.set).to.have.been.calledWithExactly({ defaults: { exts: { txt: 'app' }}});
});
});
});
});
});
});
5 changes: 5 additions & 0 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ describe('nachos-config', function () {
it('should expose a nachosConfig ctor', function () {
expect(nachosConfig.NachosConfig).to.be.a('function');
});

it('should expose app defaults functions', function () {
expect(nachosConfig.getDefaultApp).to.be.a('function');
expect(nachosConfig.setDefaultApp).to.be.a('function');
});
});
});

0 comments on commit 900e027

Please sign in to comment.