Skip to content

Commit af570ed

Browse files
committed
chore: added fastcheck tests for added features
1 parent 50c4393 commit af570ed

File tree

6 files changed

+112
-87
lines changed

6 files changed

+112
-87
lines changed

src/secrets/CommandEnv.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class CommandEnv extends CommandPolykey {
2828
'command and arguments formatted as [envPaths...][-- cmd [cmdArgs...]]',
2929
binParsers.parseEnvArgs,
3030
);
31-
this.passThroughOptions(); // Let '--' pass through as-is
3231
this.action(
3332
async (
3433
args: [Array<[string, string?, string?]>, Array<string>],
@@ -201,20 +200,22 @@ class CommandEnv extends CommandPolykey {
201200
switch (platform) {
202201
case 'linux':
203202
case 'darwin':
204-
const { exec } = await import('@matrixai/exec');
205-
try {
206-
exec.execvp(cmd, argv, envp);
207-
} catch (e) {
208-
if ('code' in e && e.code === 'GenericFailure') {
209-
throw new binErrors.ErrorPolykeyCLISubprocessFailure(
210-
`Command failed with error ${e}`,
211-
{
212-
cause: e,
213-
data: { command: [cmd, ...argv] },
214-
},
215-
);
203+
{
204+
const { exec } = await import('@matrixai/exec');
205+
try {
206+
exec.execvp(cmd, argv, envp);
207+
} catch (e) {
208+
if ('code' in e && e.code === 'GenericFailure') {
209+
throw new binErrors.ErrorPolykeyCLISubprocessFailure(
210+
`Command failed with error ${e}`,
211+
{
212+
cause: e,
213+
data: { command: [cmd, ...argv] },
214+
},
215+
);
216+
}
217+
throw e;
216218
}
217-
throw e;
218219
}
219220
break;
220221
default: {

src/utils/parsers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ function parseEnvArgs(
208208
[],
209209
[],
210210
];
211-
console.error('value', value)
212-
console.error('curent', current)
213211
if (current[1].length === 0) {
214212
// Parse a secret path
215213
if (value !== '--') {

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ function outputFormatterError(err: any): string {
449449
output += `${indent}timestamp\t${err.timestamp}\n`;
450450
} else {
451451
if (err.data && !utils.isEmptyObject(err.data)) {
452-
output += `\n${indent}data: ${JSON.stringify(err.data)}\n`
452+
output += `\n${indent}data: ${JSON.stringify(err.data)}\n`;
453453
} else {
454454
output += '\n';
455455
}

tests/secrets/env.test.ts

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('commandEnv', () => {
1818
let dataDir: string;
1919
let polykeyAgent: PolykeyAgent;
2020

21+
const secretContentNewlineArb = fc.stringMatching(/^[ -~]+\n$/).noShrink();
22+
2123
beforeEach(async () => {
2224
dataDir = await fs.promises.mkdtemp(
2325
path.join(globalThis.tmpDir, 'polykey-test-'),
@@ -59,6 +61,7 @@ describe('commandEnv', () => {
5961
'unix',
6062
`${vaultName}:SECRET`,
6163
'--',
64+
'--',
6265
'node',
6366
'-e',
6467
'console.log(JSON.stringify(process.env))',
@@ -86,6 +89,7 @@ describe('commandEnv', () => {
8689
`${vaultName}:SECRET1`,
8790
`${vaultName}:SECRET2`,
8891
'--',
92+
'--',
8993
'node',
9094
'-e',
9195
'console.log(JSON.stringify(process.env))',
@@ -115,6 +119,7 @@ describe('commandEnv', () => {
115119
'unix',
116120
`${vaultName}:dir1`,
117121
'--',
122+
'--',
118123
'node',
119124
'-e',
120125
'console.log(JSON.stringify(process.env))',
@@ -142,6 +147,7 @@ describe('commandEnv', () => {
142147
'unix',
143148
`${vaultName}:SECRET=SECRET_NEW`,
144149
'--',
150+
'--',
145151
'node',
146152
'-e',
147153
'console.log(JSON.stringify(process.env))',
@@ -170,6 +176,7 @@ describe('commandEnv', () => {
170176
'unix',
171177
`${vaultName}:dir1=SECRET_NEW`,
172178
'--',
179+
'--',
173180
'node',
174181
'-e',
175182
'console.log(JSON.stringify(process.env))',
@@ -203,6 +210,7 @@ describe('commandEnv', () => {
203210
`${vaultName}:SECRET2`,
204211
`${vaultName}:dir1`,
205212
'--',
213+
'--',
206214
'node',
207215
'-e',
208216
'console.log(JSON.stringify(process.env))',
@@ -231,6 +239,7 @@ describe('commandEnv', () => {
231239
'unix',
232240
`${vaultName}:SECRET1`,
233241
'--',
242+
'--',
234243
'node',
235244
'-e',
236245
'console.log(JSON.stringify(process.env))',
@@ -267,6 +276,7 @@ describe('commandEnv', () => {
267276
`${vaultName}:SECRET3=SECRET4`,
268277
`${vaultName}:dir1`,
269278
'--',
279+
'--',
270280
'node',
271281
'-e',
272282
'console.log(JSON.stringify(process.env))',
@@ -684,33 +694,6 @@ describe('commandEnv', () => {
684694
'unix',
685695
`${vaultName}:${secretName}`,
686696
'--',
687-
'node',
688-
'-e',
689-
'console.log(JSON.stringify(process.env))',
690-
];
691-
const result = await testUtils.pkExec(command, {
692-
env: { PK_PASSWORD: password },
693-
});
694-
expect(result.exitCode).toBe(0);
695-
const jsonOut = JSON.parse(result.stdout);
696-
expect(jsonOut[secretName]).toBe(secretContent);
697-
});
698-
// TODO: fastcheck
699-
test('single trailing newline is automatically removed', async () => {
700-
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
701-
const secretName = 'SECRET';
702-
const secretContent = 'this is a secret\n'; // Simulating Unix file ending
703-
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
704-
await vaultOps.addSecret(vault, secretName, secretContent);
705-
});
706-
const command = [
707-
'secrets',
708-
'env',
709-
'-np',
710-
dataDir,
711-
'--env-format',
712-
'unix',
713-
`${vaultName}:${secretName}`,
714697
'--',
715698
'node',
716699
'-e',
@@ -721,41 +704,76 @@ describe('commandEnv', () => {
721704
});
722705
expect(result.exitCode).toBe(0);
723706
const jsonOut = JSON.parse(result.stdout);
724-
// Remove last character
725-
expect(jsonOut[secretName]).toBe(secretContent.slice(0, -1));
726-
});
727-
// TODO: fastcheck
728-
test('trailing newlines are preserved with option', async () => {
729-
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
730-
const secretName = 'SECRET';
731-
const secretContent = 'this is a secret\n';
732-
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
733-
await vaultOps.addSecret(vault, secretName, secretContent);
734-
});
735-
const command = [
736-
'secrets',
737-
'env',
738-
'-np',
739-
dataDir,
740-
'--env-format',
741-
'unix',
742-
'--preserve-newline',
743-
`${vaultName}:${secretName}`,
744-
`${vaultName}:${secretName}`,
745-
'--',
746-
'node',
747-
'-e',
748-
'console.log(JSON.stringify(process.env))',
749-
];
750-
const result = await testUtils.pkExec(command, {
751-
env: { PK_PASSWORD: password },
752-
});
753-
console.error(result.stderr)
754-
expect(result.exitCode).toBe(0);
755-
const jsonOut = JSON.parse(result.stdout);
756-
// Remove last character
757707
expect(jsonOut[secretName]).toBe(secretContent);
758708
});
709+
test.prop([testUtils.vaultNameArb, secretContentNewlineArb], {
710+
numRuns: 2,
711+
})(
712+
'single trailing newline is automatically removed',
713+
async (vaultName, secretContent) => {
714+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
715+
const secretName = 'SECRET';
716+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
717+
await vaultOps.addSecret(vault, secretName, secretContent);
718+
});
719+
const command = [
720+
'secrets',
721+
'env',
722+
'-np',
723+
dataDir,
724+
'--env-format',
725+
'unix',
726+
`${vaultName}:${secretName}`,
727+
'--',
728+
'--',
729+
'node',
730+
'-e',
731+
'console.log(JSON.stringify(process.env))',
732+
];
733+
const result = await testUtils.pkExec(command, {
734+
env: { PK_PASSWORD: password },
735+
});
736+
expect(result.exitCode).toBe(0);
737+
const jsonOut = JSON.parse(result.stdout);
738+
// Remove last character
739+
expect(jsonOut[secretName]).toBe(secretContent.slice(0, -1));
740+
},
741+
);
742+
test.prop([testUtils.vaultNameArb, secretContentNewlineArb], {
743+
numRuns: 2,
744+
})(
745+
'trailing newlines are preserved with option',
746+
async (vaultName, secretContent) => {
747+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
748+
const secretName = 'SECRET';
749+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
750+
await vaultOps.addSecret(vault, secretName, secretContent);
751+
});
752+
const command = [
753+
'secrets',
754+
'env',
755+
'-np',
756+
dataDir,
757+
'--env-format',
758+
'unix',
759+
'--preserve-newline',
760+
`${vaultName}:${secretName}`,
761+
`${vaultName}:${secretName}`,
762+
'--',
763+
'--',
764+
'node',
765+
'-e',
766+
'console.log(JSON.stringify(process.env))',
767+
];
768+
const result = await testUtils.pkExec(command, {
769+
env: { PK_PASSWORD: password },
770+
});
771+
expect(result.exitCode).toBe(0);
772+
const jsonOut = JSON.parse(result.stdout);
773+
expect(jsonOut[secretName]).toBe(secretContent);
774+
await polykeyAgent.vaultManager.destroyVault(vaultId);
775+
},
776+
);
759777
test.prop([
760778
testUtils.secretPathEnvArrayArb,
761779
fc.string().noShrink(),
@@ -785,10 +803,10 @@ describe('commandEnv', () => {
785803
);
786804
test('handles no arguments', async () => {
787805
const command = ['secrets', 'env', '-np', dataDir, '--env-format', 'unix'];
788-
const result1 = await testUtils.pkExec(command, {
806+
const result = await testUtils.pkExec(command, {
789807
env: { PK_PASSWORD: password },
790808
});
791-
expect(result1.exitCode).toBe(64);
809+
expect(result.exitCode).toBe(64);
792810
});
793811
test('handles providing no secret paths', async () => {
794812
const command = [
@@ -799,6 +817,7 @@ describe('commandEnv', () => {
799817
'--env-format',
800818
'unix',
801819
'--',
820+
'--',
802821
'someCommand',
803822
];
804823
const result = await testUtils.pkExec(command, {

tests/utils.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as polykeyErrors from 'polykey/dist/errors';
88
import * as fc from 'fast-check';
99
import * as binUtils from '@/utils/utils';
1010
import * as binParsers from '@/utils/parsers';
11+
import * as testUtils from './utils';
1112

1213
describe('outputFormatters', () => {
1314
const nonPrintableCharArb = fc
@@ -329,10 +330,6 @@ describe('outputFormatters', () => {
329330
});
330331

331332
describe('parsers', () => {
332-
const vaultNameArb = fc.stringOf(
333-
fc.char().filter((c) => binParsers.vaultNameRegex.test(c)),
334-
{ minLength: 1, maxLength: 100 },
335-
);
336333
const singleSecretPathArb = fc.stringOf(
337334
fc.char().filter((c) => binParsers.secretPathRegex.test(c)),
338335
{ minLength: 1, maxLength: 25 },
@@ -349,28 +346,30 @@ describe('parsers', () => {
349346
.tuple(valueFirstCharArb, valueRestCharArb)
350347
.map((components) => components.join(''));
351348

352-
test.prop([vaultNameArb], { numRuns: 100 })(
349+
test.prop([testUtils.vaultNameArb], { numRuns: 100 })(
353350
'should parse vault name',
354351
async (vaultName) => {
355352
expect(binParsers.parseVaultName(vaultName)).toEqual(vaultName);
356353
},
357354
);
358-
test.prop([vaultNameArb], { numRuns: 10 })(
355+
test.prop([testUtils.vaultNameArb], { numRuns: 10 })(
359356
'should parse secret path with only vault name',
360357
async (vaultName) => {
361358
const result = [vaultName, undefined, undefined];
362359
expect(binParsers.parseSecretPath(vaultName)).toEqual(result);
363360
},
364361
);
365-
test.prop([vaultNameArb, secretPathArb], { numRuns: 100 })(
362+
test.prop([testUtils.vaultNameArb, secretPathArb], { numRuns: 100 })(
366363
'should parse full secret path with vault name',
367364
async (vaultName, secretPath) => {
368365
const query = `${vaultName}:${secretPath}`;
369366
const result = [vaultName, secretPath, undefined];
370367
expect(binParsers.parseSecretPath(query)).toEqual(result);
371368
},
372369
);
373-
test.prop([vaultNameArb, secretPathArb, valueDataArb], { numRuns: 100 })(
370+
test.prop([testUtils.vaultNameArb, secretPathArb, valueDataArb], {
371+
numRuns: 100,
372+
})(
374373
'should parse full secret path with vault name and value',
375374
async (vaultName, secretPath, valueData) => {
376375
const query = `${vaultName}:${secretPath}=${valueData}`;

tests/utils/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ const cmdArgsArrayArb = fc
112112
})
113113
.noShrink();
114114

115+
const vaultNameArb = fc
116+
.stringOf(
117+
fc.char().filter((c) => binParsers.vaultNameRegex.test(c)),
118+
{ minLength: 1, maxLength: 100 },
119+
)
120+
.noShrink();
121+
115122
export {
116123
testIf,
117124
describeIf,
@@ -120,4 +127,5 @@ export {
120127
secretPathEnvArb,
121128
secretPathEnvArrayArb,
122129
cmdArgsArrayArb,
130+
vaultNameArb,
123131
};

0 commit comments

Comments
 (0)