2024-01-12 05:03:25 -07:00
|
|
|
local uv = vim.uv
|
2016-06-21 15:01:21 -07:00
|
|
|
|
2017-03-11 03:28:18 -07:00
|
|
|
local helpers = require('test.unit.helpers')(after_each)
|
2017-03-04 18:02:45 -07:00
|
|
|
local itp = helpers.gen_itp(it)
|
2016-06-21 15:01:21 -07:00
|
|
|
|
|
|
|
local eq = helpers.eq
|
|
|
|
local ffi = helpers.ffi
|
|
|
|
local cimport = helpers.cimport
|
2017-03-19 09:16:44 -07:00
|
|
|
local cppimport = helpers.cppimport
|
2023-04-04 12:59:06 -07:00
|
|
|
local mkdir = helpers.mkdir
|
2016-06-21 15:01:21 -07:00
|
|
|
|
2017-03-19 09:16:44 -07:00
|
|
|
local m = cimport('./src/nvim/os/os.h', './src/nvim/os/fileio.h')
|
|
|
|
cppimport('fcntl.h')
|
2016-06-21 15:01:21 -07:00
|
|
|
|
2016-06-23 15:12:30 -07:00
|
|
|
local fcontents = ''
|
2016-06-21 15:01:21 -07:00
|
|
|
for i = 0, 255 do
|
2016-06-23 15:12:30 -07:00
|
|
|
fcontents = fcontents .. (i == 0 and '\0' or ('%c'):format(i))
|
2016-06-21 15:01:21 -07:00
|
|
|
end
|
2016-06-23 15:12:30 -07:00
|
|
|
fcontents = fcontents:rep(16)
|
2016-06-21 15:01:21 -07:00
|
|
|
|
|
|
|
local dir = 'Xtest-unit-file_spec.d'
|
|
|
|
local file1 = dir .. '/file1.dat'
|
|
|
|
local file2 = dir .. '/file2.dat'
|
|
|
|
local linkf = dir .. '/file.lnk'
|
|
|
|
local linkb = dir .. '/broken.lnk'
|
|
|
|
local filec = dir .. '/created-file.dat'
|
|
|
|
|
|
|
|
before_each(function()
|
2023-12-04 15:32:39 -07:00
|
|
|
mkdir(dir)
|
2016-06-21 15:01:21 -07:00
|
|
|
|
|
|
|
local f1 = io.open(file1, 'w')
|
|
|
|
f1:write(fcontents)
|
|
|
|
f1:close()
|
|
|
|
|
|
|
|
local f2 = io.open(file2, 'w')
|
|
|
|
f2:write(fcontents)
|
|
|
|
f2:close()
|
|
|
|
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_symlink('file1.dat', linkf)
|
|
|
|
uv.fs_symlink('broken.dat', linkb)
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(file1)
|
|
|
|
os.remove(file2)
|
|
|
|
os.remove(linkf)
|
|
|
|
os.remove(linkb)
|
|
|
|
os.remove(filec)
|
2024-01-12 05:03:25 -07:00
|
|
|
uv.fs_rmdir(dir)
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
local function file_open(fname, flags, mode)
|
|
|
|
local ret2 = ffi.new('FileDescriptor')
|
|
|
|
local ret1 = m.file_open(ret2, fname, flags, mode)
|
|
|
|
return ret1, ret2
|
|
|
|
end
|
|
|
|
|
2017-07-04 07:08:43 -07:00
|
|
|
local function file_open_fd(fd, flags)
|
2017-03-19 09:16:44 -07:00
|
|
|
local ret2 = ffi.new('FileDescriptor')
|
2017-07-04 07:08:43 -07:00
|
|
|
local ret1 = m.file_open_fd(ret2, fd, flags)
|
2017-03-19 09:16:44 -07:00
|
|
|
return ret1, ret2
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:01:21 -07:00
|
|
|
local function file_write(fp, buf)
|
|
|
|
return m.file_write(fp, buf, #buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function file_read(fp, size)
|
|
|
|
local buf = nil
|
|
|
|
if size == nil then
|
|
|
|
size = 0
|
|
|
|
else
|
2016-06-23 12:25:35 -07:00
|
|
|
-- For some reason if length of NUL-bytes-string is the same as `char[?]`
|
|
|
|
-- size luajit garbage collector crashes. But it does not do so in
|
|
|
|
-- os_read[v] tests in os/fs_spec.lua.
|
|
|
|
buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
|
2016-06-21 15:01:21 -07:00
|
|
|
end
|
|
|
|
local ret1 = m.file_read(fp, buf, size)
|
|
|
|
local ret2 = ''
|
|
|
|
if buf ~= nil then
|
|
|
|
ret2 = ffi.string(buf, size)
|
|
|
|
end
|
|
|
|
return ret1, ret2
|
|
|
|
end
|
|
|
|
|
2017-04-01 11:13:21 -07:00
|
|
|
local function file_flush(fp)
|
|
|
|
return m.file_flush(fp)
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:01:21 -07:00
|
|
|
local function file_fsync(fp)
|
|
|
|
return m.file_fsync(fp)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function file_skip(fp, size)
|
|
|
|
return m.file_skip(fp, size)
|
|
|
|
end
|
|
|
|
|
2017-03-19 09:16:44 -07:00
|
|
|
describe('file_open_fd', function()
|
|
|
|
itp('can use file descriptor returned by os_open for reading', function()
|
|
|
|
local fd = m.os_open(file1, m.kO_RDONLY, 0)
|
2017-12-03 06:49:30 -07:00
|
|
|
local err, fp = file_open_fd(fd, m.kFileReadOnly)
|
2017-03-19 09:16:44 -07:00
|
|
|
eq(0, err)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ #fcontents, fcontents }, { file_read(fp, #fcontents) })
|
2017-07-04 07:08:43 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2017-03-19 09:16:44 -07:00
|
|
|
end)
|
|
|
|
itp('can use file descriptor returned by os_open for writing', function()
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(nil, uv.fs_stat(filec))
|
2017-03-19 09:16:44 -07:00
|
|
|
local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
|
2017-12-03 06:49:30 -07:00
|
|
|
local err, fp = file_open_fd(fd, m.kFileWriteOnly)
|
2017-03-19 09:16:44 -07:00
|
|
|
eq(0, err)
|
|
|
|
eq(4, file_write(fp, 'test'))
|
2017-07-04 07:08:43 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(4, uv.fs_stat(filec).size)
|
2017-03-19 09:16:44 -07:00
|
|
|
eq('test', io.open(filec):read('*a'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-06-21 15:01:21 -07:00
|
|
|
describe('file_open', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can create a rwx------ file with kFileCreate', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreate, 448)
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(filec)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(33216, attrs.mode)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can create a rw------- file with kFileCreate', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreate, 384)
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(filec)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(33152, attrs.mode)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can create a rwx------ file with kFileCreateOnly', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 448)
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(filec)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(33216, attrs.mode)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can create a rw------- file with kFileCreateOnly', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(filec)
|
2023-04-04 12:59:06 -07:00
|
|
|
eq(33152, attrs.mode)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to open an existing file with kFileCreateOnly', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(file1, m.kFileCreateOnly, 384)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(m.UV_EEXIST, err)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to open an symlink with kFileNoSymlink', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(linkf, m.kFileNoSymlink, 384)
|
2016-06-23 14:06:46 -07:00
|
|
|
-- err is UV_EMLINK in FreeBSD, but if I use `ok(err == m.UV_ELOOP or err ==
|
|
|
|
-- m.UV_EMLINK)`, then I loose the ability to see actual `err` value.
|
2023-12-04 15:32:39 -07:00
|
|
|
if err ~= m.UV_ELOOP then
|
|
|
|
eq(m.UV_EMLINK, err)
|
|
|
|
end
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can open an existing file write-only with kFileCreate', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, m.kFileCreate, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can open an existing file read-only with zero', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can open an existing file read-only with kFileReadOnly', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, m.kFileReadOnly, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can open an existing file read-only with kFileNoSymlink', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, m.kFileNoSymlink, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can truncate an existing file with kFileTruncate', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, m.kFileTruncate, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(file1)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(0, attrs.size)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can open an existing file write-only with kFileWriteOnly', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, m.kFileWriteOnly, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(file1)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(4096, attrs.size)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to create a file with just kFileWriteOnly', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(filec, m.kFileWriteOnly, 384)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(m.UV_ENOENT, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(filec)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(nil, attrs)
|
|
|
|
end)
|
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
itp('can truncate an existing file with kFileTruncate when opening a symlink', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(linkf, m.kFileTruncate, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
local attrs = uv.fs_stat(file1)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(0, attrs.size)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to open a directory write-only', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(dir, m.kFileWriteOnly, 384)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(m.UV_EISDIR, err)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to open a broken symbolic link write-only', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(linkb, m.kFileWriteOnly, 384)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(m.UV_ENOENT, err)
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('fails to open a broken symbolic link read-only', function()
|
2016-06-23 15:12:30 -07:00
|
|
|
local err, _ = file_open(linkb, m.kFileReadOnly, 384)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(m.UV_ENOENT, err)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2017-04-02 12:11:35 -07:00
|
|
|
describe('file_close', function()
|
|
|
|
itp('can flush writes to disk also with true argument', function()
|
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
2017-04-02 17:07:01 -07:00
|
|
|
eq(0, err)
|
2017-04-02 12:11:35 -07:00
|
|
|
local wsize = file_write(fp, 'test')
|
|
|
|
eq(4, wsize)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(0, uv.fs_stat(filec).size)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, true))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(wsize, uv.fs_stat(filec).size)
|
2017-04-02 12:11:35 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-06-21 15:01:21 -07:00
|
|
|
describe('file_fsync', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can flush writes to disk', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, file_fsync(fp))
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(0, uv.fs_stat(filec).size)
|
2016-06-21 15:01:21 -07:00
|
|
|
local wsize = file_write(fp, 'test')
|
|
|
|
eq(4, wsize)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(0, uv.fs_stat(filec).size)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(0, file_fsync(fp))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(wsize, uv.fs_stat(filec).size)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2017-04-01 11:13:21 -07:00
|
|
|
describe('file_flush', function()
|
|
|
|
itp('can flush writes to disk', function()
|
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, file_flush(fp))
|
|
|
|
eq(0, err)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(0, uv.fs_stat(filec).size)
|
2017-04-01 11:13:21 -07:00
|
|
|
local wsize = file_write(fp, 'test')
|
|
|
|
eq(4, wsize)
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(0, uv.fs_stat(filec).size)
|
2017-04-01 11:13:21 -07:00
|
|
|
eq(0, file_flush(fp))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(wsize, uv.fs_stat(filec).size)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('file_read', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read small chunks of input until eof', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
|
|
|
local shift = 0
|
|
|
|
while shift < #fcontents do
|
|
|
|
local size = 3
|
|
|
|
local exp_err = size
|
|
|
|
local exp_s = fcontents:sub(shift + 1, shift + size)
|
|
|
|
if shift + size >= #fcontents then
|
|
|
|
exp_err = #fcontents - shift
|
2023-12-04 15:32:39 -07:00
|
|
|
exp_s = (fcontents:sub(shift + 1, shift + size) .. (('\0'):rep(size - exp_err)))
|
2016-06-21 15:01:21 -07:00
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ exp_err, exp_s }, { file_read(fp, size) })
|
2016-06-21 15:01:21 -07:00
|
|
|
shift = shift + size
|
|
|
|
end
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read the whole file at once', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ #fcontents, fcontents }, { file_read(fp, #fcontents) })
|
|
|
|
eq({ 0, ('\0'):rep(#fcontents) }, { file_read(fp, #fcontents) })
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read more then 1024 bytes after reading a small chunk', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ 5, fcontents:sub(1, 5) }, { file_read(fp, 5) })
|
|
|
|
eq({ #fcontents - 5, fcontents:sub(6) .. (('\0'):rep(5)) }, { file_read(fp, #fcontents) })
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can read file by 768-byte-chunks', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
|
|
|
local shift = 0
|
|
|
|
while shift < #fcontents do
|
|
|
|
local size = 768
|
|
|
|
local exp_err = size
|
|
|
|
local exp_s = fcontents:sub(shift + 1, shift + size)
|
|
|
|
if shift + size >= #fcontents then
|
|
|
|
exp_err = #fcontents - shift
|
2023-12-04 15:32:39 -07:00
|
|
|
exp_s = (fcontents:sub(shift + 1, shift + size) .. (('\0'):rep(size - exp_err)))
|
2016-06-21 15:01:21 -07:00
|
|
|
end
|
2023-12-04 15:32:39 -07:00
|
|
|
eq({ exp_err, exp_s }, { file_read(fp, size) })
|
2016-06-21 15:01:21 -07:00
|
|
|
shift = shift + size
|
|
|
|
end
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('file_write', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can write the whole file at once', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
2016-06-23 15:12:30 -07:00
|
|
|
local wr = file_write(fp, fcontents)
|
|
|
|
eq(#fcontents, wr)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(wr, uv.fs_stat(filec).size)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(fcontents, io.open(filec):read('*a'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can write the whole file by small chunks', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
|
|
|
local shift = 0
|
|
|
|
while shift < #fcontents do
|
|
|
|
local size = 3
|
|
|
|
local s = fcontents:sub(shift + 1, shift + size)
|
2016-06-23 15:12:30 -07:00
|
|
|
local wr = file_write(fp, s)
|
|
|
|
eq(wr, #s)
|
2016-06-21 15:01:21 -07:00
|
|
|
shift = shift + size
|
|
|
|
end
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(#fcontents, uv.fs_stat(filec).size)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(fcontents, io.open(filec):read('*a'))
|
|
|
|
end)
|
|
|
|
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can write the whole file by 768-byte-chunks', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(true, fp.wr)
|
|
|
|
local shift = 0
|
|
|
|
while shift < #fcontents do
|
|
|
|
local size = 768
|
|
|
|
local s = fcontents:sub(shift + 1, shift + size)
|
2016-06-23 15:12:30 -07:00
|
|
|
local wr = file_write(fp, s)
|
|
|
|
eq(wr, #s)
|
2016-06-21 15:01:21 -07:00
|
|
|
shift = shift + size
|
|
|
|
end
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2024-01-12 05:03:25 -07:00
|
|
|
eq(#fcontents, uv.fs_stat(filec).size)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(fcontents, io.open(filec):read('*a'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('file_skip', function()
|
2017-03-04 18:02:45 -07:00
|
|
|
itp('can skip 3 bytes', function()
|
2016-06-21 15:01:21 -07:00
|
|
|
local err, fp = file_open(file1, 0, 384)
|
|
|
|
eq(0, err)
|
|
|
|
eq(false, fp.wr)
|
|
|
|
eq(3, file_skip(fp, 3))
|
2016-06-23 15:12:30 -07:00
|
|
|
local rd, s = file_read(fp, 3)
|
|
|
|
eq(3, rd)
|
2016-06-21 15:01:21 -07:00
|
|
|
eq(fcontents:sub(4, 6), s)
|
2017-04-02 12:11:35 -07:00
|
|
|
eq(0, m.file_close(fp, false))
|
2016-06-21 15:01:21 -07:00
|
|
|
end)
|
|
|
|
end)
|