Skip to content

Commit

Permalink
adding more test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 28, 2017
1 parent 21f3748 commit 9c65515
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 12 deletions.
1 change: 1 addition & 0 deletions argv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
/* istanbul ignore next */
module.exports = require('minimist')(process.argv.slice(2), {
alias: {
version: 'v',
Expand Down
4 changes: 1 addition & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const Promise = require('bluebird');
const workspaceDir = require('./lib/workspaceDir');
const output = require('./lib/output');

const refresh = _.partial(lib.getSourceData, true);

module.exports = cli;

function setSearchTerm(options) {
Expand Down Expand Up @@ -45,7 +43,7 @@ function cli(options) {
}

if (options.refresh) {
return refresh();
return lib.getSourceData(true);
} else {
const sourceDataPromise = lib.getSourceData();
const pathsPromise = sourceDataPromise.then(lib.getPaths).filter((v) => (new RegExp(searchTerm)).test(v));
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env node
/* istanbul ignore next */
(function() {

const cli = require('./cli');
const argv = require('./argv');
const setTarget = require('./lib/setTarget');
Expand All @@ -9,3 +12,5 @@ cli(argv)
console.log(err.message);
process.exit(1);
});

}());
6 changes: 6 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
const rc = require('rc')('bitcar', {});

module.exports = {
get: () => rc
};
13 changes: 7 additions & 6 deletions lib/getSourceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ const path = require('path');
const _ = require('lodash');
const Promise = require('bluebird');
const workspaceDir = require('./workspaceDir');
const rc = require('rc')('bitcar', {});
const config = require('./config');
const drivers = require('../drivers');
const output = require('./output');

module.exports = getSourceData;
const CACHE_PATH = path.normalize(process.env.HOME + '/.bitcar/cache.json');

function getSourceData(forceRefresh = false) {
let rc = config.get();
if (fs.exists(CACHE_PATH) && !forceRefresh) {
return Promise.resolve(fs.readJSON(CACHE_PATH));
} else {
const config = fs.readJSON(path.normalize(process.env.HOME + '/.bitcar/config'));
const drivers = require('../drivers');
console.log('Loading cache...');
const selected = _.pick(drivers, _.map(config.drivers, (v) => v.type));
output.log('Loading cache...');
const selected = _.pick(drivers, _.map(rc.drivers, (v) => v.type));
return Promise.props(_.mapValues(selected, (driver) => driver(rc)))
.then((sourceData) => {
sourceData = _.mapValues(sourceData, _.flow(_.flattenDeep, _.compact));
Expand All @@ -28,7 +29,7 @@ function getSourceData(forceRefresh = false) {
fs.writeJSON(CACHE_PATH, sourceData, null, 4);
fs.commit((err) => {
if (err) return reject(err);
console.log('Cache set successfully');
output.log('Cache set successfully');
return resolve(sourceData);
});
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"watch": "npm run test -- --watch",
"changelog": "./node_modules/.bin/github-changes -o carsdotcom -r bitcar",
"generate-and-stage-changelog": "npm run changelog && git add -A",
"coverage": "rm -rf coverage && node node_modules/.bin/istanbul cover -x \"test/**/*.js\" --dir=\"coverage\" --root=\"./\" node_modules/.bin/_mocha -- \"test/**/*.js\"",
"check-coverage": "npm run coverage && ./node_modules/.bin/istanbul check-coverage --root=\"./coverage\" --statement 60 --function 70 --branch 45"
"coverage": "rm -rf coverage && node node_modules/.bin/istanbul --include-all-sources cover -x \"test/**/*.js\" --dir=\"coverage\" --root=\"./\" node_modules/.bin/_mocha -- \"test/**/*.js\"",
"check-coverage": "npm run coverage && ./node_modules/.bin/istanbul check-coverage --root=\"./coverage\" --statement 50 --function 50 --branch 45"
},
"contributors": [
{
Expand Down
48 changes: 47 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const path = require('path');
const gitFactory = require('../lib/gitFactory');
const browser = require('../lib/browser');
const output = require('../lib/output');
const config = require('../lib/config');
const drivers = require('../drivers');
const axios = require('axios');

// store cache fixture in mem-fs
const CACHE_PATH = path.normalize(process.env.HOME + '/.bitcar/cache.json');
Expand All @@ -32,12 +35,15 @@ describe('the bitcar cli', () => {
return mocks.git;
});
sandbox.stub(browser, 'open', mocks.open);
sandbox.stub(fs, 'commit');
sandbox.stub(fs, 'commit', (cb) => cb());
sandbox.stub(config, 'get', () => mocks.config);
sandbox.stub(inquirer, 'prompt', (options) => {
const setupPromptValidation = schemas.setupPrompt.validate(options);
if (!setupPromptValidation.error) return Promise.resolve(mocks.setupAnswers);
const resultsPromptValidation = schemas.resultsPrompt.validate(options);
if (!resultsPromptValidation.error) return Promise.resolve(mocks.resultAnswers);
const credentialsPromptValidation = schemas.credentialsPrompt.validate(options);
if (!credentialsPromptValidation.error) return Promise.resolve(mocks.credentialsAnswers);
return Promise.reject();
});
});
Expand Down Expand Up @@ -222,5 +228,45 @@ describe('the bitcar cli', () => {
});
});
});
describe('refresh option', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
});
it('should call each configured driver', () => {
sandbox.stub(drivers, 'bitbucket-server', () => Promise.resolve([]));
sandbox.stub(drivers, 'github', () => Promise.resolve([]));
return cli({ _: [ ], refresh: true })
.then(() => {
expect(drivers['bitbucket-server']).to.have.been.called;
expect(drivers.github).to.have.been.called;
});
});
});
describe('bitbucket-server driver', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
sandbox.stub(axios, 'request', () => Promise.resolve(mocks.bitbucketServerResponse));
sandbox.stub(drivers, 'github', () => Promise.resolve([]));
});
it('should call axios request', () => {
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.request).to.have.been.called;
});
});
});
describe('github driver', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
sandbox.stub(axios, 'get', () => Promise.resolve(mocks.githubResponse));
sandbox.stub(drivers, 'bitbucket-server', () => Promise.resolve([]));
});
it('should call axios request', () => {
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.get).to.have.been.called;
});
});
});
});

31 changes: 31 additions & 0 deletions test/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ exports.resultAnswers = {
result: 'github.com/carsdotcom/bitcar'
};

exports.credentialsAnswers = {
username: 'foo',
password: 'bar'
};

exports.git = {
clone: (url, dir, cb) => {
return cb(null, {});
Expand All @@ -28,6 +33,32 @@ exports.git = {
}
};

exports.bitbucketServerResponse = {
data: {
values: []
}
};

exports.githubResponse = {
headers: {},
data: []
};

exports.config = {
alias: "bit",
drivers: [
{
type: "github",
host: "github.com",
accessToken: "9eccdc79e394f713624486c0272f44fe67267b97"
},
{
type: "bitbucket-server",
host: "git.cars.com"
}
]
};

exports.open = function (url, cb) {
cb(null);
};
6 changes: 6 additions & 0 deletions test/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ exports.resultsPrompt = Joi.array().items(Joi.object({
choices: Joi.array().items(Joi.string().optional())
}));

exports.credentialsPrompt = Joi.array().items(Joi.object({
type: Joi.string().valid([ 'input', 'password' ]).required(),
name: Joi.string().valid([ 'username', 'password' ]).required(),
message: Joi.string().required()
}));

exports.result = Joi.object().keys({
name: Joi.string().required(),
clone: Joi.string().required(),
Expand Down

0 comments on commit 9c65515

Please sign in to comment.