Skip to content

Commit

Permalink
adding tests and refactoring options handling
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 28, 2017
1 parent d5cc0dc commit b0788e4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Change Log

### upcoming (2017/02/27 15:43 +00:00)
### v1.5.0 (2017/02/27 15:43 +00:00)
- [d5cc0dc](https://github.com/carsdotcom/bitcar/commit/d5cc0dcf588a24ca5afd0737d1aa052f61b8b0ca) 1.5.0 (@machellerogden)
- [05bd8ba](https://github.com/carsdotcom/bitcar/commit/05bd8baf9d1ae38eb1ae76348cfaae11693dd2a4) adding basic editor support (@machellerogden)
- [d8cdbb1](https://github.com/carsdotcom/bitcar/commit/d8cdbb10e76f24f63f787c2f7389e093fb508e11) adding test watch (@machellerogden)
- [8614f2f](https://github.com/carsdotcom/bitcar/commit/8614f2f04e863d25523338fc24efdfd015d6c1b9) more tests (@machellerogden)
- [6cf9aa3](https://github.com/carsdotcom/bitcar/commit/6cf9aa3ac95f76539c3804d9ff9b7dda18b6d0d2) adding tests (@machellerogden)
Expand Down
44 changes: 23 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,30 @@ const refresh = _.partial(lib.getSourceData, true);

module.exports = cli;

function cli(options) {
function setSearchTerm(options) {
let searchTerm;
if (options._ && options._[0]) {
searchTerm = options._[0];
} else if (_.isString(options.completions)) {
searchTerm = options.completions;
} else if (_.isString(options.open)) {
searchTerm = options.open;
} else if (_.isString(options.edit)) {
searchTerm = options.edit;
} else if (_.isString(options['clone-all'])) {
searchTerm = options['clone-all'];
} else if (_.isString(options['force-latest'])) {
searchTerm = options['force-latest'];
} else if (options.completions || options['clone-all'] || options['force-latest']) {
searchTerm = '.*';
} else {
searchTerm = '^' + _.takeRight(process.cwd().split(path.sep), 3).join(path.sep) + '$';
}
return searchTerm;
}

function cli(options) {
let searchTerm = setSearchTerm(options);

if (options.version) {
output.log(require('./package.json').version);
Expand All @@ -23,26 +45,6 @@ function cli(options) {
return require('./setup')();
}

if (_.isString(options.completions)) {
searchTerm = options.completions;
} else if (_.isString(options.open)) {
searchTerm = options.open;
} else if (_.isString(options.edit)) {
searchTerm = options.edit;
} else if (options['clone-all'] || options['force-latest']) {
if (_.isString(options['clone-all'])) {
searchTerm = options['clone-all'];
} else if (_.isString(options['force-latest'])) {
searchTerm = options['force-latest'];
} else {
searchTerm = '.*';
}
} else if (options._ && options._[0]) {
searchTerm = options._[0];
} else if (!options.completions) {
searchTerm = '^' + _.takeRight(process.cwd().split(path.sep), 3).join(path.sep) + '$';
}

if (options.refresh) {
return refresh();
} else {
Expand Down
41 changes: 40 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('the bitcar cli', () => {
});
describe('for non-existant entry', () => {
it('should exit non-zero with a message of "No results."', () => {
return expect(cli({ _: [ 'doesnotexist' ] })).to.eventually.be.rejectedWith('No results.');
return expect(cli({ _: [ 'doesnotexist' ], edit: true })).to.eventually.be.rejectedWith('No results.');
});
});
});
Expand All @@ -183,5 +183,44 @@ describe('the bitcar cli', () => {
});
});
});
describe('completions option', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
});
describe('with a search term', () => {
describe('for existing entry', () => {
it('should find existing entry for the search term in the cache - bitcar', () => {
return cli({ _: [ ], completions: 'bitcar' })
.then((results) => {
const resultsValidation = schemas.results.validate(results);
expect(resultsValidation.error).to.be.a('null');
});
});
it('regardless of order, find existing entry for the search term in the cache - bitcar', () => {
return cli({ _: [ 'bitcar' ], completions: true })
.then((results) => {
const resultsValidation = schemas.results.validate(results);
expect(resultsValidation.error).to.be.a('null');
});
});
});
describe('for non-existant entry', () => {
it('should exit non-zero with a message of "No results."', () => {
return expect(cli({ _: [ 'doesnotexist' ], completions: true })).to.eventually.eql([]);
});
});
});
describe('without a search term', () => {
describe('when current working directory corresponds to an entry in the cache', () => {
it('should find existing entry for the search term in the cache - bitcar', () => {
return cli({ _: [ ], completions: true })
.then((results) => {
const resultsValidation = schemas.results.validate(results);
expect(resultsValidation.error).to.be.a('null');
});
});
});
});
});
});

2 changes: 2 additions & 0 deletions test/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ exports.result = Joi.object().keys({
html: Joi.string().required(),
repoDir: Joi.string().required()
});

exports.results = Joi.array().items([ Joi.any().allow(null), exports.result ]).min(0);

0 comments on commit b0788e4

Please sign in to comment.