Skip to content

Commit da8410f

Browse files
committed
Use unique path in useTemporaryFile function
1 parent 21290d8 commit da8410f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/snaps-utils/src/fs.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ describe('useTemporaryFile', () => {
225225
expect(result).toBe('baz');
226226
});
227227

228+
it('creates a unique directory', async () => {
229+
const first = await useTemporaryFile('foo', 'bar', async (filePath) => {
230+
return filePath;
231+
});
232+
233+
const second = await useTemporaryFile('foo', 'bar', async (filePath) => {
234+
return filePath;
235+
});
236+
237+
expect(first).toContain('foo');
238+
expect(second).toContain('foo');
239+
expect(first).not.toBe(second);
240+
});
241+
228242
it('always deletes the temporary file after usage', async () => {
229243
expect.assertions(4);
230244
let filePath: string;

packages/snaps-utils/src/fs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Json } from '@metamask/utils';
22
import { promises as fs } from 'fs';
3+
import { mkdtemp } from 'fs/promises';
34
import os from 'os';
45
import pathUtils from 'path';
56

@@ -165,7 +166,9 @@ export async function useTemporaryFile<Type = unknown>(
165166
fileContents: string,
166167
fn: (path: string) => Promise<Type>,
167168
): Promise<Type> {
168-
const filePath = pathUtils.join(os.tmpdir(), fileName);
169+
const temporaryDirectory = await mkdtemp(os.tmpdir());
170+
const filePath = pathUtils.join(temporaryDirectory, fileName);
171+
169172
await fs.mkdir(pathUtils.dirname(filePath), { recursive: true });
170173
await fs.writeFile(filePath, fileContents);
171174
try {

0 commit comments

Comments
 (0)