Skip to content

Commit

Permalink
test: use both -r and --require in preload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed May 28, 2015
1 parent a77c330 commit 7c48f9c
Showing 1 changed file with 69 additions and 70 deletions.
139 changes: 69 additions & 70 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ var common = require('../common'),

var nodeBinary = process.argv[0];

var preloadOption = function(preloads) {
var option = '';
preloads.forEach(function(preload, index) {
// TODO: randomly pick -r or --require
option += '-r ' + preload + ' ';
});
return option;
var preloadOption = function(preloads, flag) {
return preloads.reduce(function(option, preload) {
return option + flag + ' ' + preload + ' ';
}, '');
};

var fixture = function(name) {
Expand All @@ -24,72 +21,74 @@ var fixtureB = fixture('printB.js');
var fixtureC = fixture('printC.js');
var fixtureThrows = fixture('throws_error4.js');

// test preloading a single module works
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA]) + ' '
+ fixtureB,
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\n');
});
['-r', '--require'].forEach(function (flag) {
// test preloading a single module works
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA], flag) + ' '
+ fixtureB,
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\n');
});

// test preloading multiple modules works
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA, fixtureB]) + ' '
+ fixtureC,
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\nC\n');
});
// test preloading multiple modules works
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA, fixtureB], flag) + ' '
+ fixtureC,
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\nC\n');
});

// test that preloading a throwing module aborts
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA, fixtureThrows]) + ' '
+ fixtureB,
function(err, stdout, stderr) {
if (err) {
assert.equal(stdout, 'A\n');
} else {
throw new Error('Preload should have failed');
}
});
// test that preloading a throwing module aborts
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA, fixtureThrows], flag) + ' '
+ fixtureB,
function(err, stdout, stderr) {
if (err) {
assert.equal(stdout, 'A\n');
} else {
throw new Error('Preload should have failed');
}
});

// test that preload can be used with --eval
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA])
+ '-e "console.log(\'hello\');"',
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nhello\n');
});
// test that preload can be used with --eval
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA], flag)
+ '-e "console.log(\'hello\');"',
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nhello\n');
});

// test that preload placement at other points in the cmdline
// also test that duplicated preload only gets loaded once
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA])
+ '-e "console.log(\'hello\');" '
+ preloadOption([fixtureA, fixtureB]),
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\nhello\n');
});
// test that preload placement at other points in the cmdline
// also test that duplicated preload only gets loaded once
child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureA], flag)
+ '-e "console.log(\'hello\');" '
+ preloadOption([fixtureA, fixtureB], flag),
function(err, stdout, stderr) {
if (err) throw err;
assert.equal(stdout, 'A\nB\nhello\n');
});

child_process.exec(nodeBinary + ' '
+ '--require ' + fixture('cluster-preload.js') + ' '
+ fixture('cluster-preload-test.js'),
function(err, stdout, stderr) {
if (err) throw err;
assert.ok(/worker terminated with code 43/.test(stdout));
});
child_process.exec(nodeBinary + ' '
+ flag + ' ' + fixture('cluster-preload.js') + ' '
+ fixture('cluster-preload-test.js'),
function(err, stdout, stderr) {
if (err) throw err;
assert.ok(/worker terminated with code 43/.test(stdout));
});

// https://github.com/nodejs/io.js/issues/1691
var originalCwd = process.cwd();
process.chdir(path.join(__dirname, '../fixtures/'));
child_process.exec(nodeBinary + ' '
+ '--expose_debug_as=v8debug '
+ '--require ' + fixture('cluster-preload.js') + ' '
+ 'cluster-preload-test.js',
function(err, stdout, stderr) {
if (err) throw err;
assert.ok(/worker terminated with code 43/.test(stdout));
});
// https://github.com/nodejs/io.js/issues/1691
var originalCwd = process.cwd();
process.chdir(path.join(__dirname, '../fixtures/'));
child_process.exec(nodeBinary + ' '
+ '--expose_debug_as=v8debug '
+ flag + ' ' + fixture('cluster-preload.js') + ' '
+ 'cluster-preload-test.js',
function(err, stdout, stderr) {
if (err) throw err;
assert.ok(/worker terminated with code 43/.test(stdout));
});
});

0 comments on commit 7c48f9c

Please sign in to comment.