Skip to content

Commit

Permalink
Fix regression when options are omitted
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow committed May 15, 2019
1 parent 6bd19f9 commit 62b3102
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
97 changes: 89 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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')
})
})
Expand Down

0 comments on commit 62b3102

Please sign in to comment.