Skip to content

Commit

Permalink
chore: working on stdin test
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 17, 2024
1 parent 2bf097a commit 3cb02c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/secrets/CommandCat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class CommandGet extends CommandPolykey {
},
logger: this.logger.getChild(PolykeyClient.name),
});
if (secretPaths.length == 0) {
process.stdout.pipe(process.stdin);
return;
}

const response = await binUtils.retryAuthentication(
(auth) =>
pkClient.rpcClient.methods.vaultsSecretsGet({
Expand Down
29 changes: 24 additions & 5 deletions tests/secrets/cat.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { VaultId, VaultName } from 'polykey/dist/vaults/types';
import path from 'path';
import process from 'process';
import fs from 'fs';
import os from 'os';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import { vaultOps } from 'polykey/dist/vaults';
Expand All @@ -12,7 +14,6 @@ describe('commandCatSecret', () => {
const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]);
let dataDir: string;
let polykeyAgent: PolykeyAgent;
let command: Array<string>;

beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('commandCatSecret', () => {
await vaultOps.addSecret(vault, 'MySecret', 'this is the secret');
});

command = ['secrets', 'cat', '-np', dataDir, `${vaultName}:MySecret`];
const command = ['secrets', 'cat', '-np', dataDir, `${vaultName}:MySecret`];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
Expand All @@ -71,7 +72,7 @@ describe('commandCatSecret', () => {
});

const secretPaths = secretNames.map((v) => `${vaultName}:${v}`);
command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];
const command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('commandCatSecret', () => {
const secretPaths = secretVaultNames.map(
(v) => `${vaultNames[v[0]]}:${v[1]}`,
);
command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];
const command = ['secrets', 'cat', '-np', dataDir, ...secretPaths];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
Expand All @@ -119,5 +120,23 @@ describe('commandCatSecret', () => {
expect(result.exitCode).toBe(0);
});

test.todo('should return stdin as-is');
test('should return stdin if no arguments are passed', async () => {
const command = ['secrets', 'cat', '-np', dataDir];
const stdin_data = `test-data${os.EOL}`;

process.stdin.cork();
process.stdin.write(stdin_data);
process.nextTick(() => process.stdin.uncork());

const childProcess = await testUtils.pkSpawn(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
childProcess.on('spawn', () => {

})
childProcess.stdin.write(stdin_data);
// expect(result.stdout).toBe(stdin_data);
// expect(result.exitCode).toBe(0);
});
});

0 comments on commit 3cb02c1

Please sign in to comment.