From 8102c104461c1bb71a13e4531c59e23e371d35be Mon Sep 17 00:00:00 2001 From: zjncs <18910855655@163.com> Date: Fri, 4 Sep 2026 15:12:12 +0800 Subject: escape brackets in path.pattern path.pattern() escapes the lua pattern magic characters except '[' and ']', so a filename containing brackets is treated as a character class. For example, os.files("a[1]*.lua") matches "a1.lua" instead of "a[1].lua", and remove_files("a[1].lua") would remove "a1.lua" instead of "a[1].lua". Escape '[' and ']' like the other magic characters so bracket characters in file names are matched literally. --- tests/modules/os/test.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/modules') diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 2dcab35c6..d24615fd4 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -208,3 +208,13 @@ function test_isexec(t) os.tryrm(tempdir) end + +function test_files_with_brackets(t) + local tmpdir = os.tmpfile() .. ".dir" + io.writefile(path.join(tmpdir, "a[1].lua"), "x") + io.writefile(path.join(tmpdir, "a1.lua"), "x") + -- a wildcard is required, a plain existing file is matched without converting the pattern + local files = os.files(path.join(tmpdir, "a[1]*.lua")) + t:are_equal(files, {path.join(tmpdir, "a[1].lua")}) + os.tryrm(tmpdir) +end -- cgit v1.3.1