diff --git a/index.js b/index.js index 08a493b..b9e67ce 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,13 @@ const tmp = require("tmp"); // file module.exports.fileSync = tmp.fileSync; -module.exports.file = promisify( +const fileWithOptions = promisify( (options, cb) => tmp.file( options, (err, path, fd, cleanup) => cb(err, {path, fd, cleanup}) ) ); +module.exports.file = async (options) => fileWithOptions(options); module.exports.withFile = async function withFile(fn, options) { const { path, fd, cleanup } = await module.exports.file(options); @@ -22,12 +23,13 @@ module.exports.withFile = async function withFile(fn, options) { // directory module.exports.dirSync = tmp.dirSync; -module.exports.dir = promisify( +const dirWithOptions = promisify( (options, cb) => tmp.dir( options, (err, path, cleanup) => cb(err, {path, cleanup}) ) ); +module.exports.dir = async (options) => dirWithOptions(options); module.exports.withDir = async function withDir(fn, options) { const { path, cleanup } = await module.exports.dir(options); diff --git a/test.js b/test.js index 7921555..f7155e0 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,92 @@ -const accessSync = require('fs').accessSync +const fs = require('fs') +const {promisify} = require('util') const assert = require('assert') -const extname = require('path').extname +const {extname} = require('path') const tmp = require('.') +async function checkFileResult(result) { + assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'fd', 'path']) -describe('withFile', function() + const { path, fd, cleanup } = result + assert.ok(typeof path === 'string') + assert.ok(typeof fd === 'number') + assert.ok(typeof cleanup === 'function') + + // Check that the path is a fille. + assert.ok(fs.statSync(path).isFile()) + + // Check that the fd is correct and points to the file. + const message = 'hello there!' + fs.writeSync(fd, message) + fs.fdatasyncSync(fd) + assert.equal(fs.readFileSync(path), message) + + // Check that the cleanup works. + await promisify(cleanup)() + assert.throws(() => fs.statSync(path)) +} + +describe('file()', function() +{ + context('when called without options', function() + { + it('creates the file, returns the expected result, and the cleanup function works', async function() + { + const result = await tmp.file() + await checkFileResult(result) + }) + }) + + context('when called with options', function() + { + it('creates the file, returns the expected result, and the cleanup function works', async function() + { + const prefix = 'myTmpDir_' + const result = await tmp.file({ prefix }) + await checkFileResult(result) + assert.ok(result.path.includes(prefix)) + }) + }) +}) + +async function checkDirResult(result) { + assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'path']) + + const { path, cleanup } = result + assert.ok(typeof path === 'string') + assert.ok(typeof cleanup === 'function') + + assert.ok(fs.statSync(path).isDirectory()) + + await promisify(cleanup)() + assert.throws(() => fs.statSync(path)) +} + +describe('dir()', function() +{ + context('when called without options', function() + { + it('creates the directory, returns the expected result, and the cleanup function works', async function() + { + const result = await tmp.dir() + await checkDirResult(result) + }) + }) + + context('when called with options', function() + { + it('creates the directory, returns the expected result, and the cleanup function works', async function() + { + const prefix = 'myTmpDir_' + const result = await tmp.dir({ prefix }) + await checkDirResult(result) + assert.ok(result.path.includes(prefix)) + }) + }) +}) + +describe('withFile()', function() { it("file doesn't exist after going out of scope", function() { @@ -15,21 +96,21 @@ describe('withFile', function() { filepath = o.path - accessSync(filepath) + fs.accessSync(filepath) assert.strictEqual(extname(filepath), '.txt') }, {discardDescriptor: true, postfix: '.txt'}) .then(function() { assert.throws(function() { - accessSync(filepath) + fs.accessSync(filepath) }, filepath + ' still exists') }) }) }) -describe('withDir', function() +describe('withDir()', function() { it("dir doesn't exist after going out of scope", function() { @@ -39,14 +120,14 @@ describe('withDir', function() { filepath = o.path - accessSync(filepath) + fs.accessSync(filepath) assert.strictEqual(extname(filepath), '.dir') }, {postfix: '.dir'}) .then(function() { assert.throws(function() { - accessSync(filepath) + fs.accessSync(filepath) }, filepath + ' still exists') }) })