1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
before:
this_module = 'posix.stdio'
global_table = '_G'
M = require(this_module)
specify posix.stdio:
- context when required:
- it does not touch the global table:
expect(show_apis {added_to=global_table, by=this_module}).
to_equal {}
- describe fdopen:
- before:
fdopen = M.fdopen
fd = fdopen(require 'posix.unistd'.STDOUT_FILENO, "w")
- context with bad arguments:
badargs.diagnose(fdopen, "(int,string)")
- it duplicates a stream:
expect(type(fd)).to_be "userdata"
- it closes an open stream: |
script = [[
local fcntl = require "posix.fcntl"
local stdio = require "posix.stdio"
local sys_stat = require "posix.sys.stat"
local unlink = require "posix.unistd".unlink
function create_file(path)
local flags = fcntl.O_WRONLY + fcntl.O_CREAT + fcntl.O_EXCL
local mode = sys_stat.S_IRUSR + sys_stat.S_IWUSR
local fd, message, open_err = fcntl.open(path, flags, mode)
if not fd then
return nil, message
end
return stdio.fdopen(fd, "w")
end
local path = '.delete.me'
unlink(path)
local f, msg = create_file(path)
if not f then
print(msg)
else
f:close()
end
unlink(path)
os.exit(0)
]]
pending "issue #217"
expect(luaproc(script)).to_succeed_with ''
- it writes to the duplicated stream: |
script = [[
local stdio = require "posix.stdio"
local fd = stdio.fdopen(require 'posix.unistd'.STDOUT_FILENO, "w")
-- Lua 5.1 file.write returns true; > 5.1 returns file handle
local r = fd:write("writing to fdopen(stdout)")
os.exit(r ~= nil and 0 or 1)
]]
pending "issue #217"
expect(luaproc(script)).to_succeed_with "writing to fdopen(stdout)"
- describe rename:
- before:
rename = M.rename
fd, path = require "posix.stdlib".mkstemp(template)
- after:
require 'posix.unistd'.close(fd)
os.remove(path)
- context with bad arguments:
badargs.diagnose(rename, "(string,string)")
- it renames an existing file:
newpath = path .. ".renamed"
expect(fd).not_to_be(nil)
require 'posix.unistd'.write(fd, "rename me")
rename(path, newpath)
expect(io.open(path, "r")).to_be(nil)
fh = io.open(newpath, "r")
expect(fh).not_to_be(nil)
expect(fh:read()).to_be "rename me"
fh:close()
rename(newpath, path)
|