Files
appium/packages/support/test/unit/process.spec.js
T
Christopher Hiller 3ce313ebb9 chore(support): ditch gulp
This PR builds and tests `@appium/support` without Gulp.  Much like the changes to `appium`:

- Test files reorganized and renamed
- Tests run via `mocha` proper using `@babel/register` (no build needed)
- Build run via `@babel/cli`
2022-03-10 11:12:14 -08:00

98 lines
3.1 KiB
JavaScript

import * as teenProcess from 'teen_process';
import { createSandbox } from 'sinon';
import { process } from '../../lib/index.js';
import { retryInterval } from 'asyncbox';
const SubProcess = teenProcess.SubProcess;
describe('process', function () {
let sandbox;
beforeEach(function () {
sandbox = createSandbox();
});
afterEach(function () {
sandbox.restore();
});
describe('getProcessIds', function () {
let proc;
before(async function () {
proc = new SubProcess('tail', ['-f', __filename]);
await proc.start();
});
after(async function () {
await proc.stop();
});
it('should get return an array for existing process', async function () {
let pids = await process.getProcessIds('tail');
pids.should.be.an.instanceof(Array);
});
it('should get process identifiers for existing process', async function () {
let pids = await process.getProcessIds('tail');
pids.should.have.length.at.least(1);
});
it('should get an empty array when the process does not exist', async function () {
let pids = await process.getProcessIds('sadfgasdfasdf');
pids.should.have.length(0);
});
it('should throw an error if pgrep fails', async function () {
let tpMock = sandbox.mock(teenProcess);
tpMock.expects('exec').throws({message: 'Oops', code: 2});
await process.getProcessIds('tail').should.eventually.be.rejectedWith(/Oops/);
tpMock.restore();
});
});
describe('killProcess', function () {
let proc;
beforeEach(async function () {
proc = new SubProcess('tail', ['-f', __filename]);
await proc.start();
});
afterEach(async function () {
if (proc.isRunning) {
await proc.stop();
}
});
it('should kill process that is running', async function () {
proc.isRunning.should.be.true;
await process.killProcess('tail');
// it may take a moment to actually be registered as killed
await retryInterval(10, 100, async () => { // eslint-disable-line require-await
proc.isRunning.should.be.false;
});
});
it('should do nothing if the process does not exist', async function () {
proc.isRunning.should.be.true;
await process.killProcess('asdfasdfasdf');
await retryInterval(10, 100, async () => { // eslint-disable-line require-await
proc.isRunning.should.be.false;
}).should.eventually.be.rejected;
});
it('should throw an error if pgrep fails', async function () {
let tpMock = sandbox.mock(teenProcess);
tpMock.expects('exec').throws({message: 'Oops', code: 2});
await process.killProcess('tail').should.eventually.be.rejectedWith(/Oops/);
tpMock.restore();
});
it('should throw an error if pkill fails', async function () {
let tpMock = sandbox.mock(teenProcess);
tpMock.expects('exec').twice()
.onFirstCall().returns({stdout: '42\n'})
.onSecondCall().throws({message: 'Oops', code: 2});
await process.killProcess('tail').should.eventually.be.rejectedWith(/Oops/);
tpMock.restore();
});
});
});