1
0
mirror of https://github.com/fuse-friends/fuse-native synced 2024-10-27 18:34:01 +00:00
fuse-friends_fuse-native/test/write.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-08-01 22:19:00 +00:00
const tape = require('tape')
const fs = require('fs')
const path = require('path')
const Fuse = require('../')
2020-01-29 12:32:31 +00:00
const createMountpoint = require('./fixtures/mnt')
2019-08-01 22:19:00 +00:00
const stat = require('./fixtures/stat')
2019-11-19 13:04:46 +00:00
const { unmount } = require('./helpers')
2019-08-01 12:24:01 +00:00
2020-01-29 12:32:31 +00:00
const mnt = createMountpoint()
2019-08-01 12:24:01 +00:00
tape('write', function (t) {
var created = false
2019-08-06 08:55:08 +00:00
var data = Buffer.alloc(1024)
2019-08-01 12:24:01 +00:00
var size = 0
var ops = {
force: true,
readdir: function (path, cb) {
2019-08-06 08:55:08 +00:00
if (path === '/') return process.nextTick(cb, null, created ? ['hello'] : [], [])
return process.nextTick(cb, Fuse.ENOENT)
2019-08-01 12:24:01 +00:00
},
truncate: function (path, size, cb) {
2019-08-06 08:55:08 +00:00
process.nextTick(cb, 0)
2019-08-01 12:24:01 +00:00
},
getattr: function (path, cb) {
2019-08-06 08:55:08 +00:00
if (path === '/') return process.nextTick(cb, null, stat({ mode: 'dir', size: 4096 }))
if (path === '/hello' && created) return process.nextTick(cb, 0, stat({ mode: 'file', size: size }))
return process.nextTick(cb, Fuse.ENOENT)
2019-08-01 12:24:01 +00:00
},
create: function (path, flags, cb) {
t.ok(!created, 'file not created yet')
created = true
2019-08-06 08:55:08 +00:00
process.nextTick(cb, 0, 42)
2019-08-01 12:24:01 +00:00
},
release: function (path, fd, cb) {
2019-08-06 08:55:08 +00:00
process.nextTick(cb, 0)
2019-08-01 12:24:01 +00:00
},
write: function (path, fd, buf, len, pos, cb) {
buf.slice(0, len).copy(data, pos)
size = Math.max(pos + len, size)
2019-08-06 08:55:08 +00:00
process.nextTick(cb, buf.length)
2019-08-01 12:24:01 +00:00
}
}
2019-08-01 22:19:00 +00:00
const fuse = new Fuse(mnt, ops, { debug: true })
fuse.mount(function (err) {
2019-08-01 12:24:01 +00:00
t.error(err, 'no error')
fs.writeFile(path.join(mnt, 'hello'), 'hello world', function (err) {
t.error(err, 'no error')
2019-08-06 08:55:08 +00:00
t.same(data.slice(0, size), Buffer.from('hello world'), 'data was written')
2019-08-01 12:24:01 +00:00
2019-11-19 13:04:46 +00:00
unmount(fuse, function () {
2019-08-01 12:24:01 +00:00
t.end()
})
})
})
})