Permalink
Browse files

fix(loader): .clutchrc loader (#15)

  • Loading branch information...
gurpreetatwal committed May 8, 2017
2 parents 529355b + 818ae43 commit 8a90557a866fbf41d4f052db93190cef44a1bcfc
Showing with 158 additions and 29 deletions.
  1. +11 −1 loader/helpers.js
  2. +1 −1 loader/index.js
  3. +2 −0 package.json
  4. +0 −1 test/loader-error/.clutchrc
  5. +0 −20 test/loader-error/index.js
  6. +0 −3 test/loader/.clutchrc
  7. +18 −0 test/loader/helpers.js
  8. +126 −3 test/loader/index.js
View
@@ -5,6 +5,16 @@ const path = require('path');
const DEFAULT_DIRECTORY = 'test';
function findParent(paths) {
for (const p of paths) {
if (!p.includes('clutch-assert')) {
return path.dirname(p);
}
}
}
function getDirectory(rc) {
if (!(typeof rc === 'object' && rc.directory)) {
@@ -43,4 +53,4 @@ function createPattern(dir) {
return dir + path.sep + '**' + path.sep + '*.js';
}
module.exports = {getDirectory, checkDirectory, createPattern};
module.exports = {getDirectory, checkDirectory, createPattern, findParent};
View
@@ -6,7 +6,7 @@ const helpers = require('./helpers');
const espower = require('espower-loader');
const patterns = require('../lib/patterns');
const parent = path.dirname(module.parent.paths[0]);
const parent = helpers.findParent(module.paths);
var rc;
try {
View
@@ -51,7 +51,9 @@
"call-signature": "0.0.2",
"eslint": "^3.7.1",
"eslint-config-smartcar": "^2.0.0",
"mockery": "^2.0.0",
"nyc": "^10.0.0",
"sinon": "^2.2.0",
"zen-observable": "^0.5.0"
}
}

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.
View

This file was deleted.

Oops, something went wrong.
View
@@ -4,6 +4,24 @@ const test = require('ava');
const {sep} = require('path');
const helpers = require('../../loader/helpers');
test('findPackageDir', function(t) {
// example output from module.paths
const paths = [
'/home/user/project/node_modules/clutch-assert/loader/node_modules',
'/home/user/project/node_modules/clutch-assert/node_modules',
'/home/user/project/node_modules',
'/home/user/node_modules',
'/home/node_modules',
'/node_modules',
];
const path = helpers.findParent(paths);
t.is(path, '/home/user/project');
});
test('getDirectory', function(t) {
t.is(helpers.getDirectory(), 'test');
View
@@ -2,16 +2,139 @@
/* eslint-disable global-require */
const os = require('os');
const fs = require('fs');
const test = require('ava');
const path = require('path');
const sinon = require('sinon');
const mockery = require('mockery');
const helpers = require('../../loader/helpers');
test.afterEach(function() {
delete require.cache[require.resolve('../../loader')];
// eslint-disable-next-line no-process-env
if (process.env.NYC_CONFIG) {
// silence mockery when running coverage
mockery.warnOnUnregistered(false);
}
/**
* Creates a temporary directory and writes a .clutchrc file in that directory
*
* @param {String} data - the data to be written to the rc file
* @return {String} the directory in which the file was written
*/
function writeRC(data) {
return new Promise(function(resolve, reject) {
fs.mkdtemp(os.tmpdir() + path.sep, function(err, dir) {
if (err) { return reject(err); }
fs.writeFile(path.join(dir, '.clutchrc'), data, function(err) {
if (err) { return reject(err); }
return resolve(dir);
});
});
});
}
test.before(function() {
mockery.enable({
useCleanCache: true,
});
});
test('overall', function(t) {
test.beforeEach(function(t) {
t.context.stub = sinon.stub();
mockery.registerMock('espower-loader', t.context.stub);
mockery.resetCache();
mockery.registerAllowables(['fs', 'path', '../../loader', '../lib/patterns']);
});
test.afterEach.always.cb(function(t) {
mockery.deregisterAll();
if (t.context.path) {
fs.unlink(t.context.path, () => t.end());
} else {
t.end();
}
});
test.after.always(function() {
mockery.disable();
});
test.serial('no rc file', function(t) {
const mock = Object.assign({}, helpers, {
findParent: () => __dirname,
});
mockery.registerMock('./helpers', mock);
mockery.registerMock('../lib/patterns', {
ENHANCED: 'MOCK',
});
require('../../loader');
t.true(t.context.stub.called);
const arg = t.context.stub.args[0][0];
t.is(arg.pattern, 'test/**/*.js');
t.is(arg.cwd, process.cwd());
t.is(arg.espowerOptions.patterns, 'MOCK');
});
test.serial('valid rc file', async function(t) {
const path = t.context.path = await writeRC('{"directory": "mock"}');
const mock = Object.assign({}, helpers, {
findParent: () => path,
checkDirectory: () => true,
});
mockery.registerMock('./helpers', mock);
require('../../loader');
t.true(t.context.stub.called);
const arg = t.context.stub.args[0][0];
t.is(arg.pattern, 'mock/**/*.js');
});
test.serial('invalid rc file', async function(t) {
const path = t.context.path = await writeRC('this is not JSON');
mockery.registerMock('./helpers', {
findParent: () => path,
});
const err = t.throws(() => require('../../loader'), SyntaxError);
t.regex(err.message, /^Unexpected token \w+( in JSON at position \d+)?$/);
});
test.serial('integration', async function(t) {
const path = t.context.path = await writeRC('{"directory": "test/fixtures"}');
mockery.warnOnUnregistered(false);
mockery.deregisterMock('espower-loader');
const mock = Object.assign({}, helpers, {
findParent: () => path,
});
mockery.registerMock('./helpers', mock);
require('../../loader');
const err = t.throws(function() {
require('../fixtures/to_be_instrumented');
});

0 comments on commit 8a90557

Please sign in to comment.