Skip to content

Commit

Permalink
Bugfix/1.0.0/babel (#117)
Browse files Browse the repository at this point in the history
* bugfix for babel config

Babel config wasn't being found correctly and this fixes the issue. Also added a test case for it.

* removed dob from cli test

This is because dates won't generate the same thing when using seeds.

* added in code coverage check to `make ci`

* added `babel-plugin-array-includes`

* added `babel-plugin-transform-runtime`

* removed es6 instances from test/utils.js

* Fixed testing issues with older versions of node
  • Loading branch information
tjbenton authored Jan 13, 2017
1 parent 1dbb4dc commit ebe203e
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
],
"plugins": [
"transform-runtime",
"external-helpers"
"external-helpers",
"array-includes"
]
}
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ test:
coverage test-coverage code-coverage:
# if there's no instance source maps files then build the files with source maps
@[ -f ./dist/index.js.map ] || (echo "building files with source maps" && make build-source-maps)
NODE_ENV="test" nyc make test -- --verbose
NODE_ENV="test" nyc --silent -- ava --verbose --no-cache


# These commands only run the report of the code coverage
report-coverage report-code-coverage:
Expand All @@ -69,7 +70,13 @@ report-coverage report-code-coverage:

# The command the ci server runs
ci:
make lint build-source-maps coverage -i
make lint build-source-maps -i
# if the tests fail then it will exit with an error
make coverage || exit 1
# show the coverage report
nyc report
# check check-coverage and if it fails then exit
nyc check-coverage --statements 95 --functions 95 --lines 95 || exit 1

# "patch", "minor", "major", "prepatch",
VERS ?= "patch"
Expand Down
5 changes: 4 additions & 1 deletion app/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ export default class Base extends Logger {
/// @raw-code
constructor(options = {}) {
super(options);
const root = process.cwd();
this.options = to.extend({
root: process.cwd(),
root,
log: true,
verbose: false,
timestamp: true,
}, this.options || {});
this.options = to.extend(this.options, options);

this.options.root = path.resolve(root, this.options.root);

if (this.options.verbose) {
this.options.log = true;
}
Expand Down
26 changes: 20 additions & 6 deletions app/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { set, get, find } from 'lodash';
import to, { is } from 'to-js';
import { transform } from 'babel-core';
import globby from 'globby';
import findRoot from 'find-root';

////
/// @name Models
Expand Down Expand Up @@ -64,14 +65,27 @@ export default class Models extends Base {
return;
}

const dir = path.join(__dirname.split('node_modules')[0], '..');
let file = await globby(this.resolvePaths(babel_config, dir), { dot: true });
let file = [ process.cwd(), this.options.root ]
.reduce((prev, next) => {
try {
return prev.concat(path.join(findRoot(next), babel_config));
} catch (e) {
return prev;
}
}, []);

file = await globby(to.unique(file), { dot: true });
file = file[0];
let config = await fs.readJson(file);
if (file.includes('package.json')) {
config = config.babelConfig || {};

if (file) {
let config = await fs.readJson(file);
if (file.includes('package.json')) {
config = config.babelConfig || {};
}

this.options.babel_config = config;
}
this.options.babel_config = config;

this.prepared = true;
}

Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"es6-promise-pool": "^2.4.4",
"es6-promisify": "^5.0.0",
"faker": "^3.1.0",
"find-root": "~1.0.0",
"fs-extra-promisify": "^0.0.2",
"globby": "^6.1.0",
"highlight-es": "~1.0.0",
Expand All @@ -77,7 +78,9 @@
"ava": "^0.16.0",
"ava-spec": "github:tjbenton/ava-spec",
"babel-cli": "^6.18.0",
"babel-plugin-array-includes": "~2.0.3",
"babel-plugin-external-helpers": "~6.18.0",
"babel-plugin-transform-runtime": "~6.15.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"coveralls": "^2.11.15",
Expand All @@ -98,7 +101,8 @@
],
"plugins": [
"transform-runtime",
"external-helpers"
"external-helpers",
"array-includes"
]
},
"ava": {
Expand All @@ -111,7 +115,8 @@
"test/utils.js"
],
"failFast": true,
"concurrency": 5
"concurrency": 5,
"babel": "inherit"
},
"nyc": {
"exclude": [
Expand Down
7 changes: 5 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test.group('console', (test) => {
doc_type: 'contact',
channels: [ 'ufp-555555555' ],
contact_id: '1d54ed12-b65a-5085-a895-5c8c626f0efb',
details: { prefix: 'Dr.', first_name: 'Daphnee', middle_name: 'Dale', last_name: 'O\'Hara', company: 'Hackett - Effertz', job_title: null, dob: '2016-08-21', nickname: null },
details: { prefix: 'Dr.', first_name: 'Daphnee', middle_name: 'Dale', last_name: 'O\'Hara', company: 'Hackett - Effertz', job_title: null, nickname: null },
phones: [ { type: 'Mobile', phone_number: '076-099-8620', extension: null }, { type: 'Other', phone_number: '965-618-1647', extension: null } ],
emails: [ 'Abigale.Bashirian@gmail.com', 'Demetris12@gmail.com' ],
addresses: [ { type: 'Work', address_1: '96735 Caroline Fields Springs', address_2: null, locality: 'Montanastad', region: 'SD', postal_code: '11307-4822', country: 'LA' } ],
Expand Down Expand Up @@ -102,6 +102,7 @@ test.group('console', (test) => {

// remove the dates because they can't be correct
stdout = _.omit(stdout[0], [ 'created_on', 'modified_on' ]);
stdout.details = _.omit(stdout.details, [ 'dob' ]);
t.deepEqual(stdout, expected_abc_seed);
})
.end(t.end);
Expand All @@ -126,6 +127,8 @@ test.group('console', (test) => {
'doc_type',
'channels',
]);
// removed the dob because it's a date
stdout.details = _.omit(stdout.details, [ 'dob' ]);

for (let key in stdout) {
if (stdout.hasOwnProperty(key)) {
Expand Down Expand Up @@ -195,7 +198,7 @@ test.cb('throws error when something goes wrong', (t) => {
/* eslint-disable quotes */
bin.clone()
.run(`folder 'error-test' 'simple/models/*' --count 1 --archive 'woohoo'`)
.stderr(/The archive file must have a file extention of \`\.zip\`/)
.stdout(/The archive file must have a file extention of \`\.zip\`/)
.end(t.end);
/* eslint-enable quotes */
});
Expand Down
19 changes: 17 additions & 2 deletions test/models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('prepare', async (t) => {
t.deepEqual(t.context.options.babel_config, babel_config);
});

test.group('setup', (test) => {
test.serial.group('setup', (test) => {
test('babel_config as a string', async (t) => {
t.is(t.context.prepared, false);
t.is(t.context.preparing, undefined);
Expand Down Expand Up @@ -123,6 +123,21 @@ test.group('setup', (test) => {
t.is(to.type(t.context.options.babel_config), 'object');
t.deepEqual(t.context.options.babel_config, babel_config);
});

test('babel_config process.cwd failed to find a babel config', async (t) => {
t.is(t.context.prepared, false);
t.is(t.context.preparing, undefined);
t.context.options.root = t.context.options.root.split('fakeit')[0].slice(0, -1);
t.context.options.babel_config = 'package.json';
t.is(typeof t.context.options.babel_config, 'string');
const preparing = t.context.setup();
t.is(typeof t.context.preparing.then, 'function');
t.is(t.context.prepared, false);
await preparing;
t.is(t.context.prepared, true);
t.is(to.type(t.context.options.babel_config), 'object');
t.deepEqual(t.context.options.babel_config, babel_config);
});
});


Expand Down Expand Up @@ -292,7 +307,7 @@ test.group('parseModelFunctions', (test) => {
const model = to.clone(contents[file]);
const paths = utils.getPaths(model, /((pre|post)_run)|(pre_|post_)?build$/);
const obj = _.pick(model, paths);
parseModelFunctions(obj);
parseModelFunctions(obj, babel_config);

for (let str of paths) {
let fn = _.get(obj, str);
Expand Down
2 changes: 1 addition & 1 deletion test/output/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('prepare', async (t) => {
t.is(t.context.prepared, true);
});

test('setup', async (t) => {
test.serial('setup', async (t) => {
t.is(t.context.prepared, false);
t.is(t.context.preparing, undefined); // eslint-disable-line
const preparing = t.context.setup();
Expand Down
4 changes: 2 additions & 2 deletions test/output/couchbase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ test.group('finalize', (test) => {
await t.context.prepare();
t.is(to.type(t.context.bucket), 'object');
t.is(t.context.prepared, true);
t.is(t.context.preparing.toString(), '[object Promise]');
t.is(typeof t.context.preparing.then, 'function');
t.is(t.context.bucket.connected, true);
await t.context.finalize();
t.is(to.type(t.context.bucket), 'object');
t.is(t.context.prepared, true);
t.is(t.context.preparing.toString(), '[object Promise]');
t.is(typeof t.context.preparing.then, 'function');
t.is(t.context.bucket.connected, false);
});
});
6 changes: 3 additions & 3 deletions test/output/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ test.group('validation', (test) => {
});


test.group('prepare', (test) => {
test.serial.group('prepare', (test) => {
const root = p(output_root, 'prepare');

test('without options', async (t) => {
Expand Down Expand Up @@ -513,7 +513,7 @@ test.group('prepare', (test) => {
});


test.group('setup', (test) => {
test.serial.group('setup', (test) => {
test('without options', async (t) => {
t.is(t.context.prepared, false);
t.is(t.context.preparing, undefined);
Expand Down Expand Up @@ -547,7 +547,7 @@ test.group('output', (test) => {
data = await getData();
});

test.group('return', languages((test, language) => {
test.serial.group('return', languages((test, language) => {
test(language, async (t) => {
const { raw, node } = data[language];
t.context.output_options.output = 'return';
Expand Down
2 changes: 1 addition & 1 deletion test/output/sync-gateway.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test.group('prepare', (test) => {
});
});

test.group('setup', (test) => {
test.serial.group('setup', (test) => {
test('no username and password', async (t) => {
t.context.output_options.bucket = 'setup';
t.is(t.context.prepared, false);
Expand Down
22 changes: 12 additions & 10 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ module.exports.models = function(settings) {
item = settings.modules[item];
}

return !!item && item.includes(model);
return !!item && item.indexOf(model) >= 0;
}).filter(Boolean).length;

// if the model isn't in the todo list then run the tests
if (
should_test && !options.todo.includes(model)
should_test && options.todo.indexOf(model) < 0
) {
let schema;
var schema;
// get the schema to use for validating that the output is correct
try {
schemas[model] = schema = schemas[model] || require(p(settings.root, settings.validation(model)));
Expand Down Expand Up @@ -146,7 +146,9 @@ module.exports.models = function(settings) {

// find the keys that still need to be validated
var omitted = _.difference(to.keys(actual), schema_keys)
.filter((key) => ![ '__key', '__name' ].includes(key));
.filter(function(key) {
return [ '__key', '__name' ].indexOf(key) < 0;
});
// test the keys that exsit
var picked = _.pick(actual, schema_keys);

Expand All @@ -160,13 +162,13 @@ module.exports.models = function(settings) {
// validate the object that can be validated
function validate(err) {
if (err) {
let segments = err.message.match(/(?:child\s+(?:"))([^"]+)(?:")/g);
var segments = err.message.match(/(?:child\s+(?:"))([^"]+)(?:")/g);
if (segments) {
segments = segments.map(function(segment) {
return segment.replace(/child\s+|"/g, '');
});
let item_path = segments.join('.');
let item = _.get(picked, item_path);
var item_path = segments.join('.');
var item = _.get(picked, item_path);
if (item) {
console.log(' ', item_path, '=', item);
} else {
Expand Down Expand Up @@ -233,11 +235,11 @@ module.exports.models = function(settings) {
module.exports.getPaths = function getPaths(model, regex) {
return to.keys(model).concat(to.keys(to.flatten(model)))
.reduce((result, next) => {
let current = '';
for (let key of next.split('.')) {
var current = '';
for (var key of next.split('.')) {
current = current.split('.').concat(key).filter(Boolean).join('.');
if (regex == null || regex.test(current)) {
if (!result.includes(current)) {
if (result.indexOf(current) < 0) {
result.push(current);
}
}
Expand Down

0 comments on commit ebe203e

Please sign in to comment.