Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
# Lua 5.3 combinations
- lua: "lua 5.3"
lpeg: "1.0.1-1"
- lua: "lua 5.3"
lpeg: "1.0.1-1"
strict_module: "pl.strict"
- lua: "lua 5.3"
lpeg: "0.12.2-1"

Expand Down Expand Up @@ -93,7 +96,7 @@ jobs:

- name: Run Tests
env:
TEST_STRICT: 1
TEST_STRICT: ${{ matrix.strict_module || '1' }}
run: |
./run_tests.sh "${{ matrix.lua }}" "${{ matrix.lpeg }}"

Expand Down
8 changes: 8 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ else
source "$ENV_DIR/bin/activate"
fi

# Ensure penlight is installed if testing under pl.strict
if [ "${TEST_STRICT:-}" = "pl.strict" ]; then
if ! luarocks list | grep -q penlight; then
echo "Installing penlight for pl.strict testing..."
luarocks install https://luarocks.org/penlight-1.15.0-1.src.rock
fi
fi

# 3. Run the tests
echo "Running tests in the target environment..."
# Clean up any residual coverage file from previous runs
Expand Down
136 changes: 89 additions & 47 deletions tests/hook_require.lua
Original file line number Diff line number Diff line change
@@ -1,64 +1,106 @@
local os = require("os")

if os.getenv("TEST_STRICT") then
local mt = getmetatable(_G)
if not mt then
mt = {}
setmetatable(_G, mt)
end
mt.__declared = mt.__declared or {}
for k in pairs(_G) do
mt.__declared[k] = true
end
mt.__declared["_ENV"] = true
local test_strict = os.getenv("TEST_STRICT")

local debug_getinfo = debug.getinfo
local function what()
local d = debug_getinfo(3, "S")
return d and d.what or "C"
end
local old_require = require
if os.getenv('LUA_OLD_INIT') then
local loadstring = rawget(_G or {}, "loadstring") or load
assert(loadstring(os.getenv('LUA_OLD_INIT')))()
else
require("luarocks.require")
end
local luarocks_require = require

local old_newindex = mt.__newindex
mt.__newindex = function(t, n, v)
if not mt.__declared[n] then
local w = what()
if w ~= "main" and w ~= "C" then
error("assign to undeclared variable '"..n.."'", 2)
if test_strict and test_strict ~= "" and test_strict ~= "0" and test_strict ~= "false" then
if test_strict == "pl.strict" then
require("pl.strict")
local mt = getmetatable(_G)
if mt then
local allowed = {
newproxy = true,
statsfile = true,
reportfile = true,
configfile = true,
runreport = true,
deletestats = true,
include = true,
exclude = true,
reporter = true,
tick = true,
modules = true,
unpack = true,
_M = true,
_NAME = true,
_PACKAGE = true,
setup = true,
teardown = true,
setfenv = true,
getfenv = true,
module = true,
}
mt.__declared = mt.__declared or {}
for k, v in pairs(allowed) do
mt.__declared[k] = v
end
local old_index = mt.__index
mt.__index = function(t, n)
if n == nil then
return nil
end
return old_index(t, n)
end
mt.__declared[n] = true
end
if old_newindex then
old_newindex(t, n, v)
else
rawset(t, n, v)
else
local mt = getmetatable(_G)
if not mt then
mt = {}
setmetatable(_G, mt)
end
end
mt.__declared = mt.__declared or {}
for k in pairs(_G) do
mt.__declared[k] = true
end
mt.__declared["_ENV"] = true

local old_index = mt.__index
mt.__index = function(t, n)
if not mt.__declared[n] then
local d = debug_getinfo(2, "S")
if d and d.source and d.source:match("lua/json/") then
error("variable '"..n.."' is not declared", 2)
local debug_getinfo = debug.getinfo
local function what()
local d = debug_getinfo(3, "S")
return d and d.what or "C"
end

local old_newindex = mt.__newindex
mt.__newindex = function(t, n, v)
if not mt.__declared[n] then
local w = what()
if w ~= "main" and w ~= "C" then
error("assign to undeclared variable '"..n.."'", 2)
end
mt.__declared[n] = true
end
if old_newindex then
old_newindex(t, n, v)
else
rawset(t, n, v)
end
end
if old_index then
return old_index(t, n)
else
return rawget(t, n)

local old_index = mt.__index
mt.__index = function(t, n)
if not mt.__declared[n] then
local d = debug_getinfo(2, "S")
if d and d.source and d.source:match("lua/json/") then
error("variable '"..n.."' is not declared", 2)
end
end
if old_index then
return old_index(t, n)
else
return rawget(t, n)
end
end
end
end

local old_require = require
if os.getenv('LUA_OLD_INIT') then
local loadstring = rawget(_G or {}, "loadstring") or load
assert(loadstring(os.getenv('LUA_OLD_INIT')))()
else
require("luarocks.require")
end
local luarocks_require = require

function require(module, ...)
if module == "json" or module:match("^json%.") then
return old_require(module, ...)
Expand Down