Skip to content

Commit

Permalink
feat(useTemporaryDirectory): Allow configuring where to create the di…
Browse files Browse the repository at this point in the history
…rectory
  • Loading branch information
vinsonchuong committed Sep 12, 2022
1 parent 505cc1c commit e701b38
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ yarn add ava-patterns
Create a temporary directory and delete it (and its contents) at the end of the
test.

Takes the following options (all of which are optional):

- `prefix`: The directory in which to create the temporary directory. Defaults
to a suitable location based on the host system (e.g. `/tmp`).

Returns an object with the following members:

- `path`: The absolute path to the temporary directory.
Expand All @@ -26,16 +31,25 @@ Returns an object with the following members:
If the file starts with a shebang, it is given executable permissions.

```js
import process from 'node:process'
import test from 'ava'
import {useTemporaryDirectory} from 'ava-patterns'

test('writing files', async (t) => {
test('writing files to a temporary directory', async (t) => {
const directory = await useTemporaryDirectory(t)
await directory.writeFile('file.txt', `
Hello World!
`)
t.pass()
})

test('creating temporary directories in a specified location', async (t) => {
const directory = await useTemporaryDirectory(t, { prefix: process.cwd() })
await directory.writeFile('file.txt', `
Hello World!
`)
t.pass()
})
```

### `runProcess()`
Expand Down
13 changes: 11 additions & 2 deletions use-temporary-directory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ import stripIndent from 'strip-indent'
* }} Directory
*/

/**
* @typedef {Object} Options
* @property {string} [prefix] - A directory in which to create the temporary
* directory
*/

/**
* Create a temporary directory and delete it at the end of the test.
*
* @param {import('ava').ExecutionContext<any>} t
* @param {Options} [options]
*
* @return {Promise<{
* path: string,
* writeFile(filePath: string, fileContents: string): Promise<void>
* }>} an object allowing manipulation of files within the directory.
*/
export default async function (t) {
const directory = tempy.directory()
export default async function (t, options) {
const directory = options?.prefix
? path.join(options.prefix, path.basename(tempy.directory()))
: tempy.directory()

await fs.ensureDir(directory)
t.teardown(async () => {
Expand Down
15 changes: 15 additions & 0 deletions use-temporary-directory/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import process from 'node:process'
import {promises as fs} from 'node:fs'
import {promisify} from 'node:util'
import childProcess from 'node:child_process'
Expand Down Expand Up @@ -62,3 +63,17 @@ test('automatically creating subdirectories', async (t) => {
'Hello World!\n',
)
})

test('configuring where to create a directory', async (t) => {
const directory = await useTemporaryDirectory(t, {prefix: process.cwd()})
t.is(path.dirname(directory.path), process.cwd())

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

0 comments on commit e701b38

Please sign in to comment.