memfd.lua
local sh = require 'lsh'
print('-- memfd --')
do
local buf = '123'
local memfd = sh.memfd(buf)
assert(tostring(memfd) == '123')
memfd:write('456789')
assert(tostring(memfd) == '123456789')
memfd:seek(0)
memfd:write('foobar')
assert(tostring(memfd) == 'foobar789')
memfd:seek(6)
local res = memfd:read()
assert(res == '789')
memfd:seek(0)
local res = memfd:read()
assert(res == 'foobar789')
end
print('-- memfd exec io --')
do
local buf = 'hello'
local res, err = sh.cmd('cat'):stdin(sh.memfd(buf))
:output()
assert(not err)
assert(tostring(res.stdout) == buf)
end
print('-- memfd read lines --')
do
local buf = '123\n456\r\n\r\n\n567\nabc\n\na\rab\rfoo'
local out = {
'123',
'456',
'',
'',
'567',
'abc',
'',
'a',
'ab',
'foo'
}
local memfd = sh.memfd(buf)
local i = 1
for line in memfd:lines() do
assert(line == out[i])
i = i + 1
end
end