Skip to content

Commit

Permalink
fix #210: regression after bug fix for #197
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jan 27, 2020
1 parent 959a39f commit 3e67fe4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
11 changes: 7 additions & 4 deletions lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const
SIGINT = 'SIGINT',

// this will hold the objects need to be removed on exit
_removeObjects = [];
_removeObjects = [],

// API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
FN_RMDIR_SYNC = fs.rmdirSync.bind(fs);

var
_gracefulCleanup = false;
Expand Down Expand Up @@ -253,7 +256,7 @@ function file(options, callback) {

// FIXME overall handling of opts.discardDescriptor is off
if (opts.discardDescriptor) {
// FIXME must not unlink
// FIXME? must not unlink as the user expects the filename to be reserved
return fs.close(fd, function _discardCallback(err) {
/* istanbul ignore else */
if (err) {
Expand Down Expand Up @@ -464,7 +467,7 @@ function _rimrafRemoveDirSyncWrapper(dirPath, next) {
*/
function _prepareTmpDirRemoveCallback(name, opts, sync) {
const removeFunction = opts.unsafeCleanup ? _rimrafRemoveDirWrapper : fs.rmdir.bind(fs);
const removeFunctionSync = opts.unsafeCleanup ? _rimrafRemoveDirSyncWrapper : fs.rmdirSync.bind(fs);
const removeFunctionSync = opts.unsafeCleanup ? _rimrafRemoveDirSyncWrapper : FN_RMDIR_SYNC;
const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync);
const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync);
if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
Expand Down Expand Up @@ -500,7 +503,7 @@ function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCall
if (index >= 0) _removeObjects.splice(index, 1);

called = true;
if (sync) {
if (sync || removeFunction === FN_RMDIR_SYNC) {
return removeFunction(fileOrDirName);
} else {
return removeFunction(fileOrDirName, next || function() {});
Expand Down
9 changes: 4 additions & 5 deletions test/dir-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
inbandStandardTests = require('./inband-standard'),
childProcess = require('./child-process').genericChildProcess,
Expand All @@ -20,7 +19,7 @@ describe('tmp', function () {
describe('when running inband standard tests', function () {
inbandStandardTests(false, function before() {
this.topic = tmp.dirSync(this.opts);
});
}, true);

describe('with invalid tries', function () {
it('should result in an error on negative tries', function () {
Expand Down Expand Up @@ -68,8 +67,8 @@ describe('tmp', function () {
if (!stderr) return done(new Error('stderr expected'));
try {
assertions.assertExists(stdout);
} catch (err) {
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
Expand All @@ -82,8 +81,8 @@ describe('tmp', function () {
if (stderr) return done(new Error(stderr));
try {
assertions.assertExists(stdout);
} catch (err) {
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
Expand Down Expand Up @@ -130,8 +129,8 @@ describe('tmp', function () {
} else {
assertions.assertExists(path.join(stdout, 'symlinkme-target'));
}
} catch (err) {
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
Expand Down
20 changes: 16 additions & 4 deletions test/file-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

var
assert = require('assert'),
fs = require('fs'),
inbandStandardTests = require('./inband-standard'),
assertions = require('./assertions'),
childProcess = require('./child-process').genericChildProcess,
Expand All @@ -20,7 +19,7 @@ describe('tmp', function () {
describe('when running inband standard tests', function () {
inbandStandardTests(true, function before() {
this.topic = tmp.fileSync(this.opts);
});
}, true);

describe('with invalid tries', function () {
it('should result in an error on negative tries', function () {
Expand Down Expand Up @@ -71,8 +70,8 @@ describe('tmp', function () {
if (!stderr) return done(new Error('stderr expected'));
try {
assertions.assertExists(stdout, true);
} catch (err) {
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
Expand All @@ -85,8 +84,8 @@ describe('tmp', function () {
if (stderr) return done(new Error(stderr));
try {
assertions.assertExists(stdout, true);
} catch (err) {
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
Expand Down Expand Up @@ -122,6 +121,19 @@ describe('tmp', function () {
done();
});
});
it('on issue #115', function (done) {
childProcess(this, 'issue115-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
if (stderr) return done(new Error(stderr));
try {
assertions.assertDoesNotExist(stdout);
} catch (err) {
rimraf.sync(stdout);
return done(err);
}
done();
});
});
});
});
});
64 changes: 35 additions & 29 deletions test/inband-standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@

var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync,
assertions = require('./assertions'),
rimraf = require('rimraf'),
tmp = require('../lib/tmp');


module.exports = function inbandStandard(isFile, beforeHook) {
var testMode = isFile ? 0600 : 0700;
describe('without any parameters', inbandStandardTests({ mode: testMode, prefix: 'tmp-' }, null, isFile, beforeHook));
describe('with prefix', inbandStandardTests({ mode: testMode }, { prefix: 'something' }, isFile, beforeHook));
describe('with postfix', inbandStandardTests({ mode: testMode }, { postfix: '.txt' }, isFile, beforeHook));
describe('with template and no leading path', inbandStandardTests({ mode: testMode, prefix: 'clike-', postfix: '-postfix' }, { template: 'clike-XXXXXX-postfix' }, isFile, beforeHook));
describe('with template and leading path', inbandStandardTests({ mode: testMode, prefix: 'clike-', postfix: '-postfix' }, { template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix')}, isFile, beforeHook));
describe('with name', inbandStandardTests({ mode: testMode }, { name: 'using-name' }, isFile, beforeHook));
describe('with mode', inbandStandardTests(null, { mode: 0755 }, isFile, beforeHook));
describe('with multiple options', inbandStandardTests(null, { prefix: 'foo', postfix: 'bar', mode: 0750 }, isFile, beforeHook));
module.exports = function inbandStandard(isFile, beforeHook, sync = false) {
var testMode = isFile ? 0o600 : 0o700;
describe('without any parameters', inbandStandardTests({ mode: testMode, prefix: 'tmp-' }, null, isFile, beforeHook, sync));
describe('with prefix', inbandStandardTests({ mode: testMode, prefix: 'tmp-' }, { prefix: 'tmp-something' }, isFile, beforeHook, sync));
describe('with postfix', inbandStandardTests({ mode: testMode, prefix: 'tmp-' }, { postfix: '.txt' }, isFile, beforeHook, sync));
describe('with template and no leading path', inbandStandardTests({ mode: testMode, prefix: 'tmp-clike-', postfix: '-postfix' }, { template: 'tmp-clike-XXXXXX-postfix' }, isFile, beforeHook, sync));
describe('with template and leading path', inbandStandardTests({ mode: testMode, prefix: 'tmp-clike-', postfix: '-postfix' }, { template: path.join(tmp.tmpdir, 'tmp-clike-XXXXXX-postfix')}, isFile, beforeHook, sync));
describe('with name', inbandStandardTests({ mode: testMode }, { name: 'tmp-using-name' }, isFile, beforeHook, sync));
describe('with mode', inbandStandardTests(null, { mode: 0o755 }, isFile, beforeHook, sync));
describe('with multiple options', inbandStandardTests(null, { prefix: 'tmp-multiple', postfix: 'bar', mode: 0o750 }, isFile, beforeHook, sync));
if (isFile) {
describe('with discardDescriptor', inbandStandardTests(null, { mode: testMode, discardDescriptor: true }, isFile, beforeHook));
describe('with detachDescriptor', inbandStandardTests(null, { mode: testMode, detachDescriptor: true }, isFile, beforeHook));
describe('with discardDescriptor', inbandStandardTests(null, { mode: testMode, discardDescriptor: true }, isFile, beforeHook, sync));
describe('with detachDescriptor', inbandStandardTests(null, { mode: testMode, detachDescriptor: true }, isFile, beforeHook, sync));
}
};


function inbandStandardTests(testOpts, opts, isFile, beforeHook) {
function inbandStandardTests(testOpts, opts, isFile, beforeHook, sync = false) {
return function () {
opts = opts || {};
testOpts = testOpts || {};
Expand All @@ -51,7 +49,7 @@ function inbandStandardTests(testOpts, opts, isFile, beforeHook) {
assertions.assertMode(this.topic.name, testOpts.mode || opts.mode);
}.bind(topic));

if (opts.prefix || testOpts.prefix) {
if(testOpts.prefix || opts.prefix) {
it('should have the expected prefix', function () {
assertions.assertPrefix(this.topic.name, testOpts.prefix || opts.prefix);
}.bind(topic));
Expand All @@ -73,18 +71,26 @@ function inbandStandardTests(testOpts, opts, isFile, beforeHook) {
}.bind(topic));
}

it('should have a working removeCallback', function (done) {
const self = this;
this.topic.removeCallback(function (err) {
if (err) return done(err);
try {
assertions.assertDoesNotExist(self.topic.name);
} catch (err) {
rimraf.sync(self.topic.name);
return done(err);
}
done();
});
}.bind(topic));
if (sync) {
it('should have a working removeCallback', function () {
assert.ok(typeof this.topic.removeCallback === 'function');
// important: remove file or dir unconditionally
rimraf.sync(this.topic.name);
}.bind(topic));
} else {
it('should have a working removeCallback', function (done) {
const self = this;
this.topic.removeCallback(function (err) {
if (err) return done(err);
try {
assertions.assertDoesNotExist(self.topic.name);
} catch (err) {
rimraf.sync(self.topic.name);
return done(err);
}
done();
});
}.bind(topic));
}
};
}

0 comments on commit 3e67fe4

Please sign in to comment.