shell: make require take a tag

This commit is contained in:
Girish Ramakrishnan
2024-10-14 19:10:31 +02:00
parent 02823c4158
commit a9e1d7641d
27 changed files with 162 additions and 156 deletions

View File

@@ -8,70 +8,70 @@ const BoxError = require('../boxerror.js'),
expect = require('expect.js'),
path = require('path'),
safe = require('safetydance'),
shell = require('../shell.js');
shell = require('../shell.js')('test');
describe('shell', function () {
describe('execArgs', function () {
it('can run valid program', async function () {
await shell.execArgs('test', 'ls', [ '-l' ], {});
await shell.execArgs('ls', [ '-l' ], {});
});
it('fails on invalid program', async function () {
const [error] = await safe(shell.execArgs('test', 'randomprogram', [ ], {}));
const [error] = await safe(shell.execArgs('randomprogram', [ ], {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('fails on failing program', async function () {
const [error] = await safe(shell.execArgs('test', '/usr/bin/false', [ ], {}));
const [error] = await safe(shell.execArgs('/usr/bin/false', [ ], {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
});
describe('sudo', function () {
it('cannot sudo invalid program', function (done) {
shell.sudo('test', [ 'randomprogram' ], {}, function (error) {
shell.sudo([ 'randomprogram' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
it('can sudo valid program', function (done) {
let RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
shell.sudo('test', [ RELOAD_NGINX_CMD, 'nginx' ], {}, function (error) {
const RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
shell.sudo([ RELOAD_NGINX_CMD, 'nginx' ], {}, function (error) {
expect(error).to.be.ok();
done();
});
});
it('can run valid program (promises)', async function () {
let RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
await safe(shell.promises.sudo('test', [ RELOAD_NGINX_CMD, 'nginx' ], {}));
const RELOAD_NGINX_CMD = path.join(__dirname, '../src/scripts/restartservice.sh');
await safe(shell.promises.sudo([ RELOAD_NGINX_CMD, 'nginx' ], {}));
});
});
describe('exec', function () {
it('exec throws for invalid program', async function () {
const [error] = await safe(shell.exec('test', 'cannotexist', {}));
const [error] = await safe(shell.exec('cannotexist', {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('exec throws for failed program', async function () {
const [error] = await safe(shell.exec('test', 'false', {}));
const [error] = await safe(shell.exec('false', {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('exec times out properly', async function () {
const [error] = await safe(shell.exec('sleeping', 'sleep 20', { timeout: 1000 }));
const [error] = await safe(shell.exec('sleep 20', { timeout: 1000 }));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('cannot exec a shell program by default', async function () {
const [error] = await safe(shell.exec('test', 'ls -l | wc -c', {}));
const [error] = await safe(shell.exec('ls -l | wc -c', {}));
expect(error.reason).to.be(BoxError.SHELL_ERROR);
});
it('cannot exec a shell program b', async function () {
await shell.exec('test', 'ls -l | wc -c', { shell: '/bin/bash' });
await shell.exec('ls -l | wc -c', { shell: '/bin/bash' });
});
});
});