shelljs/test/set.js
Nate Fischer 1a31356343 refactor: use require instead of import
No change to logic. This swaps over tests to use require() since
everything is currently designed for the commonjs module system.
2025-04-07 22:44:24 -07:00

78 lines
2.6 KiB
JavaScript

const test = require('ava');
const shell = require('..');
const utils = require('./utils/utils');
const oldConfigSilent = shell.config.silent;
const uncaughtErrorExitCode = 1;
test.beforeEach(t => {
t.context.tmp = utils.getTempDir();
shell.config.resetForTesting();
shell.cp('-r', 'test/resources', t.context.tmp);
});
test.afterEach.always(t => {
shell.rm('-rf', t.context.tmp);
});
//
// Valids
//
test('initial values', t => {
t.false(oldConfigSilent);
t.false(shell.config.verbose);
t.false(shell.config.fatal);
t.false(shell.config.noglob);
});
test('default behavior', t => {
const result = shell.exec(JSON.stringify(shell.config.execPath) + ` -e "require('./global'); ls('file_doesnt_exist'); echo(1234);"`);
t.is(result.code, 0);
t.is(result.stdout, '1234\n');
t.is(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
});
test('set -e', t => {
const result = shell.exec(JSON.stringify(shell.config.execPath) + ` -e "require('./global'); set('-e'); ls('file_doesnt_exist'); echo(1234);"`);
t.is(result.code, uncaughtErrorExitCode);
t.is(result.stdout, '');
t.truthy(result.stderr.includes('Error: ls: no such file or directory: file_doesnt_exist'));
});
test('set -v', t => {
const result = shell.exec(JSON.stringify(shell.config.execPath) + ` -e "require('./global'); set('-v'); ls('file_doesnt_exist'); echo(1234);"`);
t.is(result.code, 0);
t.is(result.stdout, '1234\n');
t.is(
result.stderr,
'ls file_doesnt_exist\nls: no such file or directory: file_doesnt_exist\necho 1234\n'
);
});
test('set -ev', t => {
const result = shell.exec(JSON.stringify(shell.config.execPath) + ` -e "require('./global'); set('-ev'); ls('file_doesnt_exist'); echo(1234);"`);
t.is(result.code, uncaughtErrorExitCode);
t.is(result.stdout, '');
t.truthy(result.stderr.includes('Error: ls: no such file or directory: file_doesnt_exist'));
t.truthy(result.stderr.includes('ls file_doesnt_exist\n'));
t.falsy(result.stderr.includes('echo 1234\n'));
});
test('set -e, set +e', t => {
const result = shell.exec(JSON.stringify(shell.config.execPath) + ` -e "require('./global'); set('-e'); set('+e'); ls('file_doesnt_exist'); echo(1234);"`);
t.is(result.code, 0);
t.is(result.stdout, '1234\n');
t.is(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
});
test('set -f', t => {
shell.set('-f'); // disable globbing
shell.rm(`${t.context.tmp}/*.txt`);
t.truthy(shell.error()); // file '*.txt' doesn't exist, so rm() fails
shell.set('+f');
shell.rm(`${t.context.tmp}/*.txt`);
t.falsy(shell.error()); // globbing works, so rm succeeds
});