Added more unit tests, up to 98% coverage. Fixed the Gruntfile so it's not a cluster-f**k anymore.

This commit is contained in:
Spencer Alger
2013-12-06 18:39:48 -07:00
parent 086636c8a4
commit 270763e0a7
37 changed files with 1361 additions and 433 deletions

View File

@ -15,9 +15,40 @@ module.exports = function (argv, steps) {
}
var tasks = {
exec: function (params, exitCb) {
var cmd = params.cmd;
var opts = {};
if (params.cwd) {
opts.cwd = path.resolve(params.cwd);
}
log('running', cmd, (opts.cwd ? 'in ' + opts.cwd : ''));
cp.exec(cmd, opts, function (err, stdout, stderr) {
stdout = stdout.trim();
stderr = stderr.trim();
if (err) {
console.error('Error! status:', err.code, ' -----\n' + err.message);
process.exit(1);
}
else {
if (argv.verbose) {
if (stderr) {
console.error('----------- STDERR -----------');
console.error(stdout);
console.error('------------------------------');
}
console.log(stdout);
}
exitCb();
}
});
},
run: function (params, exitCb) {
var cmd = params.cmd;
var args = params.args;
var args = params.args || [];
var opts = {
stdio: argv.verbose ? 'inherit' : 'ignore'
};

View File

@ -0,0 +1,24 @@
var force = process.env.FORCE || process.env.FORCE_GEN;
if (!force) {
var argv = require('optimist')
.options({
force: {
alias: 'f',
default: false,
boolean: true
}
});
if (process.env.npm_config_argv) {
// when called by NPM
argv = argv.parse(JSON.parse(process.env.npm_config_argv).original);
} else {
// when called directly
argv = argv.argv;
}
force = argv.force;
}
module.exports = force;

View File

@ -0,0 +1,59 @@
module.exports = function (force) {
var _ = require('../../../src/lib/utils');
var fs = require('fs');
var templates = require('./templates');
var restSpecUpdated = require('../../rest_spec_updated');
var outputPath = _.joinPath(__dirname, '../../../src/lib/api.js');
var docOutputDir = _.joinPath(__dirname, '../../../docs/');
function download() {
require('./actions').on('ready', function (actions) {
var namespaces = _.filter(_.map(actions, function (action) {
if (~action.location.indexOf('.')) {
var path = action.location.split('.').slice(0, -1);
_.pull(path, 'prototype');
return path.join('.');
}
}));
// seperate the proxy actions
var groups = _.groupBy(actions, function (action) {
return action.proxy ? 'proxies' : 'normal';
});
fs.unlink(outputPath, function () {
console.log('writing', actions.length, 'api actions to', outputPath);
fs.writeFileSync(outputPath, templates.apiFile({
actions: groups.normal,
proxies: groups.proxies,
namespaces: _.unique(namespaces.sort(), true)
}));
if (!fs.existsSync(docOutputDir)) {
fs.mkdirSync(docOutputDir);
}
fs.writeFileSync(docOutputDir + '_method_list.jade', templates.apiMethodList({
actions: actions
}));
fs.writeFileSync(docOutputDir + '_methods.jade', templates.apiMethods({
actions: actions
}));
});
});
}
if (force) {
download();
} else {
restSpecUpdated(function (err, updated) {
if (err || updated) {
download();
}
});
}
};

View File

@ -1,53 +1 @@
var _ = require('../../../src/lib/utils');
var fs = require('fs');
var templates = require('./templates');
var restSpecUpdated = require('../../rest_spec_updated');
var outputPath = _.joinPath(__dirname, '../../../src/lib/api.js');
var docOutputDir = _.joinPath(__dirname, '../../../docs/');
function download() {
require('./actions').on('ready', function (actions) {
var namespaces = _.filter(_.map(actions, function (action) {
if (~action.location.indexOf('.')) {
var path = action.location.split('.').slice(0, -1);
_.pull(path, 'prototype');
return path.join('.');
}
}));
// seperate the proxy actions
var groups = _.groupBy(actions, function (action) {
return action.proxy ? 'proxies' : 'normal';
});
fs.unlink(outputPath, function () {
console.log('writing', actions.length, 'api actions to', outputPath);
fs.writeFileSync(outputPath, templates.apiFile({
actions: groups.normal,
proxies: groups.proxies,
namespaces: _.unique(namespaces.sort(), true)
}));
if (!fs.existsSync(docOutputDir)) {
fs.mkdirSync(docOutputDir);
}
fs.writeFileSync(docOutputDir + '_method_list.jade', templates.apiMethodList({
actions: actions
}));
fs.writeFileSync(docOutputDir + '_methods.jade', templates.apiMethods({
actions: actions
}));
});
});
}
restSpecUpdated(function (err, updated) {
if (err || updated) {
download();
}
});
require('./generate')(require('../_force'));

View File

@ -0,0 +1,44 @@
module.exports = function (force) {
/**
* Check that the test directory exists, and is less than a day old, otherwise wipe it out
* and rebuild
*/
var fs = require('fs');
var path = require('path');
var jsYaml = require('js-yaml');
var spec = require('../../get_spec');
var restSpecUpdated = require('../../rest_spec_updated');
var testFile = path.resolve(__dirname, '../../../test/integration/yaml_suite/yaml_tests.json');
function download() {
var tests = {};
fs.unlink(testFile, function () {
spec.get('test/**/*.yaml')
.on('entry', function (entry) {
var filename = path.relative('test', entry.path);
var file = tests[filename] = [];
jsYaml.loadAll(entry.data, function (doc) {
file.push(doc);
});
})
.on('end', function () {
fs.writeFileSync(testFile, JSON.stringify(tests, null, ' '), 'utf8');
console.log('download yaml tests to', testFile);
});
});
}
if (force) {
download();
} else {
restSpecUpdated(function (err, updated) {
if (err || updated) {
download();
}
});
}
};

View File

@ -1,38 +1 @@
/**
* Check that the test directory exists, and is less than a day old, otherwise wipe it out
* and rebuild
*/
var fs = require('fs');
var path = require('path');
var jsYaml = require('js-yaml');
var spec = require('../../get_spec');
var restSpecUpdated = require('../../rest_spec_updated');
var testFile = path.resolve(__dirname, '../../../test/integration/yaml_suite/yaml_tests.json');
function download() {
var tests = {};
fs.unlink(testFile, function () {
spec.get('test/**/*.yaml')
.on('entry', function (entry) {
var filename = path.relative('test', entry.path);
var file = tests[filename] = [];
jsYaml.loadAll(entry.data, function (doc) {
file.push(doc);
});
})
.on('end', function () {
fs.writeFileSync(testFile, JSON.stringify(tests, null, ' '), 'utf8');
console.log('download yaml tests to', testFile);
});
});
}
restSpecUpdated(function (err, updated) {
if (err || updated) {
download();
}
});
require('./generate')(require('../_force'));

View File

@ -7,7 +7,6 @@ var request = {
}
};
var fs = require('fs');
var _ = require('lodash');
var lastRestSpecUpdateFile = __dirname + '/last_rest_spec_update.sha';
var lastRestSpecUpdate;
@ -17,61 +16,46 @@ if (fs.existsSync(lastRestSpecUpdateFile)) {
lastRestSpecUpdate = fs.readFileSync(lastRestSpecUpdateFile, 'utf8');
}
var req = null;
var force = false;
var req = https.get(request, function (incoming) {
if (incoming.statusCode !== 200) {
req.abort();
console.error('request for last commit failed', incoming.statusCode, incoming.headers);
return;
}
if (process.env.npm_config_force ||
process.env.FORCE_GEN ||
_.contains(process.argv, '-f') ||
_.contains(process.argv, '--force')
) {
force = true;
}
var body = '';
if (force) {
updated = true;
} else {
req = https.get(request, function (incoming) {
if (incoming.statusCode !== 200) {
req.abort();
console.error('request for last commit failed', incoming.statusCode, incoming.headers);
incoming.on('data', onData);
incoming.on('end', onEnd);
function onData(chunk) {
body += chunk;
}
function onEnd() {
incoming.removeListener('data', onData);
incoming.removeListener('end', onEnd);
var _req = req;
req = null;
var resp;
try {
resp = JSON.parse(body);
} catch (e) {
console.log('unable to parse response from github');
_req.emit('ready');
return;
}
var body = '';
incoming.on('data', onData);
incoming.on('end', onEnd);
function onData(chunk) {
body += chunk;
if (lastRestSpecUpdate === resp.sha) {
updated = false;
} else {
updated = true;
fs.writeFileSync(lastRestSpecUpdateFile, resp.sha);
}
function onEnd() {
incoming.removeListener('data', onData);
incoming.removeListener('end', onEnd);
var _req = req;
req = null;
var resp;
try {
resp = JSON.parse(body);
} catch (e) {
console.log('unable to parse response from github');
_req.emit('ready');
return;
}
if (lastRestSpecUpdate === resp.sha) {
updated = false;
} else {
updated = true;
fs.writeFileSync(lastRestSpecUpdateFile, resp.sha);
}
_req.emit('ready');
}
});
}
_req.emit('ready');
}
});
module.exports = function (cb) {
function done() {

View File

@ -0,0 +1,37 @@
var fs = require('fs');
var argv = require('optimist')
.default({
verbose: false
})
.alias({
v: 'verbose'
})
.argv;
var steps = [];
var cmd = argv._.join(' ');
if (!cmd) {
throw new Error('you should specify a command...');
}
['browser', 'jquery', 'angular'].forEach(function (build) {
if (!fs.existsSync('../bower-elasticsearch-' + build) ||
!fs.existsSync('../bower-elasticsearch-' + build + '/.git')
) {
throw new Error('Ensure that all of the bower repos are checked out next to this repo');
}
steps.push([
'exec', {
cmd: cmd,
cwd: '../bower-elasticsearch-' + build
}
]);
});
require('./_steps')(argv, steps);