Skip to content

Commit 2a723ee

Browse files
committed
chore: updated mkdir and added tests
[ci skip]
1 parent 39f349f commit 2a723ee

2 files changed

Lines changed: 120 additions & 1 deletion

File tree

src/secrets/CommandMkdir.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as binUtils from '../utils';
44
import * as binOptions from '../utils/options';
55
import * as binParsers from '../utils/parsers';
66
import * as binProcessors from '../utils/processors';
7+
import { ErrorMessage } from 'polykey/dist/client/types';
8+
import { ErrorPolykeyCLIUncaughtException } from '@/errors';
79

810
class CommandMkdir extends CommandPolykey {
911
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
@@ -68,7 +70,23 @@ class CommandMkdir extends CommandPolykey {
6870
}
6971
await writer.close();
7072
for await (const result of response.readable) {
71-
if (result.error != null) console.error(result.error);
73+
if (result.type === 'error') {
74+
const error = result as ErrorMessage;
75+
let message: string = '';
76+
if (error.code === 'ENOENT') {
77+
message = 'No such secret or directory';
78+
} else if (error.code === 'EEXIST') {
79+
message = 'Secret or directory exists';
80+
} else {
81+
throw new ErrorPolykeyCLIUncaughtException(
82+
`Unexpected error code ${error.code}`,
83+
);
84+
}
85+
86+
process.stderr.write(
87+
`${error.code}: cannot create directory ${error.reason}: ${message}`,
88+
);
89+
}
7290
}
7391
}, meta);
7492
} finally {

tests/secrets/mkdir.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import PolykeyAgent from 'polykey/dist/PolykeyAgent';
66
import { vaultOps } from 'polykey/dist/vaults';
77
import * as keysUtils from 'polykey/dist/keys/utils';
88
import * as testUtils from '../utils';
9+
import { remoteErrorCause } from '@/utils';
910

1011
describe('commandMkdir', () => {
1112
const password = 'password';
@@ -94,6 +95,106 @@ describe('commandMkdir', () => {
9495
expect(stat2.isDirectory()).toBeTruthy();
9596
});
9697
});
98+
test('should fail without recursive set', async () => {
99+
const vaultName = 'vault' as VaultName;
100+
const dirName1 = 'dir1';
101+
const dirName2 = 'dir2';
102+
const dirNameNested = `${dirName1}/${dirName2}`;
103+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
104+
105+
command = [
106+
'secrets',
107+
'mkdir',
108+
'-np',
109+
dataDir,
110+
`${vaultName}:${dirNameNested}`,
111+
];
112+
113+
const result = await testUtils.pkStdio([...command], {
114+
env: { PK_PASSWORD: password },
115+
cwd: dataDir,
116+
});
117+
expect(result.exitCode).toBe(0);
118+
expect(result.stderr).toBeDefined();
119+
120+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
121+
await vault.readF(async (efs) => {
122+
const dirName1P = efs.readdir(dirName1);
123+
await expect(dirName1P).rejects.toThrow('ENOENT');
124+
const dirNameNestedP = efs.readdir(dirNameNested);
125+
await expect(dirNameNestedP).rejects.toThrow('ENOENT');
126+
});
127+
})
128+
});
129+
test('should fail to make existing directory', async () => {
130+
const vaultName = 'vault' as VaultName;
131+
const dirName = 'dir-exists';
132+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
133+
134+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
135+
await vault.writeF(async (efs) => {
136+
await efs.mkdir(dirName);
137+
});
138+
});
139+
140+
command = [
141+
'secrets',
142+
'mkdir',
143+
'-np',
144+
dataDir,
145+
`${vaultName}:${dirName}`,
146+
];
147+
148+
const result = await testUtils.pkStdio([...command], {
149+
env: { PK_PASSWORD: password },
150+
cwd: dataDir,
151+
});
152+
expect(result.exitCode).toBe(0);
153+
expect(result.stderr).toBeDefined();
154+
155+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
156+
await vault.readF(async (efs) => {
157+
const dirP = efs.readdir(dirName);
158+
await expect(dirP).toResolve();
159+
});
160+
})
161+
});
162+
test('should fail to make existing secret', async () => {
163+
const vaultName = 'vault' as VaultName;
164+
const secretName = 'secret-exists';
165+
const secretContent = 'secret-content';
166+
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
167+
168+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
169+
await vault.writeF(async (efs) => {
170+
await efs.writeFile(secretName, secretContent);
171+
});
172+
});
173+
174+
command = [
175+
'secrets',
176+
'mkdir',
177+
'-np',
178+
dataDir,
179+
`${vaultName}:${secretName}`,
180+
];
181+
182+
const result = await testUtils.pkStdio([...command], {
183+
env: { PK_PASSWORD: password },
184+
cwd: dataDir,
185+
});
186+
expect(result.exitCode).toBe(0);
187+
expect(result.stderr).toBeDefined();
188+
189+
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
190+
await vault.readF(async (efs) => {
191+
const stat = await efs.stat(secretName);
192+
expect(stat.isFile()).toBeTruthy();
193+
const contents = await efs.readFile(secretName);
194+
expect(contents.toString()).toEqual(secretContent)
195+
});
196+
})
197+
});
97198
test('should make directories in multiple vaults', async () => {
98199
const vaultName1 = 'vault1' as VaultName;
99200
const vaultName2 = 'vault2' as VaultName;

0 commit comments

Comments
 (0)