Skip to content

Commit ce1fad9

Browse files
authored
Merge pull request #119 from raszi/gh-115
Fixes #115
2 parents 42cd5f7 + d16ec51 commit ce1fad9

8 files changed

Lines changed: 140 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
.idea/
3+
.*.swp

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ node_js:
2323
- "5.11"
2424
- "6.0"
2525
- "6.1"
26+
- "7"
2627
- "node"
2728
sudo: false
2829
cache:

lib/tmp.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ function file(options, callback) {
233233
try {
234234
fs.unlinkSync(name);
235235
} catch (e) {
236-
err = e;
236+
if (!isENOENT(e)) {
237+
err = e;
238+
}
237239
}
238240
return cb(err);
239241
}
@@ -375,12 +377,20 @@ function _prepareTmpFileRemoveCallback(name, fd, opts) {
375377
// under some node/windows related circumstances, a temporary file
376378
// may have not be created as expected or the file was already closed
377379
// by the user, in which case we will simply ignore the error
378-
if (e.errno != -EBADF && e.errno != -ENOENT) {
380+
if (!isEBADF(e) && !isENOENT(e)) {
381+
// reraise any unanticipated error
382+
throw e;
383+
}
384+
}
385+
try {
386+
fs.unlinkSync(fdPath[1]);
387+
}
388+
catch (e) {
389+
if (!isENOENT(e)) {
379390
// reraise any unanticipated error
380391
throw e;
381392
}
382393
}
383-
fs.unlinkSync(fdPath[1]);
384394
}, [fd, name]);
385395

386396
if (!opts.keep) {
@@ -456,6 +466,44 @@ function _garbageCollector() {
456466
}
457467
}
458468

469+
/**
470+
* Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
471+
*/
472+
function isEBADF(error) {
473+
return isExpectedError(error, -EBADF, 'EBADF');
474+
}
475+
476+
/**
477+
* Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
478+
*/
479+
function isENOENT(error) {
480+
return isExpectedError(error, -EBADF, 'EBADF');
481+
}
482+
483+
/**
484+
* Helper to determine whether the expected error code matches the actual code and errno,
485+
* which will differ between the supported node versions.
486+
*
487+
* - Node >= 7.0:
488+
* error.code {String}
489+
* error.errno {String|Number} any numerical value will be negated
490+
*
491+
* - Node >= 6.0 < 7.0:
492+
* error.code {String}
493+
* error.errno {Number} negated
494+
*
495+
* - Node >= 4.0 < 6.0: introduces SystemError
496+
* error.code {String}
497+
* error.errno {Number} negated
498+
*
499+
* - Node >= 0.10 < 4.0:
500+
* error.code {Number} negated
501+
* error.errno n/a
502+
*/
503+
function isExpectedError(error, code, errno) {
504+
return error.code == code || error.code == errno;
505+
}
506+
459507
/**
460508
* Sets the graceful cleanup.
461509
*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tmp",
3-
"version": "0.0.31",
3+
"version": "0.0.32",
44
"description": "Temporary file and directory creator",
55
"author": "KARASZI István <github@spam.raszi.hu> (http://raszi.hu/)",
66
"keywords": [

test/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ function _testIssue62Sync(cb) {
192192
_spawnTestWithoutError('issue62-sync.js', [], cb);
193193
}
194194

195+
function _testIssue115File(cb) {
196+
_spawnTestWithError('issue115-file.js', [], cb);
197+
}
198+
199+
function _testIssue115FileSync(cb) {
200+
_spawnTestWithError('issue115-file-sync.js', [], cb);
201+
}
202+
195203
module.exports.testStat = _testStat;
196204
module.exports.testPrefix = _testPrefix;
197205
module.exports.testPrefixSync = _testPrefixSync;
@@ -208,5 +216,7 @@ module.exports.testName = _testName;
208216
module.exports.testNameSync = _testNameSync;
209217
module.exports.testUnsafeCleanup = _testUnsafeCleanup;
210218
module.exports.testIssue62 = _testIssue62;
219+
module.exports.testIssue115File = _testIssue115File;
220+
module.exports.testIssue115FileSync = _testIssue115FileSync;
211221
module.exports.testUnsafeCleanupSync = _testUnsafeCleanupSync;
212222
module.exports.testIssue62Sync = _testIssue62Sync;

test/file-test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,30 @@ vows.describe('File creation').addBatch({
242242
removeCallback();
243243
assert.ok(!existsSync(name), 'File should be removed');
244244
}
245-
}
245+
},
246+
247+
'issue115 async: user deleted tmp file prior to cleanup': {
248+
topic: function () {
249+
Test.testIssue115File(this.callback);
250+
},
251+
252+
'should not return with an error': assert.isNull,
253+
'should return with a name': Test.assertName,
254+
'should not exist': function (err, name) {
255+
assert.ok(!existsSync(name), 'File should be removed');
256+
}
257+
},
258+
259+
'issue115 sync: user deleted tmp file prior to cleanup': {
260+
topic: function () {
261+
Test.testIssue115FileSync(this.callback);
262+
},
263+
264+
'should not return with an error': assert.isNull,
265+
'should return with a name': Test.assertName,
266+
'should not exist': function (err, name) {
267+
assert.ok(!existsSync(name), 'File should be removed');
268+
}
269+
},
246270

247271
}).exportTo(module);

test/issue115-file-sync.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var
2+
fs = require('fs'),
3+
join = require('path').join,
4+
tmp = require('../lib/tmp'),
5+
spawn = require('./spawn');
6+
7+
spawn.tmpFunction({ unsafeCleanup: true }, function (err, name) {
8+
if (err) {
9+
spawn.err(err, spawn.exit);
10+
return;
11+
}
12+
13+
try {
14+
// creates a tmp file and then closes the file descriptor as per issue 115
15+
// https://github.com/raszi/node-tmp/issues/115
16+
17+
tmpobj = tmp.fileSync();
18+
fs.closeSync(tmpobj.fd);
19+
tmpobj.removeCallback();
20+
spawn.out(tmpobj.name, spawn.exit);
21+
} catch (e) {
22+
spawn.err(e.toString() + ' with code: ' + e.code + ' and errno: ' + e.errno, spawn.exit);
23+
}
24+
});
25+

test/issue115-file.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var
2+
fs = require('fs'),
3+
join = require('path').join,
4+
tmp = require('../lib/tmp'),
5+
spawn = require('./spawn');
6+
7+
spawn.tmpFunction({ unsafeCleanup: true }, function (err, name) {
8+
if (err) {
9+
spawn.err(err, spawn.exit);
10+
return;
11+
}
12+
13+
try {
14+
// creates a tmp file and then closes the file descriptor it as per issue 115
15+
// https://github.com/raszi/node-tmp/issues/115
16+
17+
tmp.file(function (err, name, fd, callback) {
18+
fs.closeSync(fd);
19+
callback();
20+
spawn.out(name, spawn.exit);
21+
});
22+
} catch (e) {
23+
spawn.err(e.toString() + ' with code: ' + e.code + ' and errno: ' + e.errno, spawn.exit);
24+
}
25+
});
26+

0 commit comments

Comments
 (0)