Skip to content

Commit

Permalink
fix(useTemporaryDirectory): Allow writing files to subdirectories
Browse files Browse the repository at this point in the history
- Recursively create subdirectories before writing file
  • Loading branch information
vinsonchuong committed Oct 12, 2021
1 parent 662065e commit 41039ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions use-temporary-directory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default async function (t) {
const absolutePath = path.join(directory, filePath)
const contents = stripIndent(fileContents.trim()) + '\n'

await fs.mkdir(path.dirname(absolutePath), {recursive: true})
await fs.writeFile(absolutePath, contents)

if (contents.startsWith('#!')) {
Expand Down
16 changes: 16 additions & 0 deletions use-temporary-directory/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ test('automatically setting permissions for executable files', async (t) => {
})
t.is(output, 'Hello World!\n')
})

test('automatically creating subdirectories', async (t) => {
const directory = await useTemporaryDirectory(t)

await directory.writeFile(
'folder/file.txt',
`
Hello World!
`
)

t.is(
await fs.readFile(path.join(directory.path, 'folder', 'file.txt'), 'utf8'),
'Hello World!\n'
)
})

0 comments on commit 41039ff

Please sign in to comment.