Skip to content

Commit 25daefa

Browse files
committed
Fixes #115
Add node 7 to travis build matrix
1 parent 42cd5f7 commit 25daefa

File tree

7 files changed

+134
-5
lines changed

7 files changed

+134
-5
lines changed

.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: 44 additions & 4 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
}
@@ -367,20 +369,20 @@ function dirSync(options) {
367369
function _prepareTmpFileRemoveCallback(name, fd, opts) {
368370
const removeCallback = _prepareRemoveCallback(function _removeCallback(fdPath) {
369371
try {
370-
if (0 <= fdPath[0]) {
372+
if (-1 != fdPath[0]) {
371373
fs.closeSync(fdPath[0]);
372374
}
375+
fs.unlinkSync(fdPath[1]);
373376
}
374377
catch (e) {
375378
// under some node/windows related circumstances, a temporary file
376379
// may have not be created as expected or the file was already closed
377380
// by the user, in which case we will simply ignore the error
378-
if (e.errno != -EBADF && e.errno != -ENOENT) {
381+
if (!isEBADF(e) && !isENOENT(e)) {
379382
// reraise any unanticipated error
380383
throw e;
381384
}
382385
}
383-
fs.unlinkSync(fdPath[1]);
384386
}, [fd, name]);
385387

386388
if (!opts.keep) {
@@ -456,6 +458,44 @@ function _garbageCollector() {
456458
}
457459
}
458460

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

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: 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 deletes it as per issue 115
15+
// https://github.com/raszi/node-tmp/issues/115
16+
17+
tmpobj = tmp.fileSync();
18+
fs.closeSync(tmpobj.fd);
19+
fs.unlinkSync(tmpobj.name);
20+
tmpobj.removeCallback();
21+
spawn.out(tmpobj.name, spawn.exit);
22+
} catch (e) {
23+
spawn.err(e.toString() + ' with code: ' + e.code + ' and errno: ' + e.errno, spawn.exit);
24+
}
25+
});
26+

test/issue115-file.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 deletes 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+
fs.unlinkSync(name);
20+
callback();
21+
spawn.out(name, spawn.exit);
22+
});
23+
} catch (e) {
24+
spawn.err(e.toString() + ' with code: ' + e.code + ' and errno: ' + e.errno, spawn.exit);
25+
}
26+
});
27+

0 commit comments

Comments
 (0)