Files
appium/packages/support/test/unit/process.spec.js
Kazuaki Matsuo a6c4fad26b chore: bump teen-process (#21831)
* chore: bump teen-process

* remove renovate

* update tests

* update mock in e2e

* remove the custom rule in renovate

* remove unused tpMock

* remove Custom Rules completely
2025-12-23 09:30:32 -08:00

106 lines
3.3 KiB
JavaScript

import * as teenProcess from 'teen_process';
import {createSandbox} from 'sinon';
import {process, system} from '../../lib';
import {retryInterval} from 'asyncbox';
const SubProcess = teenProcess.SubProcess;
describe('process', function () {
let sandbox;
before(async function () {
const chai = await import('chai');
const chaiAsPromised = await import('chai-as-promised');
chai.use(chaiAsPromised.default);
chai.should();
});
beforeEach(function () {
sandbox = createSandbox();
});
afterEach(function () {
sandbox.verify();
sandbox.restore();
});
describe('getProcessIds', function () {
let proc;
before(async function () {
if (system.isWindows()) {
return this.skip();
}
proc = new SubProcess('tail', ['-f', __filename]);
await proc.start();
});
after(async function () {
if (proc) {
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 () {
sandbox.stub(teenProcess, 'exec').get(() => sandbox.stub().throws({message: 'Oops', code: 2}));
await process.getProcessIds('tail').should.eventually.be.rejectedWith(/Oops/);
});
});
describe('killProcess', function () {
let proc;
before(function () {
if (system.isWindows()) {
return this.skip();
}
});
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 () => {
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 () => {
proc.isRunning.should.be.false;
}).should.eventually.be.rejected;
});
it('should throw an error if pgrep fails', async function () {
sandbox.stub(teenProcess, 'exec').get(() => sandbox.stub().throws({message: 'Oops', code: 2}));
await process.killProcess('tail').should.eventually.be.rejectedWith(/Oops/);
});
it('should throw an error if pkill fails', async function () {
const innerExecStub = sandbox.stub();
innerExecStub.returns({stdout: '42\n'});
innerExecStub.throws({message: 'Oops', code: 2});
sandbox.stub(teenProcess, 'exec').get(() => innerExecStub);
await process.killProcess('tail').should.eventually.be.rejectedWith(/Oops/);
});
});
});