Skip to content

Commit 0a0fecb

Browse files
author
inkyu-bae
committed
fix: avoid double semicolons in extra_lua_path and add usage comments
1 parent fcaab4a commit 0a0fecb

2 files changed

Lines changed: 105 additions & 5 deletions

File tree

t/APISIX.pm

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,44 @@ _EOC_
272272
my $extra_lua_path = "";
273273
my $extra_lua_cpath = "";
274274

275+
# Support for extra_lua_path / extra_lua_cpath:
276+
# These allow tests to load custom plugins from non-standard directories.
277+
#
278+
# Method 1 - Block definition (preferred, takes precedence):
279+
# --- extra_lua_path
280+
# /path/to/custom/plugins/?.lua;
281+
#
282+
# Method 2 - Via extra_yaml_config:
283+
# --- extra_yaml_config
284+
# apisix:
285+
# extra_lua_path: "/path/to/custom/plugins/?.lua;"
286+
#
287+
# The extracted paths are prepended to lua_package_path / lua_package_cpath,
288+
# consistent with the runtime behavior in apisix/cli/ops.lua.
289+
275290
# Method 1: Block definition (preferred)
276291
if ($block->extra_lua_path) {
277-
$extra_lua_path = $block->extra_lua_path . ";";
292+
$extra_lua_path = $block->extra_lua_path;
293+
$extra_lua_path .= ";" unless $extra_lua_path =~ /;$/;
278294
}
279295
if ($block->extra_lua_cpath) {
280-
$extra_lua_cpath = $block->extra_lua_cpath . ";";
296+
$extra_lua_cpath = $block->extra_lua_cpath;
297+
$extra_lua_cpath .= ";" unless $extra_lua_cpath =~ /;$/;
281298
}
282299

283300
# Method 2: Extract from extra_yaml_config if block definition not provided
284301
if (!$extra_lua_path && $block->extra_yaml_config) {
285302
my $extra_yaml = $block->extra_yaml_config;
286303
if ($extra_yaml =~ m/^\s*extra_lua_path:\s*["\']?([^"\'\n]+)["\']?/m) {
287-
$extra_lua_path = $1 . ";";
304+
$extra_lua_path = $1;
305+
$extra_lua_path .= ";" unless $extra_lua_path =~ /;$/;
288306
}
289307
}
290308
if (!$extra_lua_cpath && $block->extra_yaml_config) {
291309
my $extra_yaml = $block->extra_yaml_config;
292310
if ($extra_yaml =~ m/^\s*extra_lua_cpath:\s*["\']?([^"\'\n]+)["\']?/m) {
293-
$extra_lua_cpath = $1 . ";";
311+
$extra_lua_cpath = $1;
312+
$extra_lua_cpath .= ";" unless $extra_lua_cpath =~ /;$/;
294313
}
295314
}
296315

t/admin/extra-lua-path.t

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,88 @@ SUCCESS: block definition takes precedence
203203

204204

205205

206-
=== TEST 8: Load and execute custom plugin via extra_lua_path
206+
=== TEST 8: extra_lua_path with trailing semicolon should not produce double semicolons
207+
Verify that paths already ending with semicolons are handled correctly (no ;; in path)
208+
--- extra_lua_path: /trailing/semicolon/?.lua;
209+
--- config
210+
location /t {
211+
content_by_lua_block {
212+
local path = package.path
213+
local found = string.find(path, "/trailing/semicolon/?.lua", 1, true)
214+
-- Check there is no double semicolon right after the custom path
215+
local double_sc = string.find(path, "/trailing/semicolon/?.lua;;", 1, true)
216+
217+
if found and not double_sc then
218+
ngx.say("SUCCESS: trailing semicolon handled correctly")
219+
elseif double_sc then
220+
ngx.say("FAIL: double semicolons detected in path")
221+
else
222+
ngx.say("FAIL: path not found")
223+
end
224+
}
225+
}
226+
--- request
227+
GET /t
228+
--- response_body
229+
SUCCESS: trailing semicolon handled correctly
230+
231+
232+
233+
=== TEST 9: extra_lua_cpath with trailing semicolon should not produce double semicolons
234+
Verify that cpaths already ending with semicolons are handled correctly
235+
--- extra_lua_cpath: /trailing/semicolon/?.so;
236+
--- config
237+
location /t {
238+
content_by_lua_block {
239+
local cpath = package.cpath
240+
local found = string.find(cpath, "/trailing/semicolon/?.so", 1, true)
241+
local double_sc = string.find(cpath, "/trailing/semicolon/?.so;;", 1, true)
242+
243+
if found and not double_sc then
244+
ngx.say("SUCCESS: trailing semicolon handled correctly")
245+
elseif double_sc then
246+
ngx.say("FAIL: double semicolons detected in cpath")
247+
else
248+
ngx.say("FAIL: cpath not found")
249+
end
250+
}
251+
}
252+
--- request
253+
GET /t
254+
--- response_body
255+
SUCCESS: trailing semicolon handled correctly
256+
257+
258+
259+
=== TEST 10: extra_lua_path from yaml_config with trailing semicolon
260+
Verify that yaml_config paths with trailing semicolons are handled correctly
261+
--- extra_yaml_config
262+
apisix:
263+
extra_lua_path: "/yaml/trailing/?.lua;"
264+
--- config
265+
location /t {
266+
content_by_lua_block {
267+
local path = package.path
268+
local found = string.find(path, "/yaml/trailing/?.lua", 1, true)
269+
local double_sc = string.find(path, "/yaml/trailing/?.lua;;", 1, true)
270+
271+
if found and not double_sc then
272+
ngx.say("SUCCESS: yaml trailing semicolon handled correctly")
273+
elseif double_sc then
274+
ngx.say("FAIL: double semicolons detected in path")
275+
else
276+
ngx.say("FAIL: path not found")
277+
end
278+
}
279+
}
280+
--- request
281+
GET /t
282+
--- response_body
283+
SUCCESS: yaml trailing semicolon handled correctly
284+
285+
286+
287+
=== TEST 11: Load and execute custom plugin via extra_lua_path
207288
Verify that a real custom plugin can be loaded and executed using extra_lua_path
208289
--- extra_lua_path: t/plugin/custom-plugins/?.lua
209290
--- extra_yaml_config

0 commit comments

Comments
 (0)