Blame specs/spec_helper.lua

Packit 437b5e
if os.getenv "installcheck" == nil then
Packit 437b5e
  -- Unless we're running inside `make installcheck`, add the dev-tree
Packit 437b5e
  -- directories to the module search paths.
Packit 437b5e
  local std = require "specl.std"
Packit 437b5e
Packit 437b5e
  local top_srcdir = os.getenv "top_srcdir" or "."
Packit 437b5e
  local top_builddir = os.getenv "top_builddir" or "."
Packit 437b5e
Packit 437b5e
  package.path  = std.package.normalize (
Packit 437b5e
		    top_builddir .. "/lib/?.lua",
Packit 437b5e
		    top_srcdir .. "/lib/?.lua",
Packit 437b5e
		    top_builddir .. "/lib/?/init.lua",
Packit 437b5e
		    top_srcdir .. "/lib/?/init.lua",
Packit 437b5e
		    package.path)
Packit 437b5e
  package.cpath = std.package.normalize (
Packit 437b5e
		    top_builddir .. "/ext/posix/.libs/?.so",
Packit 437b5e
		    top_srcdir .. "/ext/posix/.libs/?.so",
Packit 437b5e
		    top_builddir .. "/ext/posix/_libs/?.dll",
Packit 437b5e
		    top_srcdir .. "/ext/posix/_libs/?.dll",
Packit 437b5e
		    package.cpath)
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
local bit = require "bit32"
Packit 437b5e
band, bnot, bor = bit.band, bit.bnot, bit.bor
Packit 437b5e
Packit 437b5e
badargs = require "specl.badargs"
Packit 437b5e
hell    = require "specl.shell"
Packit 437b5e
posix   = require "posix"
Packit 437b5e
Packit 437b5e
Packit 437b5e
-- Allow user override of LUA binary used by hell.spawn, falling
Packit 437b5e
-- back to environment PATH search for "lua" if nothing else works.
Packit 437b5e
local LUA = os.getenv "LUA" or "lua"
Packit 437b5e
Packit 437b5e
Packit 437b5e
-- Easily check for std.object.type compatibility.
Packit 437b5e
function prototype (o)
Packit 437b5e
  return (getmetatable (o) or {})._type or io.type (o) or type (o)
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
local function mkscript (code)
Packit 437b5e
  local f = os.tmpname ()
Packit 437b5e
  local h = io.open (f, "w")
Packit 437b5e
  h:write (code)
Packit 437b5e
  h:close ()
Packit 437b5e
  return f
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
--- Run some Lua code with the given arguments and input.
Packit 437b5e
-- @string code valid Lua code
Packit 437b5e
-- @tparam[opt={}] string|table arg single argument, or table of
Packit 437b5e
--   arguments for the script invocation
Packit 437b5e
-- @string[opt] stdin standard input contents for the script process
Packit 437b5e
-- @treturn specl.shell.Process|nil status of resulting process if
Packit 437b5e
--   execution was successful, otherwise nil
Packit 437b5e
function luaproc (code, arg, stdin)
Packit 437b5e
  local f = mkscript (code)
Packit 437b5e
  if type (arg) ~= "table" then arg = {arg} end
Packit 437b5e
  local cmd = {LUA, f, unpack (arg)}
Packit 437b5e
  -- inject env and stdin keys separately to avoid truncating `...` in
Packit 437b5e
  -- cmd constructor
Packit 437b5e
  cmd.stdin = stdin
Packit 437b5e
  cmd.env = {
Packit 437b5e
    LUA_CPATH    = package.cpath,
Packit 437b5e
    LUA_PATH     = package.path,
Packit 437b5e
    LUA_INIT     = "",
Packit 437b5e
    LUA_INIT_5_2 = ""
Packit 437b5e
  }
Packit 437b5e
  local proc = hell.spawn (cmd)
Packit 437b5e
  os.remove (f)
Packit 437b5e
  return proc
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
-- Use a consistent template for all temporary files.
Packit 437b5e
TMPDIR = posix.getenv ("TMPDIR") or "/tmp"
Packit 437b5e
template = TMPDIR .. "/luaposix-test-XXXXXX"
Packit 437b5e
Packit 437b5e
-- Allow comparison against the error message of a function call result.
Packit 437b5e
function Emsg (_, msg) return msg or "" end
Packit 437b5e
Packit 437b5e
-- Collect stdout from a shell command, and strip surrounding whitespace.
Packit 437b5e
function cmd_output (cmd)
Packit 437b5e
  return hell.spawn (cmd).output:gsub ("^%s+", ""):gsub ("%s+$", "")
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
local st = require "posix.sys.stat"
Packit 437b5e
local stat, S_ISDIR = st.lstat, st.S_ISDIR
Packit 437b5e
Packit 437b5e
-- Recursively remove a temporary directory.
Packit 437b5e
function rmtmp (dir)
Packit 437b5e
  for f in posix.files (dir) do
Packit 437b5e
    if f ~= "." and f ~= ".." then
Packit 437b5e
      local path = dir .. "/" .. f
Packit 437b5e
      if S_ISDIR (stat (path).st_mode) ~= 0 then
Packit 437b5e
        rmtmp (path)
Packit 437b5e
      else
Packit 437b5e
        os.remove (path)
Packit 437b5e
      end
Packit 437b5e
    end
Packit 437b5e
  end
Packit 437b5e
  os.remove (dir)
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
-- Create an empty file at PATH.
Packit 437b5e
function touch (path) io.open (path, "w+"):close () end
Packit 437b5e
Packit 437b5e
Packit 437b5e
-- Format a bad argument type error.
Packit 437b5e
local function typeerrors (fname, i, want, field, got)
Packit 437b5e
  return {
Packit 437b5e
    badargs.format ("?", i, want, field, got),   -- LuaJIT
Packit 437b5e
    badargs.format (fname, i, want, field, got), -- PUC-Rio
Packit 437b5e
  }
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
Packit 437b5e
function init (M, fname)
Packit 437b5e
  return M[fname], function (...) return typeerrors (fname, ...) end
Packit 437b5e
end