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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
before:
this_module = 'posix.stdlib'
global_table = '_G'
M = require(this_module)
specify posix.stdlib:
- context when required:
- it does not touch the global table:
expect(show_apis {added_to=global_table, by=this_module}).
to_equal {}
- describe abort:
- context with bad arguments:
badargs.diagnose(M.abort, "()")
- describe getenv:
- before:
getenv = M.getenv
- context with bad arguments:
badargs.diagnose(getenv, "(?string)")
- it fetches a table of process environment variables: |
volatile = { _=true, CWD=true, LUA_PATH=true, PWD=true, SHLVL=true, }
ESC = string.char(27)
for k,v in pairs(getenv()) do
if not volatile[k] then
expect(hell.spawn('echo "' .. k .. '=$' .. k .. '" | tr "' .. ESC .. '" "^"')).
to_contain_output(k .. "=" .. string.gsub(v, '\\033', '^'))
end
end
- it fetches a named process environment variable:
expect(getenv "USER").to_be(cmd_output('echo "$USER"'))
expect(getenv "HOME").to_be(cmd_output('echo "$HOME"'))
- it returns nil for an absent environment setting:
expect(getenv "very_unlikely_to_be_set").to_be(nil)
- describe grantpt:
- context with bad arguments:
badargs.diagnose(M.grantpt, "(int)")
- describe mkdtemp:
- before:
st = require "posix.sys.stat"
mkdtemp = M.mkdtemp
stat, IFMT, IFDIR, IRWXU = st.stat, st.S_IFMT, st.S_IFDIR, st.S_IRWXU
IRWXA = bor(st.S_IRWXU, st.S_IRWXG, st.S_IRWXO)
- context with bad arguments:
badargs.diagnose(mkdtemp, "(string)")
- it creates a temporary directory from a template:
dir, errmsg = mkdtemp(template)
expect(dir).not_to_be(nil)
dirstat = stat(dir)
expect(band(dirstat.st_mode, IFMT)).to_be(IFDIR)
expect(band(dirstat.st_mode, IRWXA)).to_be(IRWXU)
rmtmp(dir)
- describe mkstemp:
- before:
fc = require "posix.fcntl"
st = require "posix.sys.stat"
unistd = require "posix.unistd"
O_RDONLY, open = fc.O_RDONLY, fc.open
mkstemp = M.mkstemp
close, isatty, read, write =
unistd.close, unistd.isatty, unistd.read, unistd.write
stat, IFMT, IFREG = st.stat, st.S_IFMT, st.S_IFREG
IRW_U = bor(st.S_IRUSR, st.S_IWUSR)
IRWXA = bor(st.S_IRWXU, st.S_IRWXG, st.S_IRWXO)
fd, path = mkstemp(template)
- after:
close(fd)
os.remove(path)
- context with bad arguments:
badargs.diagnose(mkstemp, "(string)")
- it creates a temporary file from a template:
expect(fd).not_to_be(nil)
write(fd, "12345")
expect(isatty(fd)).not_to_be(true)
fstat = stat(path)
expect(band(fstat.st_mode, IFMT)).to_be(IFREG)
expect(band(fstat.st_mode, IRWXA)).to_be(IRW_U)
expect(fstat.st_size).to_be(5)
fd2 = open(path, O_RDONLY)
expect(read(fd2, 5)).to_be "12345"
close(fd2)
- it creates a new temporary file on each call:
fd2, another = mkstemp(template)
expect(fd2).not_to_be(fd)
expect(another).not_to_be(path)
close(fd2)
os.remove(another)
- describe openpt:
- before:
fc = require "posix.fcntl"
st = require "posix.sys.stat"
close = require "posix.unistd".close
grantpt, openpt, ptsname, unlockpt =
M.grantpt, M.openpt, M.ptsname, M.unlockpt
O_RDWR, O_NOCTTY, open = fc.O_RDWR, fc.O_NOCTTY, fc.open
stat, IFMT, IFCHR = st.stat, st.S_IFMT, st.S_IFCHR
- context with bad arguments:
badargs.diagnose(openpt, "(int)")
- it can create a pseudoterminal:
masterfd = openpt(bor(O_RDWR, O_NOCTTY))
expect(type(masterfd)).to_be "number"
expect(masterfd > 0).to_be(true)
expect(Emsg(grantpt(masterfd))).to_be ""
expect(Emsg(unlockpt(masterfd))).to_be ""
slavename = ptsname(masterfd)
slavestat = stat(slavename)
expect(band(slavestat.st_mode, IFMT)).to_be(IFCHR)
slavefd = open(slavename, bor(O_RDWR, O_NOCTTY))
expect(type(slavefd)).to_be "number"
expect(slavefd > 0).to_be(true)
close(slavefd)
close(masterfd)
- describe ptsname:
- context with bad arguments:
badargs.diagnose(M.ptsname, "(int)")
- describe realpath:
- context with bad arguments:
badargs.diagnose(M.realpath, "(string)")
- describe setenv:
- before:
getenv, setenv = M.getenv, M.setenv
- context with bad arguments:
badargs.diagnose(M.setenv, "(string, ?string, ?any)")
- it sets a new value in the process environment:
setenv("MYVAR", "123")
expect(getenv "MYVAR").to_be "123"
setenv("MYVAR", nil)
expect(getenv "MYVAR").to_be(nil)
- describe unlockpt:
- context with bad arguments:
badargs.diagnose(M.unlockpt, "(int)")
|