Skip to content

Commit

Permalink
fix: switched exec from wrapping execFile to spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
emmacasolin committed Aug 9, 2022
1 parent fb66f39 commit d25120c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
10 changes: 5 additions & 5 deletions tests/nat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ChildProcess } from 'child_process';
import os from 'os';
import fs from 'fs';
import path from 'path';
import child_process from 'child_process';
import childProcess from 'child_process';
import readline from 'readline';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import * as execUtils from '../utils/exec';
Expand Down Expand Up @@ -147,7 +147,7 @@ function createUserNamespace(
logger: Logger = new Logger(createUserNamespace.name),
): ChildProcess {
logger.info('unshare --user --map-root-user');
const subprocess = child_process.spawn(
const subprocess = childProcess.spawn(
'unshare',
['--user', '--map-root-user'],
{
Expand All @@ -172,7 +172,7 @@ function createNetworkNamespace(
logger.info(
`nsenter --target ${usrnsPid.toString()} --user --preserve-credentials unshare --net`,
);
const subprocess = child_process.spawn(
const subprocess = childProcess.spawn(
'nsenter',
[
'--target',
Expand Down Expand Up @@ -1405,9 +1405,9 @@ async function setupNAT(

export {
nsenter,
setupNAT,
setupNATWithSeedNode,
createUserNamespace,
createNetworkNamespace,
setupNetworkNamespaceInterfaces,
setupNAT,
setupNATWithSeedNode,
};
57 changes: 32 additions & 25 deletions tests/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,40 @@ const generateDockerArgs = (mountPath: string) => [
];

/**
* Wrapper for execFile to make it asynchronous and non-blocking
* Execute general (non-Polykey) commands
*/
async function exec(
command: string,
args: Array<string> = [],
opts: ExecOpts = { env: {} },
): Promise<{
stdout: string;
stderr: string;
}> {
const env = {
...process.env,
...opts.env,
};
return new Promise((resolve, reject) => {
child_process.execFile(
command,
args,
{ windowsHide: true },
(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
return resolve({
stdout,
stderr,
});
}
},
);
let stdout = '',
stderr = '';
const subprocess = child_process.spawn(command, args, {
env,
windowsHide: true,
shell: false,
});
subprocess.stdout.on('data', (data) => {
stdout += data.toString();
});
subprocess.stderr.on('data', (data) => {
stderr += data.toString();
});
subprocess.on('exit', () => {
resolve({ stdout, stderr });
});
subprocess.on('error', (e) => {
reject(e);
});
});
}

Expand All @@ -101,8 +110,6 @@ async function pk(args: Array<string>): Promise<any> {
* Both stdout and stderr are the entire output including newlines
* This can only be used serially, because the mocks it relies on are global singletons
* If it is used concurrently, the mocking side-effects can conflict
* @param env Augments env for command execution
* @param cwd Defaults to temporary directory
*/
async function pkStdio(
args: Array<string> = [],
Expand Down Expand Up @@ -607,19 +614,19 @@ function escapeShellArgs(arg: string): string {
export {
tsConfigPath,
polykeyPath,
exec,
pk,
pkStdio,
pkExec,
pkSpawn,
pkExpect,
pkStdio,
exec,
pk,
setupTestAgent,
processExit,
expectProcessError,
pkExecWithShell,
pkExecWithoutShell,
pkSpawnWithShell,
pkSpawnWithoutShell,
pkExpect,
processExit,
expectProcessError,
setupTestAgent,
spawnFile,
escapeShellArgs,
};

0 comments on commit d25120c

Please sign in to comment.