File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 11import type { Json } from '@metamask/utils' ;
22import { promises as fs } from 'fs' ;
3+ import { mkdtemp } from 'fs/promises' ;
34import os from 'os' ;
45import 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 {
You can’t perform that action at this time.
0 commit comments