Skip to content

Commit caec365

Browse files
committed
Allo filepath.Join to take zero args as its underlying go implementation does and add tests for it
1 parent 29a2c16 commit caec365

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

filepath/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ func IsAbs(L *lua.LState) int {
9494

9595
// Join lua fileapth.join(path, ...) joins any number of path elements into a single path, adding a Separator if necessary.
9696
func Join(L *lua.LState) int {
97-
path := L.CheckString(1)
98-
for i := 2; i <= L.GetTop(); i++ {
99-
add := L.CheckAny(i).String()
100-
path = filepath.Join(path, add)
97+
var elems []string
98+
for i := 1; i <= L.GetTop(); i++ {
99+
elem := L.CheckAny(i).String()
100+
elems = append(elems, elem)
101101
}
102+
path := filepath.Join(elems...)
102103
L.Push(lua.LString(path))
103104
return 1
104105
}

filepath/test/test_api.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ function Test_join_and_separator(t)
1010
assert(path == need_path, string.format("expected %s; got %s", need_path, path))
1111
end
1212

13+
function Test_join(t)
14+
tests = {
15+
{
16+
name = "1/2/3",
17+
args = { "1", "2", "3" },
18+
},
19+
{
20+
name = "foo",
21+
args = { "foo" },
22+
},
23+
{
24+
name = "",
25+
args = {},
26+
},
27+
{
28+
name = "/a/b/c",
29+
args = { "/", "a", "b", "c" },
30+
},
31+
}
32+
for _, tt in ipairs(tests) do
33+
t:Run(tt.name, function(t)
34+
assert:Equal(t, tt.name, filepath.join(unpack(tt.args)))
35+
end)
36+
end
37+
end
38+
1339
function Test_glob(t)
1440
local results = filepath.glob("test" .. filepath.separator() .. "*")
1541
assert(#results == 1, string.format("expected one glob result; got %d", #results))

0 commit comments

Comments
 (0)