Browse Source

update tests

Kieran Gibb 3 years ago
parent
commit
92474d88ea
No account linked to committer's email address
4 changed files with 107 additions and 86 deletions
  1. 5
    8
      example.js
  2. 6
    3
      index.js
  3. 33
    7
      package-lock.json
  4. 63
    68
      test/index.test.js

+ 5
- 8
example.js View File

@@ -7,13 +7,10 @@ const drive = KappaDrive(storage)
7 7
 
8 8
 const mount = Mount()
9 9
 
10
-drive.ready(() => {
11
-  mount(drive, mountdir, (err, done) => {
12
-    if (err) return console.error(err)
10
+mount(drive, mountdir, (err, unmount) => {
11
+  if (err) return console.error(err)
13 12
 
14
-    setTimeout(() => {
15
-      console.log("DONE", done)
16
-      if (done) done()
17
-    }, 3000)
18
-  })
13
+  setTimeout(() => {
14
+    unmount()
15
+  }, 3000)
19 16
 })

+ 6
- 3
index.js View File

@@ -14,16 +14,19 @@ module.exports = function () {
14 14
 
15 15
       return cb(null, done)
16 16
 
17
-      function done () {
17
+      function done (callback) {
18
+        if (!callback) callback = noop
18 19
         destroy((err) => {
19 20
           if (err) throw err
20 21
           process.removeListener('SIGINT', done)
21 22
           rimraf(dir, (err) => {
22
-            if (err) return cb(err)
23
-            return cb()
23
+            if (err) return callback(err)
24
+            return callback()
24 25
           })
25 26
         })
26 27
       }
27 28
     })
28 29
   }
29 30
 }
31
+
32
+function noop () {}

+ 33
- 7
package-lock.json View File

@@ -628,7 +628,7 @@
628 628
         "duplexify": "^3.7.1",
629 629
         "filesystem-constants": "^1.0.0",
630 630
         "hypercore-byte-stream": "^1.0.2",
631
-        "hyperdrive-schemas": "^0.9.0",
631
+        "hyperdrive-schemas": "^0.12.0",
632 632
         "mountable-hypertrie": "^0.11.0",
633 633
         "mutexify": "^1.2.0",
634 634
         "nanoiterator": "^1.2.0",
@@ -641,6 +641,12 @@
641 641
         "unixify": "^1.0.0"
642 642
       },
643 643
       "dependencies": {
644
+        "hyperdrive-schemas": {
645
+          "version": "0.12.0",
646
+          "resolved": "https://registry.npmjs.org/hyperdrive-schemas/-/hyperdrive-schemas-0.12.0.tgz",
647
+          "integrity": "sha512-Uitbz7wEQwSMbgmSCdCQJVpVkmztQGpIjgTyBovRKjrlaBEKjBOGjvr0VVM++kTvkfnQ6wsaTHGZHlPYw8ljUg==",
648
+          "dev": true
649
+        },
644 650
         "mountable-hypertrie": {
645 651
           "version": "0.11.0",
646 652
           "resolved": "https://registry.npmjs.org/mountable-hypertrie/-/mountable-hypertrie-0.11.0.tgz",
@@ -667,12 +673,6 @@
667 673
         "fuse-native": "^1.1.0"
668 674
       }
669 675
     },
670
-    "hyperdrive-schemas": {
671
-      "version": "0.9.8",
672
-      "resolved": "https://registry.npmjs.org/hyperdrive-schemas/-/hyperdrive-schemas-0.9.8.tgz",
673
-      "integrity": "sha512-HJzonI8rtuLSMZL4fJhErwdopJNvu4ZLj0Pzn64n2e3Ifo5HwQBRUJYzwSXvx8+NNb/8VXUgh+2i6S6VtbVLuw==",
674
-      "dev": true
675
-    },
676 676
     "hypertrie": {
677 677
       "version": "3.7.1",
678 678
       "resolved": "https://registry.npmjs.org/hypertrie/-/hypertrie-3.7.1.tgz",
@@ -821,6 +821,32 @@
821 821
         "multifeed": "github:coboxcoop/multifeed#enable-custom-keypair-for-feeds",
822 822
         "random-access-memory": "^3.1.1",
823 823
         "through2": "^3.0.1"
824
+      },
825
+      "dependencies": {
826
+        "corestore": {
827
+          "version": "github:coboxcoop/corestore#2dc80a8733e14bdc016df89d0a1017d76aae6d3f",
828
+          "from": "github:coboxcoop/corestore",
829
+          "dev": true,
830
+          "requires": {
831
+            "dat-encoding": "^5.0.1",
832
+            "hypercore": "^7.2.2",
833
+            "hypercore-crypto": "^1.0.0",
834
+            "hypercore-protocol": "^6.11.0"
835
+          }
836
+        },
837
+        "multifeed": {
838
+          "version": "github:coboxcoop/multifeed#2056594f86273211fb2b43b6300ba92c879f8805",
839
+          "from": "github:coboxcoop/multifeed#enable-custom-keypair-for-feeds",
840
+          "dev": true,
841
+          "requires": {
842
+            "debug": "^4.1.0",
843
+            "hypercore-protocol": "^6.8.0",
844
+            "inherits": "^2.0.3",
845
+            "mutexify": "^1.2.0",
846
+            "random-access-file": "^2.0.1",
847
+            "through2": "^3.0.0"
848
+          }
849
+        }
824 850
       }
825 851
     },
826 852
     "kappa-view-kv": {

+ 63
- 68
test/index.test.js View File

@@ -1,7 +1,7 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const ram = require('random-access-memory')
3 3
 const fs = require('fs')
4
-const MountDrive = require('../')
4
+const Mount = require('../')
5 5
 const KappaDrive = require('kappa-drive')
6 6
 const path = require('path')
7 7
 
@@ -11,20 +11,21 @@ const { replicate, tmp, run } = require('./util')
11 11
 
12 12
 describe('mount', (context) => {
13 13
   context('read mounted directory', (assert, next) => {
14
-    var mountDrive = MountDrive()
14
+    var mount = Mount()
15 15
     var drive = KappaDrive(ram)
16 16
     var storage = tmp()
17
+
17 18
     drive.ready(() => {
18 19
       drive.writeFile('/hello.txt', 'world', (err) => {
19 20
         assert.error(err, 'no error')
20
-        mountDrive.mount(drive, storage).then((destroy) => {
21
+        mount(drive, storage, (err, unmount) => {
21 22
           fs.readdir(storage, (err, files) => {
22 23
             assert.error(err, 'no error')
23 24
             assert.same(files, ['hello.txt'], 'readdir returns the written file')
24 25
             fs.readFile(path.join(storage, 'hello.txt'), 'utf-8', (err, data) => {
25 26
               assert.error(err, 'no error')
26 27
               assert.same(data, 'world', 'the file has the correct content')
27
-              mountDrive.cleanup(destroy, storage).then(next)
28
+              unmount(next)
28 29
             })
29 30
           })
30 31
         })
@@ -33,17 +34,18 @@ describe('mount', (context) => {
33 34
   })
34 35
 
35 36
   context('write to mounted directory', (assert, next) => {
36
-    var mountDrive = MountDrive()
37
+    var mount = Mount()
37 38
     var drive = KappaDrive(ram)
38 39
     var storage = tmp()
40
+
39 41
     drive.ready(() => {
40
-      mountDrive.mount(drive, storage).then((destroy) => {
42
+      mount(drive, storage, (err, unmount) => {
41 43
         fs.writeFile(path.join(storage, 'hello.txt'), 'world', (err) => {
42 44
           assert.error(err, 'no error')
43 45
           drive.readdir('/', (err, files) => {
44 46
             assert.error(err, 'no error')
45 47
             assert.same(files, ['hello.txt'], 'drive.readdir returns the written file')
46
-            mountDrive.cleanup(destroy, storage).then(next)
48
+            unmount(next)
47 49
           })
48 50
         })
49 51
       })
@@ -51,17 +53,17 @@ describe('mount', (context) => {
51 53
   })
52 54
 
53 55
   context('mkdir in mounted directory', (assert, next) => {
54
-    var mountDrive = MountDrive()
56
+    var mount = Mount()
55 57
     var drive = KappaDrive(ram)
56 58
     var storage = tmp()
57 59
     drive.ready(() => {
58
-      mountDrive.mount(drive, storage).then((destroy) => {
60
+      mount(drive, storage, (err, unmount) => {
59 61
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
60 62
           assert.error(err, 'no error')
61 63
           drive.readdir('/', (err, files) => {
62 64
             assert.error(err, 'no error')
63 65
             assert.same(files, ['antelopes'], 'drive.readdir returns directory name')
64
-            mountDrive.cleanup(destroy, storage).then(next)
66
+            unmount(next)
65 67
           })
66 68
         })
67 69
       })
@@ -69,11 +71,11 @@ describe('mount', (context) => {
69 71
   })
70 72
 
71 73
   context('readdir in mounted newly created directory', (assert, next) => {
72
-    var mountDrive = MountDrive()
74
+    var mount = Mount()
73 75
     var drive = KappaDrive(ram)
74 76
     var storage = tmp()
75 77
     drive.ready(() => {
76
-      mountDrive.mount(drive, storage).then((destroy) => {
78
+      mount(drive, storage, (err, unmount) => {
77 79
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
78 80
           assert.error(err, 'no error')
79 81
           fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
@@ -84,7 +86,7 @@ describe('mount', (context) => {
84 86
               fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
85 87
                 assert.error(err, 'no error')
86 88
                 assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
87
-                mountDrive.cleanup(destroy, storage).then(next)
89
+                unmount(next)
88 90
               })
89 91
             })
90 92
           })
@@ -94,11 +96,11 @@ describe('mount', (context) => {
94 96
   })
95 97
 
96 98
   context('unlink in mounted directory', (assert, next) => {
97
-    var mountDrive = MountDrive()
99
+    var mount = Mount()
98 100
     var drive = KappaDrive(tmp())
99 101
     var storage = tmp()
100 102
     drive.ready(() => {
101
-      mountDrive.mount(drive, storage).then((destroy) => {
103
+      mount(drive, storage, (err, unmount) => {
102 104
         fs.writeFile(path.join(storage, 'tree.txt'), 'is this a tree', (err) => {
103 105
           assert.error(err, 'no error')
104 106
           fs.readdir(storage, (err, files) => {
@@ -109,7 +111,7 @@ describe('mount', (context) => {
109 111
               fs.readdir(storage, (err, files) => {
110 112
                 assert.error(err, 'no error')
111 113
                 assert.same(files, [], 'drive.readdir returns nothing')
112
-                mountDrive.cleanup(destroy, storage).then(next)
114
+                unmount(next)
113 115
               })
114 116
             })
115 117
           })
@@ -125,16 +127,15 @@ describe('mount', (context) => {
125 127
     }
126 128
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
127 129
     var blueDrive = KappaDrive(feedsOnDisk.blue)
130
+    var mount = Mount()
128 131
 
129 132
     var storage = {}
130
-    var destroy = {}
131
-    var mountDrive = {}
133
+    var unmount = {}
132 134
 
133 135
     function setup (drive, colour, cb) {
134 136
       storage[colour] = tmp()
135
-      mountDrive[colour] = MountDrive()
136
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
137
-        destroy[colour] = destroyFn
137
+      mount(drive, storage[colour], (err, unmountFn) => {
138
+        unmount[colour] = unmountFn
138 139
         drive.ready(cb)
139 140
       })
140 141
     }
@@ -153,8 +154,8 @@ describe('mount', (context) => {
153 154
       fs.readdir(storage.blue, (err, files) => {
154 155
         assert.error(err, 'no error on readdir')
155 156
         assert.same(files, ['tree.txt'], 'fs.readdir returns file')
156
-        mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
157
-          mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
157
+        unmount.orange(() => {
158
+          unmount.blue(next)
158 159
         })
159 160
       })
160 161
     }
@@ -167,16 +168,15 @@ describe('mount', (context) => {
167 168
     }
168 169
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
169 170
     var blueDrive = KappaDrive(feedsOnDisk.blue)
171
+    var mount = Mount()
170 172
 
171 173
     var storage = {}
172
-    var destroy = {}
173
-    var mountDrive = {}
174
+    var unmount = {}
174 175
 
175 176
     function setup (drive, colour, cb) {
176 177
       storage[colour] = tmp()
177
-      mountDrive[colour] = MountDrive()
178
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
179
-        destroy[colour] = destroyFn
178
+      mount(drive, storage[colour], (err, unmountFn) => {
179
+        unmount[colour] = unmountFn
180 180
         drive.ready(cb)
181 181
       })
182 182
     }
@@ -210,8 +210,8 @@ describe('mount', (context) => {
210 210
               run(`cat ${storage.orange}/hello.txt`, (err, output) => {
211 211
                 assert.error(err, 'no error on cat out file')
212 212
                 assert.same(output, ['what noise does a dog make?\nWOOF\n'], 'woof has correctly replicated')
213
-                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
214
-                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
213
+                unmount.orange(() => {
214
+                  unmount.blue(next)
215 215
                 })
216 216
               })
217 217
             })
@@ -228,16 +228,15 @@ describe('mount', (context) => {
228 228
     }
229 229
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
230 230
     var blueDrive = KappaDrive(feedsOnDisk.blue)
231
+    var mount = Mount()
231 232
 
232 233
     var storage = {}
233
-    var destroy = {}
234
-    var mountDrive = {}
234
+    var unmount = {}
235 235
 
236 236
     function setup (drive, colour, cb) {
237 237
       storage[colour] = tmp()
238
-      mountDrive[colour] = MountDrive()
239
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
240
-        destroy[colour] = destroyFn
238
+      mount(drive, storage[colour], (err, unmountFn) => {
239
+        unmount[colour] = unmountFn
241 240
         drive.ready(cb)
242 241
       })
243 242
     }
@@ -271,8 +270,8 @@ describe('mount', (context) => {
271 270
                 assert.error(err, 'no error')
272 271
                 // TODO this line should also use .split:
273 272
                 assert.ok(output.indexOf('hello.txt\n') < 0, 'ls does not find the file')
274
-                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
275
-                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
273
+                unmount.orange(() => {
274
+                  unmount.blue(next)
276 275
                 })
277 276
               })
278 277
             })
@@ -289,16 +288,15 @@ describe('mount', (context) => {
289 288
     }
290 289
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
291 290
     var blueDrive = KappaDrive(feedsOnDisk.blue)
291
+    var mount = Mount()
292 292
 
293 293
     var storage = {}
294
-    var destroy = {}
295
-    var mountDrive = {}
294
+    var unmount = {}
296 295
 
297 296
     function setup (drive, colour, cb) {
298 297
       storage[colour] = tmp()
299
-      mountDrive[colour] = MountDrive()
300
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
301
-        destroy[colour] = destroyFn
298
+      mount(drive, storage[colour], (err, unmountFn) => {
299
+        unmount[colour] = unmountFn
302 300
         drive.ready(cb)
303 301
       })
304 302
     }
@@ -331,8 +329,8 @@ describe('mount', (context) => {
331 329
               run(`ls ${storage.orange}`, (err, output) => {
332 330
                 assert.error(err, 'no error')
333 331
                 assert.false(output[0].split('\n').indexOf('cabbage') > -1, 'ls no longer lists dir')
334
-                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
335
-                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
332
+                unmount.orange(() => {
333
+                  unmount.blue(next)
336 334
                 })
337 335
               })
338 336
             })
@@ -347,18 +345,18 @@ describe('mount', (context) => {
347 345
       orange: tmp(),
348 346
       blue: tmp()
349 347
     }
348
+
350 349
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
351 350
     var blueDrive = KappaDrive(feedsOnDisk.blue)
351
+    var mount = Mount()
352 352
 
353 353
     var storage = {}
354
-    var destroy = {}
355
-    var mountDrive = {}
354
+    var unmount = {}
356 355
 
357 356
     function setup (drive, colour, cb) {
358 357
       storage[colour] = tmp()
359
-      mountDrive[colour] = MountDrive()
360
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
361
-        destroy[colour] = destroyFn
358
+      mount(drive, storage[colour], (err, unmountFn) => {
359
+        unmount[colour] = unmountFn
362 360
         drive.ready(cb)
363 361
       })
364 362
     }
@@ -386,8 +384,8 @@ describe('mount', (context) => {
386 384
           fs.readFile(path.join(storage.blue, 'hello.txt'), 'utf-8', (err, data) => {
387 385
             assert.error(err, 'no error on read file')
388 386
             assert.same(data, 'what noise', 'returns expected contents')
389
-            mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
390
-              mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
387
+            unmount.orange(() => {
388
+              unmount.blue(next)
391 389
             })
392 390
           })
393 391
         })
@@ -402,19 +400,16 @@ describe('mount', (context) => {
402 400
     }
403 401
     var orangeDrive = KappaDrive(feedsOnDisk.orange)
404 402
     var blueDrive = KappaDrive(feedsOnDisk.blue)
403
+    var mount = Mount()
405 404
 
406 405
     var storage = {}
407
-    var destroy = {}
408
-    var mountDrive = {}
406
+    var unmount = {}
409 407
 
410 408
     function setup (drive, colour, cb) {
411 409
       storage[colour] = tmp()
412
-      mountDrive[colour] = MountDrive()
413
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
414
-        destroy[colour] = destroyFn
415
-        drive.ready(() => {
416
-          cb()
417
-        })
410
+      mount(drive, storage[colour], (err, unmountFn) => {
411
+        unmount[colour] = unmountFn
412
+        drive.ready(cb)
418 413
       })
419 414
     }
420 415
 
@@ -430,6 +425,7 @@ describe('mount', (context) => {
430 425
         })
431 426
       })
432 427
     })
428
+
433 429
     function check (err) {
434 430
       assert.error(err, 'no error on replicate')
435 431
       run(`ls ${storage.blue}`, (err, output) => {
@@ -446,8 +442,8 @@ describe('mount', (context) => {
446 442
               run(`cat ${storage.orange}/hello.txt`, (err, output) => {
447 443
                 assert.error(err, 'no error on cat out file')
448 444
                 assert.same(output, ['WOOF\n'], 'woof has correctly replicated')
449
-                mountDrive.orange.cleanup(destroy.orange, storage.orange).then(() => {
450
-                  mountDrive.blue.cleanup(destroy.blue, storage.blue).then(next)
445
+                unmount.orange(() => {
446
+                  unmount.blue(next)
451 447
                 })
452 448
               })
453 449
             })
@@ -465,16 +461,15 @@ describe('mount', (context) => {
465 461
 
466 462
     var yellowDrive = KappaDrive(feedsOnDisk.yellow)
467 463
     var purpleDrive = KappaDrive(feedsOnDisk.purple)
464
+    var mount = Mount()
468 465
 
469 466
     var storage = {}
470
-    var destroy = {}
471
-    var mountDrive = {}
467
+    var unmount = {}
472 468
 
473 469
     function setup (drive, colour, cb) {
474 470
       storage[colour] = tmp()
475
-      mountDrive[colour] = MountDrive()
476
-      mountDrive[colour].mount(drive, storage[colour]).then((destroyFn) => {
477
-        destroy[colour] = destroyFn
471
+      mount(drive, storage[colour], (err, unmountFn) => {
472
+        unmount[colour] = unmountFn
478 473
         drive.ready(cb)
479 474
       })
480 475
     }
@@ -514,8 +509,8 @@ describe('mount', (context) => {
514 509
                             fs.readFile(path.join(storage.yellow, 'hello.txt'), 'utf-8', (err, data) => {
515 510
                               assert.error(err, 'no error')
516 511
                               assert.same(data, 'mundo', 'data corrently overwritten and replicted')
517
-                              mountDrive.yellow.cleanup(destroy.yellow, storage.yellow).then(() => {
518
-                                mountDrive.purple.cleanup(destroy.purple, storage.purple).then(next)
512
+                              unmount.yellow(() => {
513
+                                unmount.purple(next)
519 514
                               })
520 515
                             })
521 516
                           })