Skip to content

Commit fa4a2e8

Browse files
committed
tests/lapi: package tests
1 parent d4b91f6 commit fa4a2e8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.3 – Modules
6+
https://www.lua.org/manual/5.1/manual.html#5.3
7+
8+
PIL: 15 – Packages
9+
https://www.lua.org/pil/15.html
10+
11+
Synopsis: require(modname)
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
local MAX_INT = test_lib.MAX_INT
17+
18+
local function setenv(key, value)
19+
local env = ("export %s=%s"):format(key, value)
20+
os.execute(env)
21+
end
22+
23+
local function build_path(fdp)
24+
local max_len = fdp:consume_number(0, 100)
25+
local count = fdp:consume_number(0, 5)
26+
local paths = fdp:consume_strings(max_len, count)
27+
local path_str = table.concat(paths, ";")
28+
local def_path = fdp:consume_boolean()
29+
return def_path and path_str or path_str .. ";;"
30+
end
31+
32+
local function TestOneInput(buf)
33+
local fdp = luzer.FuzzedDataProvider(buf)
34+
35+
local lua_path = build_path(fdp)
36+
setenv("LUA_PATH", lua_path)
37+
package.path = lua_path
38+
39+
local lua_cpath = build_path(fdp)
40+
setenv("LUA_CPATH", lua_cpath)
41+
package.cpath = lua_cpath
42+
43+
local max_len = fdp:consume_number(0, MAX_INT)
44+
local module_name = fdp:consume_string(max_len)
45+
pcall(require, module_name)
46+
47+
-- Teardown.
48+
setenv("LUA_PATH", "")
49+
setenv("LUA_CPATH", "")
50+
end
51+
52+
local args = {
53+
artifact_prefix = "package_require_",
54+
}
55+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)