Skip to content

Commit

Permalink
fix: Support jumping between node.js versions (#7)
Browse files Browse the repository at this point in the history
Use `NODE_PATH` and use bare filename for `--require` if it is needed in
node.js < 12 or if node.js 12+ would otherwise require quoting the
require path.  This should avoid a crash when `node-preload` is setup in
node.js 12 but then a test is run under node.js < 12.  It should also
ensure compatibility when setup is performed in node.js < 12 and tests
are later run under node.js 12+.

Ref istanbuljs/nyc#1246
  • Loading branch information
coreyfarrell committed Dec 22, 2019
1 parent 62de5ed commit 68950a0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 84 deletions.
27 changes: 0 additions & 27 deletions generate-require-legacy.js

This file was deleted.

12 changes: 0 additions & 12 deletions generate-require-modern.js

This file was deleted.

31 changes: 28 additions & 3 deletions generate-require.js
@@ -1,6 +1,31 @@
'use strict';

/* istanbul ignore next: version specific branching */
const requireType = Number(process.versions.node.split('.')[0]) < 12 ? 'legacy' : 'modern';
const path = require('path');

module.exports = require(`./generate-require-${requireType}.js`);
const needsPathRegExp = /[\\ "]/;

const needsPathEnv = dir => needsPathRegExp.test(dir);

function generateRequire(filename) {
if (needsPathEnv(filename)) {
return `--require ${path.basename(filename)}`;
}

return `--require ${filename}`;
}

function processNodePath(value) {
const dir = path.dirname(require.resolve('./preload-path/node-preload.js'));
const existing = value === '' ? [] : value.split(path.delimiter);
if (existing.includes(dir)) {
return value;
}

return existing.concat(dir).join(path.delimiter);
}

module.exports = {
generateRequire,
processNodePath,
needsPathEnv
};
2 changes: 1 addition & 1 deletion nyc.config.js
Expand Up @@ -2,7 +2,7 @@

module.exports = {
all: true,
checkCoverage: true,
checkCoverage: process.platform !== 'win32',
functions: 100,
lines: 100,
statements: 100,
Expand Down
32 changes: 0 additions & 32 deletions test/generate-require-modern.js

This file was deleted.

21 changes: 12 additions & 9 deletions test/generate-require-legacy.js → test/generate-require.js
Expand Up @@ -2,30 +2,33 @@

const path = require('path');
const {test} = require('tape');
const legacyRequire = require('../generate-require-legacy.js');
const legacyRequire = require('../generate-require.js');

const {generateRequire, processNodePath, needsPathEnv} = legacyRequire;
const noSpaceFile = path.resolve('/dir/file.js');
const plainFile = path.resolve('/dir/file.js');
const spaceFile = path.resolve('/space dir/file.js');
const quoteFile = path.resolve('/"quoted"/file.js');
const backslashFile = path.resolve('/back\\slash/file.js');

const isWindows = process.platform === 'win32';

test('exports', t => {
t.equal(typeof legacyRequire, 'object');
t.same(Object.keys(legacyRequire).sort(), ['generateRequire', 'needsPathEnv', 'processNodePath']);
t.equal(typeof generateRequire, 'function');
t.equal(typeof processNodePath, 'function');
t.equal(needsPathEnv(noSpaceFile), false);
t.equal(needsPathEnv(plainFile), isWindows);
t.equal(needsPathEnv(spaceFile), true);
t.equal(needsPathEnv(quoteFile), true);
t.equal(needsPathEnv(backslashFile), true);
t.end();
});

test('generateRequire', t => {
t.equal(generateRequire(noSpaceFile), `--require ${noSpaceFile}`);
t.equal(generateRequire(plainFile), `--require ${isWindows ? path.basename(plainFile) : plainFile}`);
t.equal(generateRequire(spaceFile), `--require ${path.basename(spaceFile)}`);

if (process.platform !== 'win32') {
const quoteFile = path.resolve('/dir/file"quote"nospace.js');
t.equal(generateRequire(quoteFile), `--require ${quoteFile}`);
}
t.equal(generateRequire(quoteFile), `--require ${path.basename(quoteFile)}`);
t.equal(generateRequire(backslashFile), `--require ${path.basename(backslashFile)}`);

t.end();
});
Expand Down

0 comments on commit 68950a0

Please sign in to comment.