[Pkg-javascript-commits] [node-leveldown] 286/492: added destroy tests

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit 0620c6b1b0c2663aa9feb74a9a748407b70d0d1d
Author: Rod Vagg <rod at vagg.org>
Date:   Wed Mar 27 00:53:56 2013 +1100

    added destroy tests
---
 package.json                           |  4 +-
 test/cleanup-hanging-iterators-test.js | 31 +-------------
 test/common.js                         | 42 +++++++++++++++++--
 test/destroy-test.js                   | 77 ++++++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 34 deletions(-)

diff --git a/package.json b/package.json
index d1c9811..39e9157 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,8 @@
   , "devDependencies" : {
         "tap"             : "*"
       , "rimraf"          : "*"
+      , "mkfiletree"      : "*"
+      , "readfiletree"    : "*"
     }
   , "repository"      : {
         "type"            : "git"
@@ -33,4 +35,4 @@
     }
   , "license"         : "MIT"
   , "gypfile"         : true
-}
\ No newline at end of file
+}
diff --git a/test/cleanup-hanging-iterators-test.js b/test/cleanup-hanging-iterators-test.js
index 7ae12b4..6ad9dda 100644
--- a/test/cleanup-hanging-iterators-test.js
+++ b/test/cleanup-hanging-iterators-test.js
@@ -1,34 +1,7 @@
 const test       = require('tap').test
     , testCommon = require('./common')
     , leveldown  = require('../')
-
-    , makeTest   = function (name, testFn) {
-        test(name, function (t) {
-          testCommon.cleanup(function () {
-            var db   = leveldown(testCommon.location())
-              , done = function () {
-                  db.close(function (err) {
-                    t.notOk(err, 'no error from close()')
-                    testCommon.cleanup(t.end.bind(t))
-                  })
-                }
-            db.open(function (err) {
-              t.notOk(err, 'no error from open()')
-              db.batch(
-                  [
-                      { type: 'put', key: 'one', value: '1' }
-                    , { type: 'put', key: 'two', value: '2' }
-                    , { type: 'put', key: 'three', value: '3' }
-                  ]
-                , function (err) {
-                    t.notOk(err, 'no error from batch()')
-                    testFn(db, t, done)
-                  }
-              )
-            })
-          })
-        })
-      }
+    , makeTest   = testCommon.makeExistingDbTest
 
 makeTest('test ended iterator', function (db, t, done) {
   // standard iterator with an end() properly called, easy
@@ -74,4 +47,4 @@ makeTest('test ending iterators', function (db, t, done) {
         it2.end(function () {})
         done()
       })
-})
\ No newline at end of file
+})
diff --git a/test/common.js b/test/common.js
index 331ad83..ead3ba0 100644
--- a/test/common.js
+++ b/test/common.js
@@ -1,6 +1,8 @@
-const path   = require('path')
-    , fs     = require('fs')
-    , rimraf = require('rimraf')
+const path      = require('path')
+    , fs        = require('fs')
+    , rimraf    = require('rimraf')
+    , test      = require('tap').test
+    , leveldown = require('../')
 
 var dbidx = 0
 
@@ -63,6 +65,37 @@ var dbidx = 0
       next()
     }
 
+  , makeExistingDbTest = function (name, testFn) {
+      test(name, function (t) {
+        cleanup(function () {
+          var loc  = location()
+            , db   = leveldown(loc)
+            , done = function (close) {
+                if (close === false)
+                  return cleanup(t.end.bind(t))
+                db.close(function (err) {
+                  t.notOk(err, 'no error from close()')
+                  cleanup(t.end.bind(t))
+                })
+              }
+          db.open(function (err) {
+           t.notOk(err, 'no error from open()')
+            db.batch(
+                [
+                    { type: 'put', key: 'one', value: '1' }
+                  , { type: 'put', key: 'two', value: '2' }
+                  , { type: 'put', key: 'three', value: '3' }
+                ]
+              , function (err) {
+                  t.notOk(err, 'no error from batch()')
+                  testFn(db, t, done, loc)
+                }
+            )
+          })
+        })
+      })
+    }
+
 module.exports = {
     location       : location
   , cleanup        : cleanup
@@ -70,4 +103,5 @@ module.exports = {
   , setUp          : setUp
   , tearDown       : tearDown
   , collectEntries : collectEntries
-}
\ No newline at end of file
+  , makeExistingDbTest : makeExistingDbTest
+}
diff --git a/test/destroy-test.js b/test/destroy-test.js
new file mode 100644
index 0000000..79a0baf
--- /dev/null
+++ b/test/destroy-test.js
@@ -0,0 +1,77 @@
+const test         = require('tap').test
+    , fs           = require('fs')
+    , path         = require('path')
+    , mkfiletree   = require('mkfiletree')
+    , readfiletree = require('readfiletree')
+    , testCommon   = require('./common')
+    , leveldown    = require('../')
+    , makeTest     = testCommon.makeExistingDbTest
+
+test('test argument-less destroy() throws', function (t) {
+  t.throws(
+      leveldown.destroy
+    , { name: 'Error', message: 'destroy() requires `location` and `callback` arguments' }
+    , 'no-arg destroy() throws'
+  )
+  t.end()
+})
+
+test('test callback-less, 1-arg, destroy() throws', function (t) {
+  t.throws(
+      leveldown.destroy.bind(null, 'foo')
+    , { name: 'Error', message: 'destroy() requires `location` and `callback` arguments' }
+    , 'callback-less, 1-arg destroy() throws'
+  )
+  t.end()
+})
+
+test('test destroy non-existant directory', function (t) {
+  leveldown.destroy('/1/2/3/4', function () {
+    t.equal(arguments.length, 0, 'no arguments returned on callback')
+    t.end()
+  })
+})
+
+test('test destroy non leveldb directory', function (t) {
+  var tree = {
+      'foo': 'FOO'
+    , 'bar': { 'one': 'ONE', 'two': 'TWO', 'three': 'THREE' }
+  }
+  mkfiletree.makeTemp('destroy-test', tree, function (err, dir) {
+    t.notOk(err, 'no error')
+    leveldown.destroy(dir, function () {
+      t.ok(arguments, 'no arguments')
+      readfiletree(dir, function (err, actual) {
+        t.notOk(err, 'no error')
+        t.deepEqual(actual, tree, 'directory remains untouched')
+        mkfiletree.cleanUp(function (err) {
+          t.notOk(err, 'no error')
+          t.end()
+        })
+      })
+    })
+  })
+})
+
+makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t, done, location) {
+  db.close(function (err) {
+    t.notOk(err, 'no error')
+    leveldown.destroy(location, function () {
+      t.notOk(fs.existsSync(), 'directory completely removed')
+      done(false)
+    })
+  })
+})
+
+makeTest('test destroy() cleans and removes only leveldb parts of a dir', function (db, t, done, location) {
+  fs.writeFileSync(path.join(location, 'foo'), 'FOO')
+  db.close(function (err) {
+    t.notOk(err, 'no error')
+    leveldown.destroy(location, function () {
+      readfiletree(location, function (err, tree) {
+        t.deepEqual(tree, { 'foo': 'FOO' }, 'non-leveldb files left intact')
+        done(false)
+      })
+    })
+  })
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list