From fa4a3202197ec58172944e251ff8c96ec9878391 Mon Sep 17 00:00:00 2001 From: Vinson Chuong Date: Sat, 5 Sep 2020 12:49:22 -0700 Subject: [PATCH] feat(useTemporaryDirectory): Return an object with useful methods BREAKING CHANGE: useTemporaryDirectory() now returns an object instead of a string --- README.md | 12 +++++++++--- package.json | 1 + use-temporary-directory/index.js | 14 +++++++++++++- use-temporary-directory/index.test.js | 15 ++++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d648563..79c2418 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,21 @@ yarn add ava-patterns Create a temporary directory and delete it (and its contents) at the end of the test. +Returns an object with the following members: + +- `path`: The absolute path to the temporary directory. +- `writeFile(filePath, fileContents)`: Write a file with path relative to the + temporary directory. Any leading whitespace in the file contents is stripped. + ```js import test from 'ava' -import * as path from 'path' -import {promises as fs} from 'fs' import {useTemporaryDirectory} from 'ava-patterns' test('writing files', async (t) => { const directory = await useTemporaryDirectory(t) - await fs.writeFile(path.join(directory, 'file.txt'), 'Hello World!') + await directory.write('file.txt', ` + Hello World! + `) t.pass() }) ``` diff --git a/package.json b/package.json index 1b97efb..f7aefca 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "type": "module", "dependencies": { "fs-extra": "^9.0.1", + "strip-indent": "^3.0.0", "tempy": "^0.6.0" }, "devDependencies": { diff --git a/use-temporary-directory/index.js b/use-temporary-directory/index.js index 5b09d71..10efa65 100644 --- a/use-temporary-directory/index.js +++ b/use-temporary-directory/index.js @@ -1,11 +1,23 @@ +import * as path from 'path' import fs from 'fs-extra' import tempy from 'tempy' +import stripIndent from 'strip-indent' export default async function (t) { const directory = tempy.directory() + await fs.ensureDir(directory) t.teardown(async () => { await fs.remove(directory) }) - return directory + + return { + path: directory, + async writeFile(filePath, fileContents) { + await fs.writeFile( + path.join(directory, filePath), + stripIndent(fileContents.trim()) + '\n' + ) + } + } } diff --git a/use-temporary-directory/index.test.js b/use-temporary-directory/index.test.js index c2d1a73..1aa6c25 100644 --- a/use-temporary-directory/index.test.js +++ b/use-temporary-directory/index.test.js @@ -1,13 +1,26 @@ import test from 'ava' +import * as path from 'path' import {promises as fs} from 'fs' import {useTemporaryDirectory} from '../index.js' test.serial('creating a directory', async (t) => { const directory = await useTemporaryDirectory(t) - const stats = await fs.stat(directory) + const stats = await fs.stat(directory.path) t.true(stats.isDirectory()) + await directory.writeFile( + 'file.txt', + ` + Hello World! + ` + ) + + t.is( + await fs.readFile(path.join(directory.path, 'file.txt'), 'utf8'), + 'Hello World!\n' + ) + global.directory = directory })