Skip to content

Commit bea61d5

Browse files
committed
tests/lapi: package tests
1 parent bfac9d7 commit bea61d5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/lapi/package_require_test.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
17+
local MAX_STR_LEN = test_lib.MAX_STR_LEN
18+
local MAX_PATH_NUM = 10
19+
20+
local function build_path(fdp)
21+
local count = fdp:consume_integer(0, MAX_PATH_NUM)
22+
local paths = fdp:consume_strings(MAX_STR_LEN, count)
23+
local path_str = table.concat(paths, ";")
24+
local enable_def_path = fdp:consume_boolean()
25+
return enable_def_path and path_str or path_str .. ";;"
26+
end
27+
28+
local function TestOneInput(buf)
29+
-- Save paths used by `require` to search for a Lua loader.
30+
local old_path = package.path
31+
local old_cpath = package.cpath
32+
33+
local fdp = luzer.FuzzedDataProvider(buf)
34+
local path = build_path(fdp)
35+
package.path = path
36+
local cpath = build_path(fdp)
37+
package.cpath = cpath
38+
39+
local module_name = fdp:consume_string(MAX_STR_LEN)
40+
-- If there is any error loading or running the module, or if
41+
-- it cannot find any loader for the module, then require
42+
-- signals an error,
43+
-- https://www.lua.org/manual/5.1/manual.html#pdf-require.
44+
pcall(require, module_name)
45+
46+
-- Teardown.
47+
package.path = old_path
48+
package.cpath = old_cpath
49+
end
50+
51+
local args = {
52+
artifact_prefix = "package_require_",
53+
}
54+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)