Skip to content

Commit 1fdabf7

Browse files
committed
tests/lapi: package tests
1 parent 0508724 commit 1fdabf7

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tests/lapi/package_require_test.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_PATH_NUM = 10
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, max_path_num)
24+
local count = fdp:consume_integer(0, max_path_num)
25+
local paths = fdp:consume_strings(test_lib.MAX_STR_LEN, count)
26+
local path_str = table.concat(paths, ";")
27+
local def_path = fdp:consume_boolean()
28+
return def_path and path_str or path_str .. ";;"
29+
end
30+
31+
local function TestOneInput(buf)
32+
local fdp = luzer.FuzzedDataProvider(buf)
33+
34+
local lua_path = build_path(fdp, MAX_PATH_NUM)
35+
setenv("LUA_PATH", lua_path)
36+
package.path = lua_path
37+
38+
local lua_cpath = build_path(fdp, MAX_PATH_NUM)
39+
setenv("LUA_CPATH", lua_cpath)
40+
package.cpath = lua_cpath
41+
42+
local module_name = fdp:consume_string(test_lib.MAX_STR_LEN)
43+
pcall(require, module_name)
44+
45+
-- Teardown.
46+
setenv("LUA_PATH", "")
47+
setenv("LUA_CPATH", "")
48+
end
49+
50+
local args = {
51+
artifact_prefix = "package_require_",
52+
}
53+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)