Remove pathWithExtension

This doesn't actually do anything: paths will never have a trailing
slash, so it's just equivalent to `x..".lua"`.
This commit is contained in:
Jonathan Coates
2026-04-29 11:42:56 +01:00
parent 83eddb01d8
commit abe3d3410f
2 changed files with 14 additions and 12 deletions
@@ -350,16 +350,6 @@ function shell.resolve(path)
end
end
local function pathWithExtension(_sPath, _sExt)
local nLen = #sPath
local sEndChar = string.sub(_sPath, nLen, nLen)
-- Remove any trailing slashes so we can add an extension to the path safely
if sEndChar == "/" or sEndChar == "\\" then
_sPath = string.sub(_sPath, 1, nLen - 1)
end
return _sPath .. "." .. _sExt
end
--- Resolve a program, using the [program path][`path`] and list of [aliases][`aliases`].
--
-- @tparam string command The name of the program
@@ -384,7 +374,7 @@ function shell.resolveProgram(command)
if fs.exists(sPath) and not fs.isDir(sPath) then
return sPath
else
local sPathLua = pathWithExtension(sPath, "lua")
local sPathLua = sPath .. ".lua"
if fs.exists(sPathLua) and not fs.isDir(sPathLua) then
return sPathLua
end
@@ -398,7 +388,7 @@ function shell.resolveProgram(command)
if fs.exists(sPath) and not fs.isDir(sPath) then
return sPath
else
local sPathLua = pathWithExtension(sPath, "lua")
local sPathLua = sPath .. ".lua"
if fs.exists(sPathLua) and not fs.isDir(sPathLua) then
return sPathLua
end
@@ -152,6 +152,18 @@ describe("The shell", function()
shell.resolveProgram("ls")
expect.error(shell.resolveProgram, nil):eq("bad argument #1 (string expected, got nil)")
end)
it("finds files on the shell path", function()
expect(shell.resolveProgram("edit")):eq("rom/programs/edit.lua")
expect(shell.resolveProgram("edit.lua")):eq("rom/programs/edit.lua")
end)
it("finds programs even with a trailing slash", function()
-- This feels silly: "edit/" is a directory, not the name of a file. However,
-- fs.combine (and CC's other path normalisation code) strips trailing slashes,
-- so this should still resolve the program.
expect(shell.resolveProgram("/rom/programs/edit/")):eq("rom/programs/edit.lua")
end)
end)
describe("shell.complete", function()