shelljs/test/utils/utils.js
Nate Fischer 38b57c8942 chore: add skipOnWin and skipOnUnix test helpers (#746)
This adds `skipOnWin` and `skipOnUnix` to help us manage our platform-dependent
tests. These methods give a nice warning message when we skip tests. We may also
consider adding warnings when running platform-dependent tests.

Part of the motivation for this is if we ever update to AVA v0.19. This version
requires at least one assertion per test case. While this could be disabled with
an AVA setting, we instead benefit from warnings for any case when we
unintentionally skip assertions.

This adds chalk as a dev dependency to enable colored messages.
2017-06-18 11:51:01 -07:00

71 lines
2.0 KiB
JavaScript

const child = require('child_process');
const path = require('path');
const chalk = require('chalk');
const common = require('../../src/common');
function numLines(str) {
return typeof str === 'string' ? (str.match(/\n/g) || []).length + 1 : 0;
}
exports.numLines = numLines;
function getTempDir() {
// a very random directory
return ('tmp' + Math.random() + Math.random()).replace(/\./g, '');
}
exports.getTempDir = getTempDir;
// On Windows, symlinks for files need admin permissions. This helper
// skips certain tests if we are on Windows and got an EPERM error
function skipOnWinForEPERM(action, testCase) {
const ret = action();
const error = ret.code;
const isWindows = process.platform === 'win32';
if (isWindows && error && /EPERM:/.test(error)) {
console.warn('Got EPERM when testing symlinks on Windows. Assuming non-admin environment and skipping test.');
} else {
testCase();
}
}
exports.skipOnWinForEPERM = skipOnWinForEPERM;
function runScript(script, cb) {
child.execFile(common.config.execPath, ['-e', script], cb);
}
exports.runScript = runScript;
function sleep(time) {
const testDirectoryPath = path.dirname(__dirname);
child.execFileSync(common.config.execPath, [
path.join(testDirectoryPath, 'resources', 'exec', 'slow.js'),
time.toString(),
]);
}
exports.sleep = sleep;
function mkfifo(dir) {
if (process.platform !== 'win32') {
const fifo = dir + 'fifo';
child.execFileSync('mkfifo', [fifo]);
return fifo;
}
return null;
}
exports.mkfifo = mkfifo;
function skipIfTrue(booleanValue, t, closure) {
if (booleanValue) {
console.warn(
chalk.yellow('Warning: skipping platform-dependent test ') +
chalk.bold.white(`'${t._test.title}'`)
);
t.truthy(true); // dummy assertion to satisfy ava v0.19+
} else {
closure();
}
}
exports.skipOnUnix = skipIfTrue.bind(module.exports, process.platform !== 'win32');
exports.skipOnWin = skipIfTrue.bind(module.exports, process.platform === 'win32');