mirror of
https://github.com/mailscope/kumomta.git
synced 2026-08-21 03:48:17 +00:00
7e6ca34e2d
The read_dir and glob functions have been logically moved into that new namespace, leaving deprecated versions of them in the `kumo` module. A new `kumo.fs.open` function that works similarly to `io.open` is provided. This function cooperates with the kumo async io scheduler and won't block it if the filesystem is under pressure. It returns file handles that are simlar to the builtin lua file handle objects, but do not support formatting or parsing of writes or reads respectively: the calling code is responsible for that. The rationale for this difference is that is that lua's semantics for those functions are frankly a bit weird and are hard to replicate precisely.
44 lines
1.1 KiB
Lua
44 lines
1.1 KiB
Lua
local kumo = require 'kumo'
|
|
local utils = require 'policy-extras.policy_utils'
|
|
print 'doing filesystem tests'
|
|
|
|
local filename = os.tmpname()
|
|
|
|
local writer = kumo.fs.open(filename, 'w+')
|
|
local reader = kumo.fs.open(filename, 'r')
|
|
|
|
writer:write 'hello'
|
|
utils.assert_eq(reader:read(), '') -- no data visible until flushed
|
|
writer:write ' world'
|
|
writer:flush()
|
|
utils.assert_eq(reader:read(), 'hello world')
|
|
|
|
local new_pos = writer:seek 'set'
|
|
utils.assert_eq(new_pos, 0)
|
|
writer:write 'woot'
|
|
writer:seek 'set' -- check that seek implicitly flushes
|
|
utils.assert_eq(reader:read(), '')
|
|
|
|
reader:seek('set', 0)
|
|
utils.assert_eq(reader:read(4), 'woot')
|
|
reader:seek(4)
|
|
utils.assert_eq(reader:read(4), 'rld')
|
|
reader:seek('cur', -4)
|
|
utils.assert_eq(reader:read(4), 'orld')
|
|
reader:seek 'end'
|
|
utils.assert_eq(reader:read(), '')
|
|
|
|
assert(
|
|
not pcall(reader.seek, reader, 'bogus'),
|
|
'seek with bogus whence should error'
|
|
)
|
|
|
|
reader:close()
|
|
assert(not pcall(reader.read, reader), 'read after close should error')
|
|
|
|
writer:close()
|
|
assert(
|
|
not pcall(writer.write, writer, 'foo'),
|
|
'write after close should error'
|
|
)
|