Skip to content

Commit a745f52

Browse files
committed
feat: process execution standardisation
Standardised process execution for bin tests using `pkExec` and `pkSpawn`. - All usage of `pkExec`/`pkSpawn` calls underlying default `WithoutShell` or override `WithShell` methods - Combined `env` and `cwd` options into an `ExecOpts` object and added `command` and `shell` options - Refactored `pkSpawnNs` and `pkExecNs` to use standardised methods - Replaced all usage of `exec`/`execFile` with `spawn`
1 parent 94ec1d2 commit a745f52

71 files changed

Lines changed: 1836 additions & 1422 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
{
3434
"name": "global",
3535
"message": "Use `globalThis` instead"
36+
},
37+
{
38+
"name": "window",
39+
"message": "Use `globalThis` instead"
3640
}
3741
],
3842
"require-yield": 0,

src/bin/agent/CommandStart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type PolykeyAgent from '../../PolykeyAgent';
88
import type { RecoveryCode } from '../../keys/types';
99
import type { PolykeyWorkerManagerInterface } from '../../workers/types';
1010
import path from 'path';
11-
import child_process from 'child_process';
11+
import childProcess from 'child_process';
1212
import process from 'process';
1313
import CommandPolykey from '../CommandPolykey';
1414
import * as binUtils from '../utils';
@@ -130,7 +130,7 @@ class CommandStart extends CommandPolykey {
130130
);
131131
stdio[2] = agentErrFile.fd;
132132
}
133-
const agentProcess = child_process.fork(
133+
const agentProcess = childProcess.fork(
134134
path.join(__dirname, '../polykey-agent'),
135135
[],
136136
{

tests/agent/service/notificationsSend.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb';
2626
import * as notificationsPB from '@/proto/js/polykey/v1/notifications/notifications_pb';
2727
import * as nodesUtils from '@/nodes/utils';
2828
import * as notificationsUtils from '@/notifications/utils';
29-
import { expectRemoteError } from '../../utils';
29+
import { expectRemoteError } from '../../utils/utils';
3030
import { globalRootKeyPems } from '../../fixtures/globalRootKeyPems';
3131

3232
describe('notificationsSend', () => {

tests/bin/agent/lock.test.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,19 @@ describe('lock', () => {
2929
testUtils.testIf(
3030
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
3131
)('lock deletes the session token', async () => {
32-
await execUtils.pkStdio(
33-
['agent', 'unlock'],
34-
{
32+
await execUtils.pkExec(['agent', 'unlock'], {
33+
env: {
3534
PK_NODE_PATH: agentDir,
3635
PK_PASSWORD: agentPassword,
3736
},
38-
agentDir,
39-
);
40-
const { exitCode } = await execUtils.pkStdio(
41-
['agent', 'lock'],
42-
{
37+
cwd: agentDir,
38+
});
39+
const { exitCode } = await execUtils.pkExec(['agent', 'lock'], {
40+
env: {
4341
PK_NODE_PATH: agentDir,
4442
},
45-
agentDir,
46-
);
43+
cwd: agentDir,
44+
});
4745
expect(exitCode).toBe(0);
4846
const session = await Session.createSession({
4947
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
@@ -61,30 +59,23 @@ describe('lock', () => {
6159
mockedPrompts.mockImplementation(async (_opts: any) => {
6260
return { password };
6361
});
64-
await execUtils.pkStdio(
65-
['agent', 'unlock'],
66-
{
62+
await execUtils.pkStdio(['agent', 'unlock'], {
63+
env: {
6764
PK_NODE_PATH: agentDir,
6865
PK_PASSWORD: agentPassword,
6966
},
70-
agentDir,
71-
);
67+
cwd: agentDir,
68+
});
7269
// Session token is deleted
73-
await execUtils.pkStdio(
74-
['agent', 'lock'],
75-
{
76-
PK_NODE_PATH: agentDir,
77-
},
78-
agentDir,
79-
);
70+
await execUtils.pkStdio(['agent', 'lock'], {
71+
env: { PK_NODE_PATH: agentDir },
72+
cwd: agentDir,
73+
});
8074
// Will prompt to reauthenticate
81-
await execUtils.pkStdio(
82-
['agent', 'status'],
83-
{
84-
PK_NODE_PATH: agentDir,
85-
},
86-
agentDir,
87-
);
75+
await execUtils.pkStdio(['agent', 'status'], {
76+
env: { PK_NODE_PATH: agentDir },
77+
cwd: agentDir,
78+
});
8879
// Prompted for password 1 time
8980
expect(mockedPrompts.mock.calls.length).toBe(1);
9081
mockedPrompts.mockClear();

tests/bin/agent/lockall.test.ts

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,17 @@ describe('lockall', () => {
3535
testUtils.testIf(
3636
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
3737
)('lockall deletes the session token', async () => {
38-
await execUtils.pkStdio(
39-
['agent', 'unlock'],
40-
{
38+
await execUtils.pkExec(['agent', 'unlock'], {
39+
env: {
4140
PK_NODE_PATH: agentDir,
4241
PK_PASSWORD: agentPassword,
4342
},
44-
agentDir,
45-
);
46-
const { exitCode } = await execUtils.pkStdio(
47-
['agent', 'lockall'],
48-
{
49-
PK_NODE_PATH: agentDir,
50-
},
51-
agentDir,
52-
);
43+
cwd: agentDir,
44+
});
45+
const { exitCode } = await execUtils.pkExec(['agent', 'lockall'], {
46+
env: { PK_NODE_PATH: agentDir },
47+
cwd: agentDir,
48+
});
5349
expect(exitCode).toBe(0);
5450
const session = await Session.createSession({
5551
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
@@ -63,33 +59,26 @@ describe('lockall', () => {
6359
'lockall ensures reauthentication is required',
6460
async () => {
6561
const password = agentPassword;
66-
await execUtils.pkStdio(
67-
['agent', 'unlock'],
68-
{
62+
await execUtils.pkStdio(['agent', 'unlock'], {
63+
env: {
6964
PK_NODE_PATH: agentDir,
7065
PK_PASSWORD: agentPassword,
7166
},
72-
agentDir,
73-
);
74-
await execUtils.pkStdio(
75-
['agent', 'lockall'],
76-
{
77-
PK_NODE_PATH: agentDir,
78-
},
79-
agentDir,
80-
);
67+
cwd: agentDir,
68+
});
69+
await execUtils.pkStdio(['agent', 'lockall'], {
70+
env: { PK_NODE_PATH: agentDir },
71+
cwd: agentDir,
72+
});
8173
// Token is deleted, reauthentication is required
8274
mockedPrompts.mockClear();
8375
mockedPrompts.mockImplementation(async (_opts: any) => {
8476
return { password };
8577
});
86-
await execUtils.pkStdio(
87-
['agent', 'status'],
88-
{
89-
PK_NODE_PATH: agentDir,
90-
},
91-
agentDir,
92-
);
78+
await execUtils.pkStdio(['agent', 'status'], {
79+
env: { PK_NODE_PATH: agentDir },
80+
cwd: agentDir,
81+
});
9382
// Prompted for password 1 time
9483
expect(mockedPrompts.mock.calls.length).toBe(1);
9584
mockedPrompts.mockClear();
@@ -98,37 +87,37 @@ describe('lockall', () => {
9887
testUtils.testIf(
9988
testUtils.isTestPlatformEmpty || testUtils.isTestPlatformDocker,
10089
)('lockall causes old session tokens to fail', async () => {
101-
await execUtils.pkStdio(
102-
['agent', 'unlock'],
103-
{
90+
await execUtils.pkExec(['agent', 'unlock'], {
91+
env: {
10492
PK_NODE_PATH: agentDir,
10593
PK_PASSWORD: agentPassword,
10694
},
107-
agentDir,
108-
);
95+
cwd: agentDir,
96+
});
10997
const session = await Session.createSession({
11098
sessionTokenPath: path.join(agentDir, config.defaults.tokenBase),
11199
fs,
112100
logger,
113101
});
114102
const token = await session.readToken();
115103
await session.stop();
116-
await execUtils.pkStdio(
117-
['agent', 'lockall'],
118-
{
104+
await execUtils.pkExec(['agent', 'lockall'], {
105+
env: {
119106
PK_NODE_PATH: agentDir,
120107
PK_PASSWORD: agentPassword,
121108
},
122-
agentDir,
123-
);
109+
cwd: agentDir,
110+
});
124111
// Old token is invalid
125-
const { exitCode, stderr } = await execUtils.pkStdio(
112+
const { exitCode, stderr } = await execUtils.pkExec(
126113
['agent', 'status', '--format', 'json'],
127114
{
128-
PK_NODE_PATH: agentDir,
129-
PK_TOKEN: token,
115+
env: {
116+
PK_NODE_PATH: agentDir,
117+
PK_TOKEN: token,
118+
},
119+
cwd: agentDir,
130120
},
131-
agentDir,
132121
);
133122
execUtils.expectProcessError(exitCode, stderr, [
134123
new errors.ErrorClientAuthDenied(),

0 commit comments

Comments
 (0)