Browse Source

correct callback

Kieran Gibb 2 years ago
parent
commit
9300d2debc
No account linked to committer's email address
2 changed files with 44 additions and 24 deletions
  1. 14
    14
      test/index.test.js
  2. 30
    10
      test/util.js

+ 14
- 14
test/index.test.js View File

@@ -15,11 +15,11 @@ describe('load', (context) => {
15 15
   })
16 16
 
17 17
   context.afterEach((c) => {
18
-    cleanup(storage.root)
18
+    cleanup(storage)
19 19
   })
20 20
 
21 21
   context('default', (assert, next) => {
22
-    var config = Config(storage.path)
22
+    var config = Config(storage)
23 23
     assert.ok(config.identities.list() instanceof Array, 'identities list defaults to empty Array')
24 24
     assert.ok(config.groups.list() instanceof Array, 'groups list defaults to empty Array')
25 25
     next()
@@ -34,11 +34,11 @@ describe('add', (context) => {
34 34
   })
35 35
 
36 36
   context.afterEach((c) => {
37
-    cleanup(storage.root)
37
+    cleanup(storage)
38 38
   })
39 39
 
40 40
   context('byKey', (assert, next) => {
41
-    var config = Config(storage.path)
41
+    var config = Config(storage)
42 42
     var group = crypto.unpack(crypto.accessKey())
43 43
     var identity = crypto.keyPair()
44 44
 
@@ -47,7 +47,7 @@ describe('add', (context) => {
47 47
 
48 48
     config.save()
49 49
 
50
-    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage.path, 'config.yml')))
50
+    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
51 51
     var rawGroup = reload.groups.byKey[group.publicKey.toString('hex')]
52 52
     var rawIdentity = reload.identities.byKey[identity.publicKey.toString('hex')]
53 53
 
@@ -57,7 +57,7 @@ describe('add', (context) => {
57 57
   })
58 58
 
59 59
   context('byName', (assert, next) => {
60
-    var config = Config(storage.path)
60
+    var config = Config(storage)
61 61
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
62 62
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
63 63
 
@@ -66,7 +66,7 @@ describe('add', (context) => {
66 66
 
67 67
     config.save()
68 68
 
69
-    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage.path, 'config.yml')))
69
+    var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
70 70
     var rawGroup = reload.groups.byKey[group.publicKey.toString('hex')]
71 71
     var rawIdentity = reload.identities.byKey[identity.publicKey.toString('hex')]
72 72
 
@@ -75,7 +75,7 @@ describe('add', (context) => {
75 75
     assert.ok(rawIdentity.name, 'saves identity with a name')
76 76
     assert.same(config.identities.get(identity.name), rawIdentity, 'saves identities to storage')
77 77
 
78
-    var reload = Config(storage.path)
78
+    var reload = Config(storage)
79 79
 
80 80
     assert.same(reload._groups, config._groups, 'load groups from storage')
81 81
     assert.same(reload._identities, config._identities, 'load identities from storage')
@@ -91,11 +91,11 @@ describe('get', (context) => {
91 91
   })
92 92
 
93 93
   context.afterEach((c) => {
94
-    cleanup(storage.root)
94
+    cleanup(storage)
95 95
   })
96 96
 
97 97
   context('works with buffers or strings', (assert, next) => {
98
-    var config = Config(storage.path)
98
+    var config = Config(storage)
99 99
     var group = crypto.unpack(crypto.accessKey())
100 100
     var identity = crypto.keyPair()
101 101
 
@@ -121,11 +121,11 @@ describe('remove', (context) => {
121 121
   })
122 122
 
123 123
   context.afterEach((c) => {
124
-    cleanup(storage.root)
124
+    cleanup(storage)
125 125
   })
126 126
 
127 127
   context('removes relevant entry using buffers', (assert, next) => {
128
-    var config = Config(storage.path)
128
+    var config = Config(storage)
129 129
     var group = crypto.unpack(crypto.accessKey())
130 130
     var identity = crypto.keyPair()
131 131
 
@@ -144,7 +144,7 @@ describe('remove', (context) => {
144 144
   })
145 145
 
146 146
   context('removes relevant entry using buffers', (assert, next) => {
147
-    var config = Config(storage.path)
147
+    var config = Config(storage)
148 148
     var group = crypto.unpack(crypto.accessKey())
149 149
     var identity = crypto.keyPair()
150 150
 
@@ -163,7 +163,7 @@ describe('remove', (context) => {
163 163
   })
164 164
 
165 165
   context('removes relevant entry using names', (assert, next) => {
166
-    var config = Config(storage.path)
166
+    var config = Config(storage)
167 167
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
168 168
     var identity = Object.assign({ name: 'Alice' }, crypto.keyPair())
169 169
 

+ 30
- 10
test/util.js View File

@@ -1,21 +1,41 @@
1 1
 const tmpdir = require('tmp').dirSync
2 2
 const mkdirp = require('mkdirp')
3 3
 const rimraf = require('rimraf')
4
-const debug = require('debug')('cobox-config')
5 4
 
6
-function cleanup (dir) {
7
-  rimraf(dir, (err) => {
8
-    debug(`[CLEANUP] ${ err ? 'failed' : 'success'}`)
9
-    if (err) throw (err)
10
-  })
5
+function cleanup (dirs, cb) {
6
+  if (!cb) cb = noop
7
+  if (!Array.isArray(dirs)) dirs = [dirs]
8
+  var pending = 1
9
+
10
+  function next (n) {
11
+    var dir = dirs[n]
12
+    if (!dir) return done()
13
+    ++pending
14
+    process.nextTick(next, n + 1)
15
+
16
+    rimraf(dir, (err) => {
17
+      if (err) return done(err)
18
+      done()
19
+    })
20
+  }
21
+
22
+  function done (err) {
23
+    if (err) {
24
+      pending = Infinity
25
+      return cb(err)
26
+    }
27
+    if (!--pending) return cb()
28
+  }
29
+
30
+  next(0)
11 31
 }
12 32
 
13 33
 function tmp () {
14
-  var root = `./tmp/`
15
-  var path = `.${tmpdir().name}`
34
+  var path = tmpdir().name
16 35
   mkdirp.sync(path)
17
-  debug(`[TEMP] ${path}`)
18
-  return { path, root }
36
+  return path
19 37
 }
20 38
 
39
+function noop () {}
40
+
21 41
 module.exports = { cleanup, tmp }