From 061ed7d6eb766ec63eb4057e26be4f2c43d60446 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 03:23:17 -0500 Subject: [PATCH 1/6] Add some basic tests, via mocha --- test/test-fsdocs.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test/test-fsdocs.js diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js new file mode 100644 index 0000000..68e81bf --- /dev/null +++ b/test/test-fsdocs.js @@ -0,0 +1,62 @@ +var assert = require('assert'); +var child_process = require('child_process'); +var fsdocs = require('../fsdocs'); + +var datadir = __dirname + '/data'; + +function clear(done) { + var p = child_process.spawn('rm', ['-rf', datadir]); + p.on('exit', function(code) { + var error; + if (code == 0) { + error = null; + } else { + error = new Error('Could not remove data directory: rm exited with code ' + code); + } + done(error); + }); +} + +describe('Basics', function() { + before(clear); + + it('Should report nonexisting documents as missing', function() { + var docs = new fsdocs.FSDocs(datadir); + + docs.get('nonexistent', function(err, document) { + assert(err); + assert.equal('ENOENT', err.code); + }); + }); + + it('Should be able to put and get documents asynchronously', function() { + var docs = new fsdocs.FSDocs(datadir); + + docs.put('test-doc', {hello: 'world'}, function(err, ok) { + assert(!err); + assert(ok); + + docs.get('test-doc', function(err, document) { + assert(!err); + assert(document); + assert.equal('world', document.hello); + }); + }); + }); + + it('Should be able to put and get documents synchronously', function() { + var docs = new fsdocs.FSDocs(datadir); + var ok, document; + + assert.throws(function() { + docs.getSync('test-doc'); + }, Error.ENOENT); + + ok = docs.putSync('test-doc', {hello: 'world'}); + assert(ok); + + document = docs.getSync('test-doc'); + assert(document); + assert.equal('world', document.hello); + }); +}); From 7ece20fec0592edf94aeddb9c61609b507dc83a4 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 03:32:05 -0500 Subject: [PATCH 2/6] Add a conflict detection test --- test/test-fsdocs.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js index 68e81bf..1bfce44 100644 --- a/test/test-fsdocs.js +++ b/test/test-fsdocs.js @@ -17,7 +17,7 @@ function clear(done) { }); } -describe('Basics', function() { +describe('FSDocs', function() { before(clear); it('Should report nonexisting documents as missing', function() { @@ -59,4 +59,17 @@ describe('Basics', function() { assert(document); assert.equal('world', document.hello); }); + + it('Should refuse to write the same version twice', function() { + var docs = new fsdocs.FSDocs(datadir); + + docs.put('test-doc', {hello: 'world'}, function(err, ok) { + assert(!err); + assert(ok); + + docs.put('test-doc', {hello: 'world'}, function(err, ok) { + assert(err); + }); + }); + }); }); From eddbb473f2b324ef6cda702a085cd0be24a335fa Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 03:59:19 -0500 Subject: [PATCH 3/6] Delete and create base datadir in preparation of per-test datadirs --- test/test-fsdocs.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js index 1bfce44..89779d5 100644 --- a/test/test-fsdocs.js +++ b/test/test-fsdocs.js @@ -1,3 +1,4 @@ +var fs = require('fs'); var assert = require('assert'); var child_process = require('child_process'); var fsdocs = require('../fsdocs'); @@ -5,18 +6,31 @@ var fsdocs = require('../fsdocs'); var datadir = __dirname + '/data'; function clear(done) { - var p = child_process.spawn('rm', ['-rf', datadir]); - p.on('exit', function(code) { - var error; - if (code == 0) { - error = null; + fs.exists(datadir, function(yes) { + if (yes) { + var p = child_process.spawn('rm', ['-rf', datadir]); + p.on('exit', function(code) { + var error; + if (code == 0) { + create(done); + } else { + error = new Error('Could not remove data directory: rm exited with code ' + code); + done(error); + } + }); } else { - error = new Error('Could not remove data directory: rm exited with code ' + code); + create(done); } - done(error); }); } +function create(done) { + var mode = 0777 - process.umask(); + fs.mkdir(datadir, mode, done); +} + +// Important: each test should use its own data directory + describe('FSDocs', function() { before(clear); From 3bd380b8df4823200b85c0ca798db1266dbf78c0 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 04:00:01 -0500 Subject: [PATCH 4/6] Use per-test datadirs to avoid weird issues that I do not understand, are tests being run concurrently? --- test/test-fsdocs.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js index 89779d5..e45ffe2 100644 --- a/test/test-fsdocs.js +++ b/test/test-fsdocs.js @@ -35,7 +35,7 @@ describe('FSDocs', function() { before(clear); it('Should report nonexisting documents as missing', function() { - var docs = new fsdocs.FSDocs(datadir); + var docs = new fsdocs.FSDocs(datadir + '/missing'); docs.get('nonexistent', function(err, document) { assert(err); @@ -44,7 +44,7 @@ describe('FSDocs', function() { }); it('Should be able to put and get documents asynchronously', function() { - var docs = new fsdocs.FSDocs(datadir); + var docs = new fsdocs.FSDocs(datadir + '/get-put-async'); docs.put('test-doc', {hello: 'world'}, function(err, ok) { assert(!err); @@ -59,7 +59,7 @@ describe('FSDocs', function() { }); it('Should be able to put and get documents synchronously', function() { - var docs = new fsdocs.FSDocs(datadir); + var docs = new fsdocs.FSDocs(datadir + '/get-put-sync'); var ok, document; assert.throws(function() { @@ -75,7 +75,7 @@ describe('FSDocs', function() { }); it('Should refuse to write the same version twice', function() { - var docs = new fsdocs.FSDocs(datadir); + var docs = new fsdocs.FSDocs(datadir + '/conflict-detect'); docs.put('test-doc', {hello: 'world'}, function(err, ok) { assert(!err); From ac13a947070581dd14a14291b47322c673a38030 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 04:00:24 -0500 Subject: [PATCH 5/6] With errno changed to code behavior of get and getSync changes --- test/test-fsdocs.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js index e45ffe2..28a12c6 100644 --- a/test/test-fsdocs.js +++ b/test/test-fsdocs.js @@ -38,8 +38,8 @@ describe('FSDocs', function() { var docs = new fsdocs.FSDocs(datadir + '/missing'); docs.get('nonexistent', function(err, document) { - assert(err); - assert.equal('ENOENT', err.code); + assert(err === null); + assert(document === null); }); }); @@ -62,9 +62,8 @@ describe('FSDocs', function() { var docs = new fsdocs.FSDocs(datadir + '/get-put-sync'); var ok, document; - assert.throws(function() { - docs.getSync('test-doc'); - }, Error.ENOENT); + document = docs.getSync('test-doc'); + assert(document === null); ok = docs.putSync('test-doc', {hello: 'world'}); assert(ok); From e96e818337a92ca88712f633722183df3e612e1b Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 17 Dec 2012 04:01:17 -0500 Subject: [PATCH 6/6] Lowercase should --- test/test-fsdocs.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test-fsdocs.js b/test/test-fsdocs.js index 28a12c6..178ad81 100644 --- a/test/test-fsdocs.js +++ b/test/test-fsdocs.js @@ -34,7 +34,7 @@ function create(done) { describe('FSDocs', function() { before(clear); - it('Should report nonexisting documents as missing', function() { + it('should report nonexisting documents as missing', function() { var docs = new fsdocs.FSDocs(datadir + '/missing'); docs.get('nonexistent', function(err, document) { @@ -43,7 +43,7 @@ describe('FSDocs', function() { }); }); - it('Should be able to put and get documents asynchronously', function() { + it('should be able to put and get documents asynchronously', function() { var docs = new fsdocs.FSDocs(datadir + '/get-put-async'); docs.put('test-doc', {hello: 'world'}, function(err, ok) { @@ -58,7 +58,7 @@ describe('FSDocs', function() { }); }); - it('Should be able to put and get documents synchronously', function() { + it('should be able to put and get documents synchronously', function() { var docs = new fsdocs.FSDocs(datadir + '/get-put-sync'); var ok, document; @@ -73,7 +73,7 @@ describe('FSDocs', function() { assert.equal('world', document.hello); }); - it('Should refuse to write the same version twice', function() { + it('should refuse to write the same version twice', function() { var docs = new fsdocs.FSDocs(datadir + '/conflict-detect'); docs.put('test-doc', {hello: 'world'}, function(err, ok) {