diff --git a/lib/customUtils.js b/lib/customUtils.js index 53109a2..1a20c0a 100644 --- a/lib/customUtils.js +++ b/lib/customUtils.js @@ -18,16 +18,6 @@ function uid (len) { } -function ensureFileDoesntExist (file, callback) { - fs.exists(file, function (exists) { - if (!exists) { return callback(null); } - - fs.unlink(file, function (err) { return callback(err); }); - }); -} - - // Interface module.exports.uid = uid; -module.exports.ensureFileDoesntExist = ensureFileDoesntExist; diff --git a/test/customUtil.test.js b/test/customUtil.test.js index 0bca55d..cb69def 100644 --- a/test/customUtil.test.js +++ b/test/customUtil.test.js @@ -7,29 +7,20 @@ var should = require('chai').should() describe('customUtils', function () { - describe('ensureFileDoesntExist', function () { - - it('Doesnt do anything if file already doesnt exist', function (done) { - customUtils.ensureFileDoesntExist('workspace/nonexisting', function (err) { - assert.isNull(err); - fs.existsSync('workspace/nonexisting').should.equal(false); - done(); - }); - }); + describe('uid', function () { - it('Deletes file if it exists', function (done) { - fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); - fs.existsSync('workspace/existing').should.equal(true); - - customUtils.ensureFileDoesntExist('workspace/existing', function (err) { - assert.isNull(err); - fs.existsSync('workspace/existing').should.equal(false); - done(); - }); + it('Generates a string of the expected length', function () { + customUtils.uid(3).length.should.equal(3); + customUtils.uid(16).length.should.equal(16); + customUtils.uid(42).length.should.equal(42); + customUtils.uid(1000).length.should.equal(1000); }); - - }); + // Very small probability of conflict + it('Generated uids should not be the same', function () { + customUtils.uid(56).should.not.equal(customUtils.uid(56)); + }); + }); }); diff --git a/test/persistence.test.js b/test/persistence.test.js index 0f1b73e..b98bab8 100644 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -491,9 +491,9 @@ describe('Persistence', function () { var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; async.waterfall([ - async.apply(customUtils.ensureFileDoesntExist, dbFile) - , async.apply(customUtils.ensureFileDoesntExist, dbFile + '~') - , async.apply(customUtils.ensureFileDoesntExist, dbFile + '~~') + async.apply(Persistence.ensureFileDoesntExist, dbFile) + , async.apply(Persistence.ensureFileDoesntExist, dbFile + '~') + , async.apply(Persistence.ensureFileDoesntExist, dbFile + '~~') , function (cb) { theDb = new Datastore({ filename: dbFile }); theDb.loadDatabase(cb); @@ -617,4 +617,29 @@ describe('Persistence', function () { }); // ==== End of 'Prevent dataloss when persisting data' ==== + + describe('ensureFileDoesntExist', function () { + + it('Doesnt do anything if file already doesnt exist', function (done) { + Persistence.ensureFileDoesntExist('workspace/nonexisting', function (err) { + assert.isNull(err); + fs.existsSync('workspace/nonexisting').should.equal(false); + done(); + }); + }); + + it('Deletes file if it exists', function (done) { + fs.writeFileSync('workspace/existing', 'hello world', 'utf8'); + fs.existsSync('workspace/existing').should.equal(true); + + Persistence.ensureFileDoesntExist('workspace/existing', function (err) { + assert.isNull(err); + fs.existsSync('workspace/existing').should.equal(false); + done(); + }); + }); + + }); // ==== End of 'ensureFileDoesntExist' ==== + + });