Browse Source

unlink test (failing on replicate), replicate into empty drive test

ameba23 3 years ago
parent
commit
76251e94b3
1 changed files with 122 additions and 0 deletions
  1. 122
    0
      test/index.test.js

+ 122
- 0
test/index.test.js View File

@@ -86,6 +86,70 @@ describe('mount', (context) => {
86 86
     })
87 87
   })
88 88
 
89
+  context('unlink in mounted directory', (assert, next) => {
90
+    var drive = KappaDrive(tmp())
91
+    var storage = tmp()
92
+    drive.ready(() => {
93
+      mount(drive, storage).then((destroy) => {
94
+        fs.writeFile(path.join(storage, 'tree.txt'), 'is this a tree', (err) => {
95
+          assert.error(err, 'no error')
96
+          fs.readdir(storage, (err, files) => {
97
+            assert.error(err, 'no error')
98
+            assert.same(files, ['tree.txt'], 'readdir')
99
+            fs.unlink(path.join(storage, 'tree.txt'), (err) => {
100
+              assert.error(err, 'no error')
101
+              fs.readdir(storage, (err, files) => {
102
+                assert.error(err, 'no error')
103
+                assert.same(files, [], 'drive.readdir returns nothing')
104
+                cleanup(destroy, storage).then(next)
105
+              })
106
+            })
107
+          })
108
+        })
109
+      })
110
+    })
111
+  })
112
+
113
+  context('replicate into empty drive', (assert, next) => {
114
+    var feedsOnDisk = {
115
+      orange: tmp(),
116
+      blue: tmp()
117
+    }
118
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
119
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
120
+
121
+    var storage = {}
122
+    var destroy = {}
123
+
124
+    function setup (drive, colour, cb) {
125
+      storage[colour] = tmp()
126
+      mount(drive, storage[colour]).then((destroyFn) => {
127
+        destroy[colour] = destroyFn
128
+        drive.ready(cb)
129
+      })
130
+    }
131
+
132
+    setup(orangeDrive, 'orange', function () {
133
+      setup(blueDrive, 'blue', function () {
134
+        fs.writeFile(path.join(storage.orange, 'tree.txt'), 'is this a tree', (err) => {
135
+          assert.error(err, 'no error on write')
136
+          replicate(orangeDrive, blueDrive, check)
137
+        })
138
+      })
139
+    })
140
+
141
+    function check (err) {
142
+      assert.error(err, 'no error on replicate')
143
+      fs.readdir(storage.blue, (err, files) => {
144
+        assert.error(err, 'no error on readdir')
145
+        assert.same(files, ['tree.txt'], 'fs.readdir returns file')
146
+        cleanup(destroy.orange, storage.orange).then(() => {
147
+          cleanup(destroy.blue, storage.blue).then(next)
148
+        })
149
+      })
150
+    }
151
+  })
152
+
89 153
   context("replicate - append to another peer's file - using unix commands", (assert, next) => {
90 154
     var feedsOnDisk = {
91 155
       orange: tmp(),
@@ -145,6 +209,64 @@ describe('mount', (context) => {
145 209
     }
146 210
   })
147 211
 
212
+  context("replicate - unlink another peer's file - using unix commands", (assert, next) => {
213
+    var feedsOnDisk = {
214
+      orange: tmp(),
215
+      blue: tmp()
216
+    }
217
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
218
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
219
+
220
+    var storage = {}
221
+    var destroy = {}
222
+
223
+    function setup (drive, colour, cb) {
224
+      storage[colour] = tmp()
225
+      mount(drive, storage[colour]).then((destroyFn) => {
226
+        destroy[colour] = destroyFn
227
+        drive.ready(cb)
228
+      })
229
+    }
230
+
231
+    setup(orangeDrive, 'orange', function () {
232
+      setup(blueDrive, 'blue', function () {
233
+        run(`echo "what noise does a dog make?" > ${storage.orange}/hello.txt`, (err, output) => {
234
+          assert.error(err, 'no error on write')
235
+          // 'seed' the blue drive with a file (TODO this is a hack)
236
+          run(`echo "this file is not needed" > ${storage.blue}/blue.txt`, (err, output) => {
237
+            assert.error(err, 'no error on echo to file')
238
+            replicate(orangeDrive, blueDrive, check)
239
+          })
240
+        })
241
+      })
242
+    })
243
+
244
+    function check (err) {
245
+      assert.error(err, 'no error on replicate')
246
+      run(`ls ${storage.blue}`, (err, output) => {
247
+        assert.error(err, 'no error on ls')
248
+        assert.true(output[0].split('\n').indexOf('hello.txt') > -1, 'file successfully replicated')
249
+        run(`rm -f ${storage.blue}/hello.txt`, (err, output) => {
250
+          assert.error(err, 'no error on rm file')
251
+          run(`cat ${storage.blue}/hello.txt`, (err, output) => {
252
+            assert.ok(err, 'error on cat out file')
253
+            console.log(output)
254
+            replicate(orangeDrive, blueDrive, (err) => {
255
+              assert.error(err, 'no error on replicate')
256
+              run(`ls ${storage.orange}`, (err, output) => {
257
+                assert.error(err, 'no error')
258
+                assert.same(output, [''], 'ls returns')
259
+                cleanup(destroy.orange, storage.orange).then(() => {
260
+                  cleanup(destroy.blue, storage.blue).then(next)
261
+                })
262
+              })
263
+            })
264
+          })
265
+        })
266
+      })
267
+    }
268
+  })
269
+
148 270
   context("replicate - append to another peer's empty file - using unix commands", (assert, next) => {
149 271
     var feedsOnDisk = {
150 272
       orange: tmp(),