Skip to content

Commit

Permalink
fix #133, #134
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jul 4, 2017
1 parent d72a47a commit b9b5375
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ function isEBADF(error) {
* Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
*/
function isENOENT(error) {
return isExpectedError(error, -EBADF, 'EBADF');
return isExpectedError(error, -ENOENT, 'ENOENT');
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
existsSync = fs.existsSync || path.existsSync,
tmp = require('../lib/tmp');

var _cleanup_fn = undefined;

// FIXME:does not seem to work at all
// make sure that we do not test spam the global tmp
tmp.TMP_DIR = './tmp';

Expand Down Expand Up @@ -200,6 +205,18 @@ function _testIssue115FileSync(cb) {
_spawnTestWithError('issue115-file-sync.js', [], cb);
}

function _testCleanup(err, name, fd, fn) {
var actual_fn = fn;
if (typeof(fd) == 'function') actual_fn = fd;
if (actual_fn) actual_fn();
assert.ok(!existsSync(name), 'should not exist');
}

function _testCleanupSync(result) {
result.removeCallback();
assert.ok(!existsSync(result.name), 'should not exist');
}

module.exports.testStat = _testStat;
module.exports.testPrefix = _testPrefix;
module.exports.testPrefixSync = _testPrefixSync;
Expand All @@ -220,3 +237,5 @@ module.exports.testIssue115File = _testIssue115File;
module.exports.testIssue115FileSync = _testIssue115FileSync;
module.exports.testUnsafeCleanupSync = _testUnsafeCleanupSync;
module.exports.testIssue62Sync = _testIssue62Sync;
module.exports.testCleanup = _testCleanup;
module.exports.testCleanupSync = _testCleanupSync;
23 changes: 14 additions & 9 deletions test/dir-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ vows.describe('Synchronous directory creation').addBatch({

'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040700),
'should have the default prefix': Test.testPrefixSync('tmp-')
'should have the default prefix': Test.testPrefixSync('tmp-'),
'should have been cleaned up': Test.testCleanupSync
},

'when using with prefix': {
Expand All @@ -41,7 +42,8 @@ vows.describe('Synchronous directory creation').addBatch({

'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040700),
'should have the provided prefix': Test.testPrefixSync('something')
'should have the provided prefix': Test.testPrefixSync('something'),
'should have been cleaned up': Test.testCleanupSync
},

'when using with postfix': {
Expand All @@ -51,7 +53,8 @@ vows.describe('Synchronous directory creation').addBatch({

'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040700),
'should have the provided postfix': Test.testPostfixSync('.txt')
'should have the provided postfix': Test.testPostfixSync('.txt'),
'should have been cleaned up': Test.testCleanupSync
},

'when using template': {
Expand All @@ -62,7 +65,8 @@ vows.describe('Synchronous directory creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040700),
'should have the provided prefix': Test.testPrefixSync('clike-'),
'should have the provided postfix': Test.testPostfixSync('-postfix')
'should have the provided postfix': Test.testPostfixSync('-postfix'),
'should have been cleaned up': Test.testCleanupSync
},

'when using name': {
Expand All @@ -74,9 +78,8 @@ vows.describe('Synchronous directory creation').addBatch({
'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name')),
'should be a directory': function (result) {
_testDir(040700)(result);
result.removeCallback();
assert.ok(!existsSync(result.name), 'Directory should be removed');
}
},
'should have been cleaned up': Test.testCleanupSync
},

'when using multiple options': {
Expand All @@ -87,7 +90,8 @@ vows.describe('Synchronous directory creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040750),
'should have the provided prefix': Test.testPrefixSync('foo'),
'should have the provided postfix': Test.testPostfixSync('bar')
'should have the provided postfix': Test.testPostfixSync('bar'),
'should have been cleaned up': Test.testCleanupSync
},

'when using multiple options and mode': {
Expand All @@ -98,7 +102,8 @@ vows.describe('Synchronous directory creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a directory': _testDir(040755),
'should have the provided prefix': Test.testPrefixSync('complicated'),
'should have the provided postfix': Test.testPostfixSync('options')
'should have the provided postfix': Test.testPostfixSync('options'),
'should have been cleaned up': Test.testCleanupSync
},

'no tries': {
Expand Down
23 changes: 15 additions & 8 deletions test/dir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var
tmp = require('../lib/tmp.js'),
Test = require('./base.js');


function _testDir(mode) {
return function _testDirGenerated(err, name) {
assert.ok(existsSync(name), 'should exist');
Expand All @@ -23,14 +22,16 @@ function _testDir(mode) {
};
}


vows.describe('Directory creation').addBatch({
'when using without parameters': {
topic: function () {
tmp.dir(this.callback);
},

'should be a directory': _testDir(040700),
'should have the default prefix': Test.testPrefix('tmp-')
'should have the default prefix': Test.testPrefix('tmp-'),
'should have been cleaned up': Test.testCleanup
},

'when using with prefix': {
Expand All @@ -41,7 +42,8 @@ vows.describe('Directory creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700),
'should have the provided prefix': Test.testPrefix('something')
'should have the provided prefix': Test.testPrefix('something'),
'should have been cleaned up': Test.testCleanup
},

'when using with postfix': {
Expand All @@ -52,7 +54,8 @@ vows.describe('Directory creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700),
'should have the provided postfix': Test.testPostfix('.txt')
'should have the provided postfix': Test.testPostfix('.txt'),
'should have been cleaned up': Test.testCleanup
},

'when using template': {
Expand All @@ -64,7 +67,8 @@ vows.describe('Directory creation').addBatch({
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700),
'should have the provided prefix': Test.testPrefix('clike-'),
'should have the provided postfix': Test.testPostfix('-postfix')
'should have the provided postfix': Test.testPostfix('-postfix'),
'should have been cleaned up': Test.testCleanup
},

'when using name': {
Expand All @@ -75,7 +79,8 @@ vows.describe('Directory creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700),
'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name'))
'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name')),
'should have been cleaned up': Test.testCleanup
},

'when using multiple options': {
Expand All @@ -87,7 +92,8 @@ vows.describe('Directory creation').addBatch({
'should return with a name': Test.assertName,
'should be a directory': _testDir(040750),
'should have the provided prefix': Test.testPrefix('foo'),
'should have the provided postfix': Test.testPostfix('bar')
'should have the provided postfix': Test.testPostfix('bar'),
'should have been cleaned up': Test.testCleanup
},

'when using multiple options and mode': {
Expand All @@ -99,7 +105,8 @@ vows.describe('Directory creation').addBatch({
'should return with a name': Test.assertName,
'should be a directory': _testDir(040755),
'should have the provided prefix': Test.testPrefix('complicated'),
'should have the provided postfix': Test.testPostfix('options')
'should have the provided postfix': Test.testPostfix('options'),
'should have been cleaned up': Test.testCleanup
},

'no tries': {
Expand Down
26 changes: 16 additions & 10 deletions test/file-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ vows.describe('Synchronous file creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100600, true),
'should have the default prefix': Test.testPrefixSync('tmp-'),
'should have the default postfix': Test.testPostfixSync('.tmp')
'should have the default postfix': Test.testPostfixSync('.tmp'),
'should have been cleaned up': Test.testCleanupSync
},

'when using with prefix': {
Expand All @@ -53,7 +54,8 @@ vows.describe('Synchronous file creation').addBatch({

'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100600, true),
'should have the provided prefix': Test.testPrefixSync('something')
'should have the provided prefix': Test.testPrefixSync('something'),
'should have been cleaned up': Test.testCleanupSync
},

'when using with postfix': {
Expand All @@ -63,7 +65,8 @@ vows.describe('Synchronous file creation').addBatch({

'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100600, true),
'should have the provided postfix': Test.testPostfixSync('.txt')
'should have the provided postfix': Test.testPostfixSync('.txt'),
'should have been cleaned up': Test.testCleanupSync
},

'when using template': {
Expand All @@ -74,20 +77,21 @@ vows.describe('Synchronous file creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100600, true),
'should have the provided prefix': Test.testPrefixSync('clike-'),
'should have the provided postfix': Test.testPostfixSync('-postfix')
'should have the provided postfix': Test.testPostfixSync('-postfix'),
'should have been cleaned up': Test.testCleanupSync
},

'when using name': {
topic: function () {
return tmp.fileSync({ name: 'using-name-sync.tmp' });
return tmp.fileSync({ name: 'using-name' });
},

'should return with a name': Test.assertNameSync,
'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name-sync.tmp')),
'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name')),
'should be a file': function (result) {
_testFile(0100600, true);
fs.unlinkSync(result.name);
}
},
'should have been cleaned up': Test.testCleanupSync
},

'when using multiple options': {
Expand All @@ -98,7 +102,8 @@ vows.describe('Synchronous file creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100640, true),
'should have the provided prefix': Test.testPrefixSync('foo'),
'should have the provided postfix': Test.testPostfixSync('bar')
'should have the provided postfix': Test.testPostfixSync('bar'),
'should have been cleaned up': Test.testCleanupSync
},

'when using multiple options and mode': {
Expand All @@ -109,7 +114,8 @@ vows.describe('Synchronous file creation').addBatch({
'should return with a name': Test.assertNameSync,
'should be a file': _testFile(0100644, true),
'should have the provided prefix': Test.testPrefixSync('complicated'),
'should have the provided postfix': Test.testPostfixSync('options')
'should have the provided postfix': Test.testPostfixSync('options'),
'should have been cleaned up': Test.testCleanupSync
},

'no tries': {
Expand Down
24 changes: 16 additions & 8 deletions test/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should be a file': _testFile(0100600, true),
'should have the default prefix': Test.testPrefix('tmp-'),
'should have the default postfix': Test.testPostfix('.tmp')
'should have the default postfix': Test.testPostfix('.tmp'),
'should have been cleaned up': Test.testCleanup
},

'when using with prefix': {
Expand All @@ -84,7 +85,8 @@ vows.describe('File creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a file': _testFile(0100600, true),
'should have the provided prefix': Test.testPrefix('something')
'should have the provided prefix': Test.testPrefix('something'),
'should have been cleaned up': Test.testCleanup
},

'when using with postfix': {
Expand All @@ -95,7 +97,8 @@ vows.describe('File creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a file': _testFile(0100600, true),
'should have the provided postfix': Test.testPostfix('.txt')
'should have the provided postfix': Test.testPostfix('.txt'),
'should have been cleaned up': Test.testCleanup
},

'when using template': {
Expand All @@ -107,7 +110,8 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should be a file': _testFile(0100600, true),
'should have the provided prefix': Test.testPrefix('clike-'),
'should have the provided postfix': Test.testPostfix('-postfix')
'should have the provided postfix': Test.testPostfix('-postfix'),
'should have been cleaned up': Test.testCleanup
},

'when using name': {
Expand All @@ -121,7 +125,8 @@ vows.describe('File creation').addBatch({
'should be a file': function (err, name) {
_testFile(0100600, true);
fs.unlinkSync(name);
}
},
'should have been cleaned up': Test.testCleanup
},

'when using discardDescriptor': {
Expand All @@ -133,6 +138,7 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should not return with a descriptor': Test.assertNoDescriptor,
'should be a file': _testFileNoDescriptor(0100600),
'should have been cleaned up': Test.testCleanup
},

'when using detachDescriptor': {
Expand All @@ -147,6 +153,7 @@ vows.describe('File creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should have working descriptor after removeCallback': _testFileAfterDetachRemove(0100600),
'should have been cleaned up': Test.testCleanup
},

'when using multiple options': {
Expand All @@ -158,7 +165,8 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should be a file': _testFile(0100640, true),
'should have the provided prefix': Test.testPrefix('foo'),
'should have the provided postfix': Test.testPostfix('bar')
'should have the provided postfix': Test.testPostfix('bar'),
'should have been cleaned up': Test.testCleanup
},

'when using multiple options and mode': {
Expand All @@ -170,7 +178,8 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should be a file': _testFile(0100644, true),
'should have the provided prefix': Test.testPrefix('complicated'),
'should have the provided postfix': Test.testPostfix('options')
'should have the provided postfix': Test.testPostfix('options'),
'should have been cleaned up': Test.testCleanup
},

'no tries': {
Expand All @@ -190,7 +199,6 @@ vows.describe('File creation').addBatch({
'should return with a name': Test.assertName,
'should be a file': function (err, name) {
_testFile(0100600, false)(err, name, null);
fs.unlinkSync(name);
}
},

Expand Down

0 comments on commit b9b5375

Please sign in to comment.