Skip to content

Commit 654208d

Browse files
committed
fix: pkStdio can only be used on commands that exit with code 0
1 parent 0db0aaa commit 654208d

File tree

6 files changed

+28
-30
lines changed

6 files changed

+28
-30
lines changed

tests/identities/authenticateAuthenticated.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('authenticate/authenticated', () => {
120120
let exitCode: number;
121121
// Authenticate
122122
// Invalid provider
123-
({ exitCode } = await testUtils.pkStdio(
123+
({ exitCode } = await testUtils.pkExec(
124124
['identities', 'authenticate', '', testToken.identityId],
125125
{
126126
env: {
@@ -133,16 +133,13 @@ describe('authenticate/authenticated', () => {
133133
expect(exitCode).toBe(sysexits.USAGE);
134134
// Authenticated
135135
// Invalid provider
136-
({ exitCode } = await testUtils.pkStdio(
137-
['identities', 'authenticate', ''],
138-
{
139-
env: {
140-
PK_NODE_PATH: nodePath,
141-
PK_PASSWORD: password,
142-
},
143-
cwd: dataDir,
136+
({ exitCode } = await testUtils.pkExec(['identities', 'authenticate', ''], {
137+
env: {
138+
PK_NODE_PATH: nodePath,
139+
PK_PASSWORD: password,
144140
},
145-
));
141+
cwd: dataDir,
142+
}));
146143
expect(exitCode).toBe(sysexits.USAGE);
147144
});
148145
});

tests/identities/claim.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('claim', () => {
103103
// Expect(claim!.payload.data.type).toBe('identity');
104104
});
105105
test('cannot claim unauthenticated identities', async () => {
106-
const { exitCode } = await testUtils.pkStdio(
106+
const { exitCode } = await testUtils.pkExec(
107107
[
108108
'identities',
109109
'claim',
@@ -122,7 +122,7 @@ describe('claim', () => {
122122
test('should fail on invalid inputs', async () => {
123123
let exitCode: number;
124124
// Invalid provider
125-
({ exitCode } = await testUtils.pkStdio(
125+
({ exitCode } = await testUtils.pkExec(
126126
['identities', 'claim', `:${testToken.identityId}`],
127127
{
128128
env: {
@@ -134,7 +134,7 @@ describe('claim', () => {
134134
));
135135
expect(exitCode).toBe(sysexits.USAGE);
136136
// Invalid identity
137-
({ exitCode } = await testUtils.pkStdio(
137+
({ exitCode } = await testUtils.pkExec(
138138
['identities', 'claim', `${testToken.providerId}:`],
139139
{
140140
env: {

tests/identities/discoverGet.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('discover/get', () => {
284284
});
285285
test('should fail on invalid inputs', async () => {
286286
// Discover
287-
const { exitCode } = await testUtils.pkStdio(
287+
const { exitCode } = await testUtils.pkExec(
288288
['identities', 'discover', 'invalid'],
289289
{
290290
env: {
@@ -296,7 +296,7 @@ describe('discover/get', () => {
296296
);
297297
expect(exitCode).toBe(sysexits.USAGE);
298298
// Get
299-
await testUtils.pkStdio(['identities', 'get', 'invalid'], {
299+
await testUtils.pkExec(['identities', 'get', 'invalid'], {
300300
env: {
301301
PK_NODE_PATH: nodePath,
302302
PK_PASSWORD: password,

tests/identities/search.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ describe('search', () => {
361361
test('should fail on invalid inputs', async () => {
362362
let exitCode: number;
363363
// Invalid identity id
364-
({ exitCode } = await testUtils.pkStdio(
364+
({ exitCode } = await testUtils.pkExec(
365365
['identities', 'search', '--identity-id', ''],
366366
{
367367
env: {
@@ -373,7 +373,7 @@ describe('search', () => {
373373
));
374374
expect(exitCode).toBe(sysexits.USAGE);
375375
// Invalid auth identity id
376-
({ exitCode } = await testUtils.pkStdio(
376+
({ exitCode } = await testUtils.pkExec(
377377
['identities', 'search', '--auth-identity-id', ''],
378378
{
379379
env: {
@@ -385,7 +385,7 @@ describe('search', () => {
385385
));
386386
expect(exitCode).toBe(sysexits.USAGE);
387387
// Invalid value for limit
388-
({ exitCode } = await testUtils.pkStdio(
388+
({ exitCode } = await testUtils.pkExec(
389389
['identities', 'search', '--limit', 'NaN'],
390390
{
391391
env: {

tests/identities/trustUntrustList.test.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('trust/untrust/list', () => {
274274
// belongs to and add it to our gestalt graph
275275
// This command should fail first time as we need to allow time for the
276276
// identity to be linked to a node in the node graph
277-
({ exitCode } = await testUtils.pkStdio(
277+
({ exitCode } = await testUtils.pkExec(
278278
['identities', 'trust', providerString],
279279
{
280280
env: {
@@ -379,19 +379,16 @@ describe('trust/untrust/list', () => {
379379
test('should fail on invalid inputs', async () => {
380380
let exitCode: number;
381381
// Trust
382-
({ exitCode } = await testUtils.pkStdio(
383-
['identities', 'trust', 'invalid'],
384-
{
385-
env: {
386-
PK_NODE_PATH: nodePath,
387-
PK_PASSWORD: password,
388-
},
389-
cwd: dataDir,
382+
({ exitCode } = await testUtils.pkExec(['identities', 'trust', 'invalid'], {
383+
env: {
384+
PK_NODE_PATH: nodePath,
385+
PK_PASSWORD: password,
390386
},
391-
));
387+
cwd: dataDir,
388+
}));
392389
expect(exitCode).toBe(sysexits.USAGE);
393390
// Untrust
394-
({ exitCode } = await testUtils.pkStdio(
391+
({ exitCode } = await testUtils.pkExec(
395392
['identities', 'untrust', 'invalid'],
396393
{
397394
env: {

tests/utils/exec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ async function pk(args: Array<string>): Promise<any> {
150150
* Runs pk command functionally with mocked STDIO
151151
* Both stdout and stderr are the entire output including newlines
152152
* This can only be used serially, because the mocks it relies on are global singletons
153-
* If it is used concurrently, the mocking side effects can conflict
153+
* If it is used concurrently, the mocking side effects can conflict.
154+
* Note that because this does not launch Polykey in a subprocess, the `process.exitCode` of the main process is overriden.
155+
* This should only be used for commands that are expected to exit with code 0.
154156
*/
155157
async function pkStdio(
156158
args: Array<string> = [],
@@ -160,6 +162,7 @@ async function pkStdio(
160162
stdout: string;
161163
stderr: string;
162164
}> {
165+
const lastExitCode = process.exitCode;
163166
const cwd =
164167
opts.cwd ??
165168
(await fs.promises.mkdtemp(path.join(globalThis.tmpDir, 'polykey-test-')));
@@ -221,6 +224,7 @@ async function pkStdio(
221224
mockProcessAddListener.mockRestore();
222225
mockProcessOnce.mockRestore();
223226
mockProcessOn.mockRestore();
227+
process.exitCode = lastExitCode;
224228
return {
225229
exitCode,
226230
stdout,

0 commit comments

Comments
 (0)