Skip to content

Commit

Permalink
fix: pkStdio can only be used on commands that exit with code 0
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Feb 26, 2024
1 parent 0db0aaa commit 654208d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
17 changes: 7 additions & 10 deletions tests/identities/authenticateAuthenticated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('authenticate/authenticated', () => {
let exitCode: number;
// Authenticate
// Invalid provider
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'authenticate', '', testToken.identityId],
{
env: {
Expand All @@ -133,16 +133,13 @@ describe('authenticate/authenticated', () => {
expect(exitCode).toBe(sysexits.USAGE);
// Authenticated
// Invalid provider
({ exitCode } = await testUtils.pkStdio(
['identities', 'authenticate', ''],
{
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: password,
},
cwd: dataDir,
({ exitCode } = await testUtils.pkExec(['identities', 'authenticate', ''], {
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: password,
},
));
cwd: dataDir,
}));
expect(exitCode).toBe(sysexits.USAGE);
});
});
6 changes: 3 additions & 3 deletions tests/identities/claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('claim', () => {
// Expect(claim!.payload.data.type).toBe('identity');
});
test('cannot claim unauthenticated identities', async () => {
const { exitCode } = await testUtils.pkStdio(
const { exitCode } = await testUtils.pkExec(
[
'identities',
'claim',
Expand All @@ -122,7 +122,7 @@ describe('claim', () => {
test('should fail on invalid inputs', async () => {
let exitCode: number;
// Invalid provider
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'claim', `:${testToken.identityId}`],
{
env: {
Expand All @@ -134,7 +134,7 @@ describe('claim', () => {
));
expect(exitCode).toBe(sysexits.USAGE);
// Invalid identity
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'claim', `${testToken.providerId}:`],
{
env: {
Expand Down
4 changes: 2 additions & 2 deletions tests/identities/discoverGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('discover/get', () => {
});
test('should fail on invalid inputs', async () => {
// Discover
const { exitCode } = await testUtils.pkStdio(
const { exitCode } = await testUtils.pkExec(
['identities', 'discover', 'invalid'],
{
env: {
Expand All @@ -296,7 +296,7 @@ describe('discover/get', () => {
);
expect(exitCode).toBe(sysexits.USAGE);
// Get
await testUtils.pkStdio(['identities', 'get', 'invalid'], {
await testUtils.pkExec(['identities', 'get', 'invalid'], {
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: password,
Expand Down
6 changes: 3 additions & 3 deletions tests/identities/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe('search', () => {
test('should fail on invalid inputs', async () => {
let exitCode: number;
// Invalid identity id
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'search', '--identity-id', ''],
{
env: {
Expand All @@ -373,7 +373,7 @@ describe('search', () => {
));
expect(exitCode).toBe(sysexits.USAGE);
// Invalid auth identity id
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'search', '--auth-identity-id', ''],
{
env: {
Expand All @@ -385,7 +385,7 @@ describe('search', () => {
));
expect(exitCode).toBe(sysexits.USAGE);
// Invalid value for limit
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'search', '--limit', 'NaN'],
{
env: {
Expand Down
19 changes: 8 additions & 11 deletions tests/identities/trustUntrustList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('trust/untrust/list', () => {
// belongs to and add it to our gestalt graph
// This command should fail first time as we need to allow time for the
// identity to be linked to a node in the node graph
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'trust', providerString],
{
env: {
Expand Down Expand Up @@ -379,19 +379,16 @@ describe('trust/untrust/list', () => {
test('should fail on invalid inputs', async () => {
let exitCode: number;
// Trust
({ exitCode } = await testUtils.pkStdio(
['identities', 'trust', 'invalid'],
{
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: password,
},
cwd: dataDir,
({ exitCode } = await testUtils.pkExec(['identities', 'trust', 'invalid'], {
env: {
PK_NODE_PATH: nodePath,
PK_PASSWORD: password,
},
));
cwd: dataDir,
}));
expect(exitCode).toBe(sysexits.USAGE);
// Untrust
({ exitCode } = await testUtils.pkStdio(
({ exitCode } = await testUtils.pkExec(
['identities', 'untrust', 'invalid'],
{
env: {
Expand Down
6 changes: 5 additions & 1 deletion tests/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ async function pk(args: Array<string>): Promise<any> {
* Runs pk command functionally with mocked STDIO
* 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
* If it is used concurrently, the mocking side effects can conflict.
* Note that because this does not launch Polykey in a subprocess, the `process.exitCode` of the main process is overriden.
* This should only be used for commands that are expected to exit with code 0.
*/
async function pkStdio(
args: Array<string> = [],
Expand All @@ -160,6 +162,7 @@ async function pkStdio(
stdout: string;
stderr: string;
}> {
const lastExitCode = process.exitCode;
const cwd =
opts.cwd ??
(await fs.promises.mkdtemp(path.join(globalThis.tmpDir, 'polykey-test-')));
Expand Down Expand Up @@ -221,6 +224,7 @@ async function pkStdio(
mockProcessAddListener.mockRestore();
mockProcessOnce.mockRestore();
mockProcessOn.mockRestore();
process.exitCode = lastExitCode;
return {
exitCode,
stdout,
Expand Down

0 comments on commit 654208d

Please sign in to comment.