Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lute/std/libs/path/posix.luau
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,23 @@ local function joinHelper(path: path, addend: pathlike): ()
end

function posix.join(...: pathlike): path
local parts = { ... }
local parts: { pathlike } = { ... }
if #parts == 0 then
return {
parts = {},
absolute = false,
}
end

local path = posix.parse(parts[1])
local path: path
if typeof(parts[1]) == "string" then
path = posix.parse(parts[1])
else
path = {
parts = table.clone((parts[1] :: path).parts),
absolute = (parts[1] :: path).absolute,
}
end

for i = 2, #parts do
joinHelper(path, parts[i])
Expand Down
11 changes: 10 additions & 1 deletion lute/std/libs/path/win32.luau
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,16 @@ function win32.join(...: pathlike): path
}
end

local path = win32.parse(parts[1])
local path: path
if typeof(parts[1]) == "string" then
path = win32.parse(parts[1])
else
path = {
parts = table.clone((parts[1] :: path).parts),
kind = (parts[1] :: path).kind,
driveLetter = (parts[1] :: path).driveLetter,
}
end

for i = 2, #parts do
joinHelper(path, parts[i])
Expand Down
14 changes: 14 additions & 0 deletions lute/std/libs/require.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local fs = require("@lute/fs")
local luau = require("@lute/luau")

local function requirebypath(path: string): any
local migrationHandle = fs.open(path, "r")

local migrationBytecode = luau.compile(fs.read(migrationHandle))

fs.close(migrationHandle)

return luau.load(migrationBytecode, path)()
end

return table.freeze({ requirebypath = requirebypath })
4 changes: 4 additions & 0 deletions tests/path.posix.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ test.suite("PathPosixJoinSuite", function(suite)
assert.eq(result.parts[2], "user")
assert.eq(result.parts[3], "documents")
assert.eq(result.parts[4], "file.txt")
assert.eq(#pathobj.parts, 2) -- original pathobj should be unchanged
assert.eq(pathobj.parts[1], "home")
assert.eq(pathobj.parts[2], "user")
assert.eq(pathobj.absolute, true)
end)

suite:case("join_error_on_absolute_addend", function(assert)
Expand Down
5 changes: 5 additions & 0 deletions tests/path.win32.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ test.suite("PathWin32JoinSuite", function(suite)
assert.eq(result.parts[2], "username")
assert.eq(result.parts[3], "Documents")
assert.eq(result.parts[4], "file.txt")
assert.eq(#pathobj.parts, 2) -- original pathobj should be unchanged
assert.eq(pathobj.parts[1], "Users")
assert.eq(pathobj.parts[2], "username")
assert.eq(pathobj.kind, "absolute")
assert.eq(pathobj.driveLetter, "C")
end)

suite:case("join_error_on_absolute_addend", function(assert)
Expand Down
68 changes: 68 additions & 0 deletions tests/requirebypath.test.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local fs = require("@lute/fs")
local process = require("@lute/process")
local path = require("@std/path")
local test = require("@std/test")

local REQUIRER_CONTENTS = [[
local args = { ... }
assert(#args == 2, "Expected one argument: path to Luau script to require")

local requireLib = require("@std/require")
local result = requireLib.requirebypath(args[2])
print(result)
]]

local REQUIREE_CONTENTS = [[return "Success"]]

local lutePath = process.execpath()

local tmpDirStr = ".tmp"
local tmpDirExists = fs.exists(tmpDirStr)
local testDir = path.join(tmpDirStr, "lute_require_test")
local testDirStr = path.format(testDir)

test.suite("Lute CLI", function(suite)
suite:beforeall(function()
if not tmpDirExists then
fs.mkdir(tmpDirStr)
end

if not fs.exists(testDirStr) then
fs.mkdir(testDirStr)
end
end)

suite:case("help1", function(check)
-- Setup
-- Create files
local requirerPath = path.format(path.join(testDir, "requirer.luau"))
local requirerFile = fs.open(requirerPath, "w+")
fs.write(requirerFile, REQUIRER_CONTENTS)
fs.close(requirerFile)

local requireePath = path.format(path.join(testDir, "requiree.luau"))
local requireeFile = fs.open(requireePath, "w+")
fs.write(requireeFile, REQUIREE_CONTENTS)
fs.close(requireeFile)

local result = process.run({ lutePath, requirerPath, requireePath })
check.eq(result.exitcode, 0)
check.eq(result.stdout, "Success\n")

-- Cleanup
fs.remove(requirerPath)
fs.remove(requireePath)
end)

suite:afterall(function()
if fs.exists(testDirStr) then
fs.rmdir(testDirStr)
end

if not tmpDirExists then
fs.rmdir(tmpDirStr)
end
end)
end)

test.run()
Loading