Comparing version 0.3.3 to 0.3.4
@@ -0,1 +1,5 @@ | ||
# 0.3.4 / 2013-11-23 | ||
- changed to throw error if error code is not EEXIST | ||
# 0.3.3 / 2013-11-14 | ||
@@ -2,0 +6,0 @@ |
@@ -22,10 +22,18 @@ /*! | ||
if (err) { | ||
// FIXME: infinite loop | ||
createFile(template, callback); | ||
return; | ||
if (err.code === 'EEXIST') { | ||
// FIXME: infinite loop | ||
createFile(template, callback); | ||
return; | ||
} | ||
filename = null; | ||
} | ||
fs.close(fd, function(err) { | ||
if (fd) { | ||
fs.close(fd, function(err) { | ||
callback(err, filename); | ||
}); | ||
} else { | ||
callback(err, filename); | ||
}); | ||
} | ||
}); | ||
@@ -52,3 +60,7 @@ } | ||
} catch (e) { | ||
overlap = true; | ||
if (e.code === 'EEXIST') { | ||
overlap = true; | ||
} else { | ||
throw e; | ||
} | ||
} finally { | ||
@@ -74,5 +86,9 @@ fd && fs.closeSync(fd); | ||
if (err) { | ||
// FIXME: infinite loop | ||
createDir(template, callback); | ||
return; | ||
if (err.code === 'EEXIST') { | ||
// FIXME: infinite loop | ||
createDir(template, callback); | ||
return; | ||
} | ||
dirname = null; | ||
} | ||
@@ -101,3 +117,7 @@ | ||
} catch (e) { | ||
overlap = true; | ||
if (e.code === 'EEXIST') { | ||
overlap = true; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
@@ -104,0 +124,0 @@ } while (overlap); |
{ | ||
"name": "mktemp", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"author": "sasa+1 <sasaplus1@gmail.com>", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -39,6 +39,6 @@ # mktemp | ||
```js | ||
'XXXXXXXXXXX' // /^[\da-zA-Z]{11}$/ | ||
'abc-XXXXXXX' // /^abc-[\da-zA-Z]{7}$/ | ||
'XXX-XXXXXXX' // /^XXX-[\da-zA-Z]{7}$/ | ||
'XXX-XXX.tmp' // /^XXX-[\da-zA-Z]{3}\.tmp$/ | ||
mktemp.createFileSync('XXXXXXXXXXX'); // match to /^[\da-zA-Z]{11}$/ | ||
mktemp.createFileSync('abc-XXXXXXX'); // match to /^abc-[\da-zA-Z]{7}$/ | ||
mktemp.createFileSync('XXX-XXXXXXX'); // match to /^XXX-[\da-zA-Z]{7}$/ | ||
mktemp.createFileSync('XXX-XXX.tmp'); // match to /^XXX-[\da-zA-Z]{3}\.tmp$/ | ||
``` | ||
@@ -45,0 +45,0 @@ |
@@ -8,30 +8,82 @@ var fs = require('fs'), | ||
before(function() { | ||
sinon.stub(fs, 'open').callsArgWith(3, null); | ||
sinon.stub(fs, 'close').callsArgWith(1, null); | ||
sinon.stub(fs, 'openSync'); | ||
sinon.stub(fs, 'closeSync'); | ||
sinon.stub(fs, 'mkdir').callsArgWith(2, null); | ||
sinon.stub(fs, 'mkdirSync'); | ||
}); | ||
describe('#createFile()', function() { | ||
after(function() { | ||
fs.open.restore(); | ||
fs.openSync.restore(); | ||
fs.close.restore(); | ||
fs.closeSync.restore(); | ||
fs.mkdir.restore(); | ||
fs.mkdirSync.restore(); | ||
}); | ||
describe('success', function() { | ||
describe('#createFile()', function() { | ||
before(function() { | ||
sinon.stub(fs, 'open').callsArgWith(3, null); | ||
sinon.stub(fs, 'close').callsArgWith(1, null); | ||
}); | ||
it('should create file of random name', function(done) { | ||
mktemp.createFile('file-XXXXX', function(err, path) { | ||
expect(path).to.match(/^file-[\da-zA-Z]{5}$/); | ||
expect(path).not.to.be('file-XXXXX'); | ||
done(); | ||
after(function() { | ||
fs.open.restore(); | ||
fs.close.restore(); | ||
}); | ||
it('should create file of random name', function(done) { | ||
mktemp.createFile('file-XXXXX', function(err, path) { | ||
expect(path).to.match(/^file-[\da-zA-Z]{5}$/); | ||
expect(path).not.to.be('file-XXXXX'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('has error, err.code is EEXIST', function() { | ||
before(function() { | ||
var transientObject = { | ||
getCount: 0 | ||
}; | ||
Object.defineProperty(transientObject, 'code', { | ||
get: function() { | ||
// return 'EEXIST' if first call | ||
return (this.getCount++ > 0) ? null : 'EEXIST'; | ||
} | ||
}); | ||
sinon.stub(fs, 'open').callsArgWith(3, transientObject); | ||
sinon.stub(fs, 'close').callsArgWith(1, transientObject); | ||
}); | ||
after(function() { | ||
fs.open.restore(); | ||
fs.close.restore(); | ||
}); | ||
it('should once more call for callback', function(done) { | ||
mktemp.createFile('', function(err, path) { | ||
expect(err.getCount).to.be(2); | ||
expect(err.code).to.be(null); | ||
expect(path).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('has error, err.code is not EEXIST', function() { | ||
before(function() { | ||
sinon.stub(fs, 'open').callsArgWith(3, {code: 'ENOENT'}); | ||
sinon.stub(fs, 'close').callsArgWith(1, {code: 'ENOENT'}); | ||
}); | ||
after(function() { | ||
fs.open.restore(); | ||
fs.close.restore(); | ||
}); | ||
it('should set error to err and set null to path', function(done) { | ||
mktemp.createFile('', function(err, path) { | ||
expect(err).to.eql({code: 'ENOENT'}); | ||
expect(path).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -41,7 +93,77 @@ | ||
it('should create file of random name', function() { | ||
expect(mktemp.createFileSync('XXX')).to.match(/^[\da-zA-Z]{3}$/); | ||
expect(mktemp.createFileSync('XXX')).not.to.be('XXX'); | ||
describe('success', function() { | ||
before(function() { | ||
sinon.stub(fs, 'openSync').returns(1); | ||
sinon.stub(fs, 'closeSync').returns(1); | ||
}); | ||
after(function() { | ||
fs.openSync.restore(); | ||
fs.closeSync.restore(); | ||
}); | ||
it('should create file of random name', function() { | ||
expect(mktemp.createFileSync('XXX')).to.match(/^[\da-zA-Z]{3}$/); | ||
expect(mktemp.createFileSync('XXX')).not.to.be('XXX'); | ||
}); | ||
}); | ||
describe('throws error, err.code is EEXIST', function() { | ||
before(function() { | ||
var transientObject = { | ||
getCount: 0 | ||
}; | ||
Object.defineProperty(transientObject, 'code', { | ||
get: function() { | ||
// return 'EEXIST' if first call | ||
return (this.getCount++ > 0) ? null : 'EEXIST'; | ||
} | ||
}); | ||
sinon.stub(fs, 'openSync').throws(transientObject); | ||
sinon.stub(fs, 'closeSync').throws(transientObject); | ||
}); | ||
after(function() { | ||
fs.openSync.restore(); | ||
fs.closeSync.restore(); | ||
}); | ||
it('should once more call for fs.openSync', function() { | ||
expect(function() { | ||
mktemp.createFileSync(''); | ||
}).to.throwError(function(e) { | ||
expect(e.getCount).to.be(2); | ||
expect(e.code).to.be(null); | ||
}); | ||
}); | ||
}); | ||
describe('throws error, err.code is not EEXIST', function() { | ||
before(function() { | ||
sinon.stub(fs, 'openSync').throws({code: 'ENOENT'}); | ||
sinon.stub(fs, 'closeSync').throws({code: 'ENOENT'}); | ||
}); | ||
after(function() { | ||
fs.openSync.restore(); | ||
fs.closeSync.restore(); | ||
}); | ||
it('should throws error of fs.openSync', function() { | ||
expect(function() { | ||
mktemp.createFileSync(''); | ||
}).to.throwError(function(e) { | ||
expect(e.code).to.be('ENOENT'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -51,10 +173,73 @@ | ||
it('should create directory of random name', function(done) { | ||
mktemp.createDir('dir-XXXXX', function(err, path) { | ||
expect(path).to.match(/^dir-[\da-zA-Z]{5}$/); | ||
expect(path).not.to.be('dir-XXXXX'); | ||
done(); | ||
describe('success', function() { | ||
before(function() { | ||
sinon.stub(fs, 'mkdir').callsArgWith(2, null); | ||
}); | ||
after(function() { | ||
fs.mkdir.restore(); | ||
}); | ||
it('should create directory of random name', function(done) { | ||
mktemp.createDir('dir-XXXXX', function(err, path) { | ||
expect(path).to.match(/^dir-[\da-zA-Z]{5}$/); | ||
expect(path).not.to.be('dir-XXXXX'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('has error, err.code is EEXIST', function() { | ||
before(function() { | ||
var transientObject = { | ||
getCount: 0 | ||
}; | ||
Object.defineProperty(transientObject, 'code', { | ||
get: function() { | ||
// return 'EEXIST' if first call | ||
return (this.getCount++ > 0) ? null : 'EEXIST'; | ||
} | ||
}); | ||
sinon.stub(fs, 'mkdir').callsArgWith(2, transientObject); | ||
}); | ||
after(function() { | ||
fs.mkdir.restore(); | ||
}); | ||
it('should once more call for callback', function(done) { | ||
mktemp.createDir('', function(err, path) { | ||
expect(err.getCount).to.be(2); | ||
expect(path).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('has error, err.code is not EEXIST', function() { | ||
before(function() { | ||
sinon.stub(fs, 'mkdir').callsArgWith(2, {code: 'ENOENT'}); | ||
}); | ||
after(function() { | ||
fs.mkdir.restore(); | ||
}); | ||
it('should set error to err and set null to path', function(done) { | ||
mktemp.createDir('', function(err, path) { | ||
expect(err).to.eql({code: 'ENOENT'}); | ||
expect(path).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -64,9 +249,73 @@ | ||
it('should create directory of random name', function() { | ||
expect(mktemp.createDirSync('XXX')).to.match(/^[\da-zA-Z]{3}$/); | ||
expect(mktemp.createDirSync('XXX')).to.not.be('XXX'); | ||
describe('success', function() { | ||
before(function() { | ||
sinon.stub(fs, 'mkdirSync'); | ||
}); | ||
after(function() { | ||
fs.mkdirSync.restore(); | ||
}); | ||
it('should create directory of random name', function() { | ||
expect(mktemp.createDirSync('XXX')).to.match(/^[\da-zA-Z]{3}$/); | ||
expect(mktemp.createDirSync('XXX')).to.not.be('XXX'); | ||
}); | ||
}); | ||
describe('throws error, err.code is EEXIST', function() { | ||
before(function() { | ||
var transientObject = { | ||
getCount: 0 | ||
}; | ||
Object.defineProperty(transientObject, 'code', { | ||
get: function() { | ||
// return 'EEXIST' if first call | ||
return (this.getCount++ > 0) ? null : 'EEXIST'; | ||
} | ||
}); | ||
sinon.stub(fs, 'mkdirSync').throws(transientObject); | ||
}); | ||
after(function() { | ||
fs.mkdirSync.restore(); | ||
}); | ||
it('should once more call for fs.mkdirSync', function() { | ||
expect(function() { | ||
mktemp.createDirSync(''); | ||
}).to.throwError(function(e) { | ||
expect(e.getCount).to.be(2); | ||
expect(e.code).to.be(null); | ||
}); | ||
}); | ||
}); | ||
describe('throws error, err.code is not EEXIST', function() { | ||
before(function() { | ||
sinon.stub(fs, 'mkdirSync').throws({code: 'ENOENT'}); | ||
}); | ||
after(function() { | ||
fs.mkdirSync.restore(); | ||
}); | ||
it('should throws error of fs.mkdirSync', function() { | ||
expect(function() { | ||
mktemp.createFileSync(''); | ||
}).to.throwError(function(e) { | ||
expect(e.code).to.be('ENOENT'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17012
423