mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
* fix: convert error output to be consistent cross-platform The error output produced by `shell.error()` or `result.stderr` should not be inconsistent between platforms. This ensures that path separators are always printed by ShellJS as `/` instead of as `\` on Windows. This should allow scripts using ShellJS to be more consistent cross-platform. We were not previously relying on error output to always be consistent-- only checking its truthiness. Since this was not part of our tested API, it should be reasonable to change this and release in a patch. Fixes #681 * Fix broken pushd test case * Fix TODO in a test case
331 lines
8.5 KiB
JavaScript
331 lines
8.5 KiB
JavaScript
import path from 'path';
|
|
|
|
import test from 'ava';
|
|
|
|
import shell from '..';
|
|
|
|
const rootDir = path.resolve();
|
|
|
|
function reset() {
|
|
shell.dirs('-c');
|
|
shell.cd(rootDir);
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
shell.config.resetForTesting();
|
|
reset();
|
|
});
|
|
|
|
test.after.always(() => {
|
|
reset();
|
|
});
|
|
|
|
//
|
|
// Valids
|
|
//
|
|
|
|
test('Push valid directories', t => {
|
|
const trail = shell.pushd('resources/pushd');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('Two directories', t => {
|
|
shell.pushd('resources/pushd');
|
|
const trail = shell.pushd('a');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('Three directories', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
const trail = shell.pushd('../b');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('Four directories', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
const trail = shell.pushd('c');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('Push stuff around with positive indices', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('+0');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('+1 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('+1');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
]);
|
|
});
|
|
|
|
test('+2 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('+2');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
]);
|
|
});
|
|
|
|
test('+3 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('+3');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
]);
|
|
});
|
|
|
|
test('+4 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('+4');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
]);
|
|
});
|
|
|
|
test('Push stuff around with negative indices', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('-0');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
]);
|
|
});
|
|
|
|
test('-1 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('-1');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
]);
|
|
});
|
|
|
|
test('-2 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('-2');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
]);
|
|
});
|
|
|
|
test('-3 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('-3');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
]);
|
|
});
|
|
|
|
test('-4 option', t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd('a');
|
|
shell.pushd('../b');
|
|
shell.pushd('c');
|
|
const trail = shell.pushd('-4');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
path.resolve(rootDir, 'resources/pushd/b/c'),
|
|
path.resolve(rootDir, 'resources/pushd/b'),
|
|
path.resolve(rootDir, 'resources/pushd/a'),
|
|
path.resolve(rootDir, 'resources/pushd'),
|
|
rootDir,
|
|
]);
|
|
});
|
|
|
|
test('Push without changing directory or resolving paths', t => {
|
|
const trail = shell.pushd('-n', 'resources/pushd');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
rootDir,
|
|
'resources/pushd',
|
|
]);
|
|
});
|
|
|
|
test('Using the -n option with a non-empty stack', t => {
|
|
shell.pushd('-n', 'resources/pushd');
|
|
const trail = shell.pushd('-n', 'resources/pushd/a');
|
|
t.falsy(shell.error());
|
|
t.is(process.cwd(), trail[0]);
|
|
t.deepEqual(trail, [
|
|
rootDir,
|
|
'resources/pushd/a',
|
|
'resources/pushd',
|
|
]);
|
|
});
|
|
|
|
test('Push invalid directory', t => {
|
|
const oldCwd = process.cwd();
|
|
shell.pushd('does/not/exist');
|
|
t.is(
|
|
shell.error(),
|
|
`pushd: no such file or directory: ${path.resolve('.', 'does/not/exist')
|
|
.replace(/\\/g, '/')}`
|
|
);
|
|
t.is(process.cwd(), oldCwd);
|
|
});
|
|
|
|
test(
|
|
'Push without args swaps top two directories when stack length is 2',
|
|
t => {
|
|
let trail = shell.pushd('resources/pushd');
|
|
t.falsy(shell.error());
|
|
t.is(trail.length, 2);
|
|
t.is(path.relative(rootDir, trail[0]), path.join('resources', 'pushd'));
|
|
t.is(trail[1], rootDir);
|
|
t.is(process.cwd(), trail[0]);
|
|
trail = shell.pushd();
|
|
t.falsy(shell.error());
|
|
t.is(trail.length, 2);
|
|
t.is(trail[0], rootDir);
|
|
t.is(path.relative(rootDir, trail[1]), path.join('resources', 'pushd'));
|
|
t.is(process.cwd(), trail[0]);
|
|
}
|
|
);
|
|
|
|
test(
|
|
'Push without args swaps top two directories for larger stacks',
|
|
t => {
|
|
shell.pushd('resources/pushd');
|
|
shell.pushd();
|
|
const trail = shell.pushd('resources/pushd/a');
|
|
t.falsy(shell.error());
|
|
t.is(trail.length, 3);
|
|
t.is(path.relative(rootDir, trail[0]), path.join('resources', 'pushd', 'a'));
|
|
t.is(trail[1], rootDir);
|
|
t.is(path.relative(rootDir, trail[2]), path.join('resources', 'pushd'));
|
|
t.is(process.cwd(), trail[0]);
|
|
}
|
|
);
|
|
|
|
test('Pushing with no args', t => {
|
|
shell.pushd('-n', 'resources/pushd');
|
|
shell.pushd('resources/pushd/a');
|
|
const trail = shell.pushd();
|
|
t.falsy(shell.error());
|
|
t.is(trail.length, 3);
|
|
t.is(trail[0], rootDir);
|
|
t.is(path.relative(rootDir, trail[1]), path.join('resources', 'pushd', 'a'));
|
|
t.is(path.relative(rootDir, trail[2]), path.join('resources', 'pushd'));
|
|
t.is(process.cwd(), trail[0]);
|
|
});
|
|
|
|
test('Push without arguments invalid when stack is empty', t => {
|
|
shell.pushd();
|
|
t.is(shell.error(), 'pushd: no other directory');
|
|
});
|