Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Normalize repository, dropping node <10.13 support #118

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format
phated committed Oct 18, 2021
commit 7c6e97082aa9bc365c8005cff3ae67167464923d
58 changes: 34 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -89,10 +89,13 @@ Liftoff.prototype.buildEnvironment = function (opts) {
var modulePath, modulePackage;
try {
var delim = path.delimiter;
var paths = (process.env.NODE_PATH ? process.env.NODE_PATH.split(delim) : []);
modulePath = resolve.sync(this.moduleName, { basedir: configBase || cwd, paths: paths });
var paths = process.env.NODE_PATH ? process.env.NODE_PATH.split(delim) : [];
modulePath = resolve.sync(this.moduleName, {
basedir: configBase || cwd,
paths: paths,
});
modulePackage = silentRequire(fileSearch('package.json', [modulePath]));
} catch (e) { }
} catch (e) {}

// if we have a configuration but we failed to find a local module, maybe
// we are developing against ourselves?
@@ -103,7 +106,10 @@ Liftoff.prototype.buildEnvironment = function (opts) {
modulePackage = silentRequire(modulePackagePath);
if (modulePackage && modulePackage.name === this.moduleName) {
// if it does, our module path is `main` inside package.json
modulePath = path.join(path.dirname(modulePackagePath), modulePackage.main || 'index.js');
modulePath = path.join(
path.dirname(modulePackagePath),
modulePackage.main || 'index.js'
);
cwd = configBase;
} else {
// clear if we just required a package for some other project
@@ -152,9 +158,11 @@ Liftoff.prototype.handleFlags = function (cb) {
}
});
} else {
process.nextTick(function () {
cb(null, this.v8flags);
}.bind(this));
process.nextTick(
function () {
cb(null, this.v8flags);
}.bind(this)
);
}
};

@@ -184,26 +192,28 @@ Liftoff.prototype.execute = function (env, forcedFlags, fn) {
throw new Error('You must provide a callback function.');
}

this.handleFlags(function (err, flags) {
if (err) {
throw err;
}
flags = flags || [];
this.handleFlags(
function (err, flags) {
if (err) {
throw err;
}
flags = flags || [];

flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this));
flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this));

function execute(ready, child, argv) {
if (child !== process) {
var execArgv = getNodeFlags.fromReorderedArgv(argv);
this.emit('respawn', execArgv, child);
}
if (ready) {
preloadModules(this, env);
registerLoader(this, this.extensions, env.configPath, env.cwd);
fn.call(this, env, argv);
function execute(ready, child, argv) {
if (child !== process) {
var execArgv = getNodeFlags.fromReorderedArgv(argv);
this.emit('respawn', execArgv, child);
}
if (ready) {
preloadModules(this, env);
registerLoader(this, this.extensions, env.configPath, env.cwd);
fn.call(this, env, argv);
}
}
}
}.bind(this));
}.bind(this)
);
};

function preloadModules(inst, env) {
4 changes: 2 additions & 2 deletions lib/build_config_name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function(opts) {
module.exports = function (opts) {
opts = opts || {};
var configName = opts.configName;
var extensions = opts.extensions;
@@ -11,7 +11,7 @@ module.exports = function(opts) {
if (!Array.isArray(extensions)) {
throw new Error('Please provide an array of valid extensions.');
}
return extensions.map(function(ext) {
return extensions.map(function (ext) {
return configName + ext;
});
};
2 changes: 1 addition & 1 deletion lib/file_search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var findup = require('findup-sync');

module.exports = function(search, paths) {
module.exports = function (search, paths) {
var path;
var len = paths.length;
for (var i = 0; i < len; i++) {
6 changes: 4 additions & 2 deletions lib/find_config.js
Original file line number Diff line number Diff line change
@@ -2,15 +2,17 @@ var fs = require('fs');
var path = require('path');
var fileSearch = require('./file_search');

module.exports = function(opts) {
module.exports = function (opts) {
opts = opts || {};
var configNameSearch = opts.configNameSearch;
var configPath = opts.configPath;
var searchPaths = opts.searchPaths;
// only search for a config if a path to one wasn't explicitly provided
if (!configPath) {
if (!Array.isArray(searchPaths)) {
throw new Error('Please provide an array of paths to search for config in.');
throw new Error(
'Please provide an array of paths to search for config in.'
);
}
if (!configNameSearch) {
throw new Error('Please provide a configNameSearch.');
2 changes: 1 addition & 1 deletion lib/find_cwd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var path = require('path');

module.exports = function(opts) {
module.exports = function (opts) {
if (!opts) {
opts = {};
}
1 change: 0 additions & 1 deletion lib/get_node_flags.js
Original file line number Diff line number Diff line change
@@ -27,4 +27,3 @@ module.exports = {
arrayOrFunction: arrayOrFunction,
fromReorderedArgv: fromReorderedArgv,
};

2 changes: 1 addition & 1 deletion lib/parse_options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var extend = require('extend');

module.exports = function(opts) {
module.exports = function (opts) {
var defaults = {
extensions: {
'.js': null,
10 changes: 6 additions & 4 deletions lib/register_loader.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var rechoir = require('rechoir');

module.exports = function(eventEmitter, extensions, configPath, cwd) {
module.exports = function (eventEmitter, extensions, configPath, cwd) {
extensions = extensions || {};

if (typeof configPath !== 'string') {
return;
}

var autoloads = rechoir.prepare(extensions, configPath, cwd, true);
if (autoloads instanceof Error) { // Only errors
autoloads.failures.forEach(function(failed) {
if (autoloads instanceof Error) {
// Only errors
autoloads.failures.forEach(function (failed) {
eventEmitter.emit('loader:failure', failed.moduleName, failed.error);
});
return;
}

if (!Array.isArray(autoloads)) { // Already required or no config.
if (!Array.isArray(autoloads)) {
// Already required or no config.
return;
}

2 changes: 1 addition & 1 deletion lib/silent_require.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function(path) {
module.exports = function (path) {
try {
return require(path);
} catch (e) {}
32 changes: 23 additions & 9 deletions test/build_config_name.js
Original file line number Diff line number Diff line change
@@ -3,29 +3,44 @@ var expect = require('expect');
var buildConfigName = require('../lib/build_config_name');

describe('buildConfigName', function () {

it('should throw if no configName is provided', function (done) {
expect(function () { buildConfigName(); }).toThrow();
expect(function () {
buildConfigName();
}).toThrow();
done();
});

it('should use configName directly if it is a regex', function (done) {
var configNameSearch = /mocha/;
expect(buildConfigName({ configName: configNameSearch })).toEqual([configNameSearch]);
expect(buildConfigName({ configName: configNameSearch })).toEqual([
configNameSearch,
]);
done();
});

it('should throw if no array of extensions are provided and config is not a regex already', function (done) {
expect(function () { buildConfigName({ configName: 'foo' }); }).toThrow();
expect(function () { buildConfigName({ configName: 'foo', extensions: '?' }); }).toThrow();
expect(function () { buildConfigName({ configName: 'foo', extensions: ['.js'] }); }).not.toThrow();
expect(function () {
buildConfigName({ configName: 'foo' });
}).toThrow();
expect(function () {
buildConfigName({ configName: 'foo', extensions: '?' });
}).toThrow();
expect(function () {
buildConfigName({ configName: 'foo', extensions: ['.js'] });
}).not.toThrow();
done();
});

it('should build an array of possible config names', function (done) {
var multiExtension = buildConfigName({ configName: 'foo', extensions: ['.js', '.coffee'] });
var multiExtension = buildConfigName({
configName: 'foo',
extensions: ['.js', '.coffee'],
});
expect(multiExtension).toEqual(['foo.js', 'foo.coffee']);
var singleExtension = buildConfigName({ configName: 'foo', extensions: ['.js'] });
var singleExtension = buildConfigName({
configName: 'foo',
extensions: ['.js'],
});
expect(singleExtension).toEqual(['foo.js']);
done();
});
@@ -62,5 +77,4 @@ describe('buildConfigName', function () {
}).toThrow();
done();
});

});
6 changes: 3 additions & 3 deletions test/file_search.js
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@ var expect = require('expect');
var fileSearch = require('../lib/file_search');

describe('fileSearch', function () {

it('should locate a file using findup from an array of possible base paths', function (done) {
expect(fileSearch('mochafile.js', ['../'])).toEqual(null);
expect(fileSearch('package.json', [process.cwd()])).toEqual(path.resolve(__dirname, '..', 'package.json'));
expect(fileSearch('package.json', [process.cwd()])).toEqual(
path.resolve(__dirname, '..', 'package.json')
);
done();
});

});
47 changes: 29 additions & 18 deletions test/find_config.js
Original file line number Diff line number Diff line change
@@ -5,34 +5,45 @@ var expect = require('expect');
var findConfig = require('../lib/find_config');

describe('findConfig', function () {

it('should throw if searchPaths or configNameRegex are empty when configName isn\'t explicltly provided', function (done) {
expect(function () { findConfig(); }).toThrow();
expect(function () { findConfig({ searchPaths: ['../'] }); }).toThrow();
expect(function () { findConfig({ configNameRegex: 'dude' }); }).toThrow();
it("should throw if searchPaths or configNameRegex are empty when configName isn't explicltly provided", function (done) {
expect(function () {
findConfig();
}).toThrow();
expect(function () {
findConfig({ searchPaths: ['../'] });
}).toThrow();
expect(function () {
findConfig({ configNameRegex: 'dude' });
}).toThrow();
done();
});

it('if configPath is explicitly provided, return the absolute path to the file or null if it doesn\'t actually exist', function (done) {
it("if configPath is explicitly provided, return the absolute path to the file or null if it doesn't actually exist", function (done) {
var configPath = path.resolve('test/fixtures/mochafile.js');
expect(findConfig({ configPath: configPath })).toEqual(configPath);
expect(findConfig({ configPath: 'path/to/nowhere' })).toEqual(null);
done();
});

it('should return the absolute path to the first config file found in searchPaths', function (done) {
expect(findConfig({
configNameSearch: ['mochafile.js', 'mochafile.coffee'],
searchPaths: ['test/fixtures'],
})).toEqual(path.resolve('test/fixtures/mochafile.js'));
expect(findConfig({
configNameSearch: ['mochafile.js', 'mochafile.coffee'],
searchPaths: ['test/fixtures/search_path', 'test/fixtures/coffee'],
})).toEqual(path.resolve('test/fixtures/search_path/mochafile.js'));
expect(findConfig({
configNameSearch: 'mochafile.js',
searchPaths: ['test/fixtures/search_path', 'test/fixtures/coffee'],
})).toEqual(path.resolve('test/fixtures/search_path/mochafile.js'));
expect(
findConfig({
configNameSearch: ['mochafile.js', 'mochafile.coffee'],
searchPaths: ['test/fixtures'],
})
).toEqual(path.resolve('test/fixtures/mochafile.js'));
expect(
findConfig({
configNameSearch: ['mochafile.js', 'mochafile.coffee'],
searchPaths: ['test/fixtures/search_path', 'test/fixtures/coffee'],
})
).toEqual(path.resolve('test/fixtures/search_path/mochafile.js'));
expect(
findConfig({
configNameSearch: 'mochafile.js',
searchPaths: ['test/fixtures/search_path', 'test/fixtures/coffee'],
})
).toEqual(path.resolve('test/fixtures/search_path/mochafile.js'));
done();
});

14 changes: 8 additions & 6 deletions test/find_cwd.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ var expect = require('expect');
var findCwd = require('../lib/find_cwd');

describe('findCwd', function () {

it('should return process.cwd if no options are passed', function (done) {
expect(findCwd()).toEqual(process.cwd());
done();
@@ -17,23 +16,26 @@ describe('findCwd', function () {
});

it('should return directory of config if configPath defined', function (done) {
expect(findCwd({ configPath: 'test/fixtures/mochafile.js' })).toEqual(path.resolve('test/fixtures'));
expect(findCwd({ configPath: 'test/fixtures/mochafile.js' })).toEqual(
path.resolve('test/fixtures')
);
done();
});

it('should return path from cwd if both it and configPath are defined', function (done) {
expect(findCwd({ cwd: '../', configPath: 'test/fixtures/mochafile.js' })).toEqual(path.resolve('../'));
expect(
findCwd({ cwd: '../', configPath: 'test/fixtures/mochafile.js' })
).toEqual(path.resolve('../'));
done();
});

it('should ignore cwd if it isn\'t a string', function (done) {
it("should ignore cwd if it isn't a string", function (done) {
expect(findCwd({ cwd: true })).toEqual(process.cwd());
done();
});

it('should ignore configPath if it isn\'t a string', function (done) {
it("should ignore configPath if it isn't a string", function (done) {
expect(findCwd({ configPath: true })).toEqual(process.cwd());
done();
});

});
2 changes: 1 addition & 1 deletion test/fixtures/configfiles/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"aaa":"AAA"}
{ "aaa": "AAA" }
16 changes: 7 additions & 9 deletions test/fixtures/configfiles/require-md.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.md'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-md';
};

}());
require.extensions['.md'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-md';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/configfiles/require-txt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.txt'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-txt';
};

}());
require.extensions['.txt'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-txt';
};
})();
6 changes: 3 additions & 3 deletions test/fixtures/developing_yourself/app1/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var Liftoff = require('../../../..');

var app1 = new Liftoff({
name: 'app1'
name: 'app1',
});

app1.prepare({}, function(env) {
app1.execute(env, function(env) {
app1.prepare({}, function (env) {
app1.execute(env, function (env) {
console.log(env.modulePackage);
console.log(env.modulePath);
console.log(env.cwd);
6 changes: 3 additions & 3 deletions test/fixtures/developing_yourself/app2/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var Liftoff = require('../../../..');

var app2 = new Liftoff({
name: 'app2'
name: 'app2',
});

app2.prepare({}, function(env) {
app2.execute(env, function(env) {
app2.prepare({}, function (env) {
app2.execute(env, function (env) {
console.log(JSON.stringify(env.modulePackage));
console.log(env.modulePath);
console.log(env.cwd);
6 changes: 3 additions & 3 deletions test/fixtures/developing_yourself/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var Liftoff = require('../../..');

var app0 = new Liftoff({
name: 'app0'
name: 'app0',
});

app0.prepare({}, function(env) {
app0.execute(env, function(env) {
app0.prepare({}, function (env) {
app0.execute(env, function (env) {
console.log(JSON.stringify(env.modulePackage));
console.log(env.modulePath);
console.log(env.cwd);
6 changes: 3 additions & 3 deletions test/fixtures/prepare-execute/nodeflags_only.js
Original file line number Diff line number Diff line change
@@ -4,13 +4,13 @@ var Test = new Liftoff({
name: 'test',
});

Test.on('respawn', function(execArgv) {
Test.on('respawn', function (execArgv) {
console.log('saw respawn', execArgv);
});

Test.prepare({}, function(env) {
Test.prepare({}, function (env) {
var forcedFlags = ['--lazy'];
Test.execute(env, forcedFlags, function(env, argv) {
Test.execute(env, forcedFlags, function (env, argv) {
console.error(argv.slice(1).join(' '));
});
});
8 changes: 4 additions & 4 deletions test/fixtures/prepare-execute/v8flags.js
Original file line number Diff line number Diff line change
@@ -2,14 +2,14 @@ var Liftoff = require('../../..');

var Test = new Liftoff({
name: 'test',
v8flags: ['--lazy']
v8flags: ['--lazy'],
});
Test.on('respawn', function(flags, proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Test.prepare({}, function(env) {
Test.execute(env, function(env) {
Test.prepare({}, function (env) {
Test.execute(env, function (env) {
console.error(process.execArgv.join(''));
});
});
6 changes: 3 additions & 3 deletions test/fixtures/prepare-execute/v8flags_config.js
Original file line number Diff line number Diff line change
@@ -5,13 +5,13 @@ var Test = new Liftoff({
v8flags: ['--harmony'],
});

Test.on('respawn', function(flags, proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn', flags);
});

Test.prepare({}, function(env) {
Test.prepare({}, function (env) {
var forcedFlags = ['--lazy'];
Test.execute(env, forcedFlags, function(env, argv) {
Test.execute(env, forcedFlags, function (env, argv) {
console.error(argv.slice(1).join(' '));
});
});
14 changes: 7 additions & 7 deletions test/fixtures/prepare-execute/v8flags_error.js
Original file line number Diff line number Diff line change
@@ -2,18 +2,18 @@ var Liftoff = require('../../..');

var Test = new Liftoff({
name: 'test',
v8flags: function(cb) {
process.nextTick(function() {
v8flags: function (cb) {
process.nextTick(function () {
cb(new Error('v8flags error!'), ['--lazy']);
})
}
});
},
});
Test.on('respawn', function(flags, proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Test.prepare({}, function(env) {
Test.execute(env, function(env) {
Test.prepare({}, function (env) {
Test.execute(env, function (env) {
console.error(process.execArgv.join(''));
});
});
14 changes: 7 additions & 7 deletions test/fixtures/prepare-execute/v8flags_function.js
Original file line number Diff line number Diff line change
@@ -2,18 +2,18 @@ var Liftoff = require('../../..');

var Test = new Liftoff({
name: 'test',
v8flags: function(cb) {
process.nextTick(function() {
v8flags: function (cb) {
process.nextTick(function () {
cb(null, ['--lazy']);
})
}
});
},
});
Test.on('respawn', function(flags, proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Test.prepare({}, function(env) {
Test.execute(env, function(env) {
Test.prepare({}, function (env) {
Test.execute(env, function (env) {
console.error(process.execArgv.join(''));
});
});
8 changes: 4 additions & 4 deletions test/fixtures/prepare-execute/v8flags_value.js
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@ var Liftoff = require('../../..');

var Test = new Liftoff({
name: 'test',
v8flags: ['--stack_size']
v8flags: ['--stack_size'],
});

Test.on('respawn', function(flags) {
Test.on('respawn', function (flags) {
console.error(flags.join(' '));
});

Test.prepare({}, function(env) {
Test.execute(env, function() {});
Test.prepare({}, function (env) {
Test.execute(env, function () {});
});
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-cfg.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.cfg'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-cfg';
};

}());
require.extensions['.cfg'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-cfg';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-conf.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.conf'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-conf';
};

}());
require.extensions['.conf'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-conf';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-b.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.b'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-b';
};

}());
require.extensions['.b'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-b';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-bc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.c'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-bc';
};

}());
require.extensions['.c'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-bc';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-cd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.d'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-cd';
};

}());
require.extensions['.d'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-cd';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-d.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.d'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-d';
};

}());
require.extensions['.d'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-d';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-ecd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.d'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-ecd';
};

}());
require.extensions['.d'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-ecd';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-file-fcd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.d'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-fcd';
};

}());
require.extensions['.d'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-file-fcd';
};
})();
16 changes: 7 additions & 9 deletions test/fixtures/register_loader/require-rc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
(function() {
(function () {
var path = require('path');

var path = require('path');

require.extensions['.rc'] = function(module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-rc';
};

}());
require.extensions['.rc'] = function (module, filepath) {
module.loaded = true;
module.exports = 'Load ' + path.basename(filepath) + ' by require-rc';
};
})();
23 changes: 13 additions & 10 deletions test/fixtures/respawn_and_require.js
Original file line number Diff line number Diff line change
@@ -5,19 +5,22 @@ const Test = new Liftoff({
v8flags: ['--harmony'],
});

Test.on('respawn', function(flags, proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn', flags);
});

Test.on('preload:success', function(name) {
Test.on('preload:success', function (name) {
console.log('preload:success', name);
});

Test.prepare({
preload: 'coffeescript/register',
}, function(env) {
var forcedFlags = ['--lazy'];
Test.execute(env, forcedFlags, function() {
console.log('execute');
});
});
Test.prepare(
{
preload: 'coffeescript/register',
},
function (env) {
var forcedFlags = ['--lazy'];
Test.execute(env, forcedFlags, function () {
console.log('execute');
});
}
);
72 changes: 55 additions & 17 deletions test/get_node_flags.js
Original file line number Diff line number Diff line change
@@ -3,32 +3,42 @@ var expect = require('expect');
var getNodeFlags = require('../lib/get_node_flags');

describe('getNodeFlags', function () {

describe('arrayOrFunction', function () {

it('should return the first argument when it is an array', function (done) {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction([], env)).toEqual([]);
expect(getNodeFlags.arrayOrFunction(['--lazy', '--use_strict', '--harmony'], env))
.toEqual(expect.arrayContaining(['--lazy', '--harmony', '--use_strict']));
expect(
getNodeFlags.arrayOrFunction(
['--lazy', '--use_strict', '--harmony'],
env
)
).toEqual(
expect.arrayContaining(['--lazy', '--harmony', '--use_strict'])
);
done();
});

it('should return the exection result of the first argument when it is a function', function (done) {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction(function () {
return [];
}, env)).toEqual([]);
expect(getNodeFlags.arrayOrFunction(function (arg) {
expect(arg).toEqual(env);
return ['--lazy', '--harmony'];
}, env)).toEqual(expect.arrayContaining(['--lazy', '--harmony']));
expect(
getNodeFlags.arrayOrFunction(function () {
return [];
}, env)
).toEqual([]);
expect(
getNodeFlags.arrayOrFunction(function (arg) {
expect(arg).toEqual(env);
return ['--lazy', '--harmony'];
}, env)
).toEqual(expect.arrayContaining(['--lazy', '--harmony']));
done();
});

it('should return an array which has an element of the first argument when the first argument is a string', function (done) {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction('--lazy', env)).toEqual(expect.arrayContaining(['--lazy']));
expect(getNodeFlags.arrayOrFunction('--lazy', env)).toEqual(
expect.arrayContaining(['--lazy'])
);
done();
});

@@ -47,25 +57,53 @@ describe('getNodeFlags', function () {
});

describe('fromReorderedArgv', function () {

it('should return only node flags from respawning arguments', function (done) {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--harmony', '--use_strict', './aaa/bbb/app.js', '--ccc', 'ddd', '-e', 'fff'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).toEqual(['--lazy', '--harmony', '--use_strict']);
var cmd = [
'node',
'--lazy',
'--harmony',
'--use_strict',
'./aaa/bbb/app.js',
'--ccc',
'ddd',
'-e',
'fff',
];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).toEqual([
'--lazy',
'--harmony',
'--use_strict',
]);
done();
});

it('should end node flags before "--"', function (done) {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--', '--harmony', '--use_strict', './aaa/bbb/app.js', '--ccc', 'ddd', '-e', 'fff'];
var cmd = [
'node',
'--lazy',
'--',
'--harmony',
'--use_strict',
'./aaa/bbb/app.js',
'--ccc',
'ddd',
'-e',
'fff',
];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).toEqual(['--lazy']);
done();
});

it('should return node flags when arguments are only node flags', function (done) {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--harmony', '--use_strict'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).toEqual(['--lazy', '--harmony', '--use_strict']);
expect(getNodeFlags.fromReorderedArgv(cmd, env)).toEqual([
'--lazy',
'--harmony',
'--use_strict',
]);
done();
});

302 changes: 183 additions & 119 deletions test/index.js

Large diffs are not rendered by default.

55 changes: 29 additions & 26 deletions test/parse_options.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ var parseOptions = require('../lib/parse_options');
var NAME = 'mocha';

describe('parseOptions', function () {

it('should auto-set processTitle, moduleName, & configFile if `name` is provided', function (done) {
var opts = parseOptions({ name: NAME });
expect(opts.processTitle).toEqual(NAME);
@@ -38,35 +37,39 @@ describe('parseOptions', function () {
done();
});

it('should use .processTitle/.configName/.moduleName preferencially',
function (done) {
var opts = parseOptions({
name: 'a',
processTitle: 'b',
configName: 'c',
moduleName: 'd',
});
expect(opts.processTitle).toEqual('b');
expect(opts.configName).toEqual('c');
expect(opts.moduleName).toEqual('d');
done();
it('should use .processTitle/.configName/.moduleName preferencially', function (done) {
var opts = parseOptions({
name: 'a',
processTitle: 'b',
configName: 'c',
moduleName: 'd',
});
expect(opts.processTitle).toEqual('b');
expect(opts.configName).toEqual('c');
expect(opts.moduleName).toEqual('d');
done();
});

it('should throw error if opts does not have .name and .moduleName',
function (done) {
expect(function () {
parseOptions({
processTitle: 'a',
configName: 'b',
});
}).toThrow();
done();
});
it('should throw error if opts does not have .name and .moduleName', function (done) {
expect(function () {
parseOptions({
processTitle: 'a',
configName: 'b',
});
}).toThrow();
done();
});

it('should throw error if opts is null or empty', function (done) {
expect(function () { parseOptions(null); }).toThrow();
expect(function () { parseOptions(undefined); }).toThrow();
expect(function () { parseOptions({}); }).toThrow();
expect(function () {
parseOptions(null);
}).toThrow();
expect(function () {
parseOptions(undefined);
}).toThrow();
expect(function () {
parseOptions({});
}).toThrow();
done();
});
});
30 changes: 15 additions & 15 deletions test/register_loader.js
Original file line number Diff line number Diff line change
@@ -18,11 +18,8 @@ function handlerNotEmit() {
}

describe('registerLoader', function () {

describe('register loader', function () {

it('Should emit a `loader:success` event when registering loader succeeds', function (done) {

var loaderPath = path.join(testDir, 'require-cfg.js');
var configPath = path.join(testDir, 'app.cfg');
var extensions = { '.cfg': loaderPath };
@@ -39,7 +36,6 @@ describe('registerLoader', function () {
});

it('Should emit only a `loader:success` event when registering loader failed and succeeds', function (done) {

var loaderPath = path.join(testDir, 'require-conf.js');
var configPath = path.join(testDir, 'app.conf');
var extensions = { '.conf': ['xxx', loaderPath] };
@@ -56,7 +52,6 @@ describe('registerLoader', function () {
});

it('Should emit a `loader:failure` event when loader is not found', function (done) {

var loaderPath = path.join(testDir, 'require-tmp.js');
var configPath = path.join(testDir, 'app.tmp');
var extensions = { '.tmp': ['xxx', loaderPath] };
@@ -103,7 +98,6 @@ describe('registerLoader', function () {

describe('cwd', function () {
it('Should use "cwd" as a base directory of loaded file path if specified', function (done) {

var loaderPath = path.join(testDir, 'require-rc.js');
var configPath = 'app.rc';
var extensions = { '.rc': loaderPath };
@@ -145,13 +139,13 @@ describe('registerLoader', function () {

registerLoader(app, 123, 'aaa/bbb.cfg');
registerLoader(app, true, 'aaa/bbb.cfg');
registerLoader(app, function () { }, 'aaa/bbb.cfg');
registerLoader(app, function () {}, 'aaa/bbb.cfg');
registerLoader(app, ['.rc', '.cfg'], 'aaa/bbb.cfg');

// .js is one of default extensions
registerLoader(app, 123, 'aaa/bbb.js');
registerLoader(app, true, 'aaa/bbb.js');
registerLoader(app, function () { }, 'aaa/bbb.js');
registerLoader(app, function () {}, 'aaa/bbb.js');
registerLoader(app, ['.js', '.json'], 'aaa/bbb.js');
done();
});
@@ -204,7 +198,7 @@ describe('registerLoader', function () {
registerLoader(app, extensions0, 123);
registerLoader(app, extensions0, ['aaa', 'bbb']);
registerLoader(app, extensions1, {});
registerLoader(app, extensions1, function () { });
registerLoader(app, extensions1, function () {});
done();
});

@@ -261,7 +255,9 @@ describe('registerLoader', function () {
app.on('loader:failure', handlerNotEmit);
app.on('loader:success', function (moduleName /* , module */) {
expect(moduleName).toEqual(loaderPath);
expect(require(configPath)).toEqual('Load file.a.b.c by require-file-bc');
expect(require(configPath)).toEqual(
'Load file.a.b.c by require-file-bc'
);
done();
});

@@ -293,17 +289,23 @@ describe('registerLoader', function () {
switch (count) {
case 0: {
expect(moduleName).toEqual(loaderPathCD);
expect(require(configPathCD)).toEqual('Load file.a.b.c.d by require-file-cd');
expect(require(configPathCD)).toEqual(
'Load file.a.b.c.d by require-file-cd'
);
break;
}
case 1: {
expect(moduleName).toEqual(loaderPathECD);
expect(require(configPathECD)).toEqual('Load file.a.e.c.d by require-file-ecd');
expect(require(configPathECD)).toEqual(
'Load file.a.e.c.d by require-file-ecd'
);
break;
}
case 2: {
expect(moduleName).toEqual(loaderPathFCD);
expect(require(configPathFCD)).toEqual('Load file.a.f.c.d by require-file-fcd');
expect(require(configPathFCD)).toEqual(
'Load file.a.f.c.d by require-file-fcd'
);
done();
break;
}
@@ -317,6 +319,4 @@ describe('registerLoader', function () {
registerLoader(app, extensions, configPathFCD);
});
});

});

5 changes: 3 additions & 2 deletions test/silent_require.js
Original file line number Diff line number Diff line change
@@ -5,9 +5,10 @@ var expect = require('expect');
var silentRequire = require('../lib/silent_require');

describe('silentRequire', function () {

it('should require a file', function (done) {
expect(silentRequire(path.resolve('./package'))).toEqual(require('../package'));
expect(silentRequire(path.resolve('./package'))).toEqual(
require('../package')
);
done();
});