Skip to content

Commit

Permalink
Merge pull request #14 from pallene-lang/spec-util
Browse files Browse the repository at this point in the history
Move util.lua to the specs directory
  • Loading branch information
hugomg authored Aug 9, 2024
2 parents 4829659 + 6a8c867 commit bbc022f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 6 additions & 5 deletions spec/tracebacks_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local util = require "misc.util"
local util = require "spec.util"

local function assert_test(example, expected_content)
local cdir = "spec/tracebacks/"..example.."/";
local ok, err = util.execute("cd "..cdir.."&& make")
local cdir = util.shell_quote("spec/tracebacks/"..example)
local ok, err = util.execute(string.format("cd %s && make", cdir))
assert(ok, err)

local ok, _, output_content, err_content = util.outputs_of_execute("cd "..cdir.." && pt-run main.lua")
local ok, _, output_content, err_content =
util.outputs_of_execute(string.format("cd %s && pt-run main.lua", cdir))
assert(not ok, output_content)
assert.are.same(expected_content, err_content)
end
Expand Down Expand Up @@ -82,7 +83,7 @@ Stack traceback:
]])
end)

it("Traceback Ellipsis", function()
it("Traceback Ellipsis", function()
assert_test("ellipsis", [[
Runtime error: C stack overflow
Stack traceback:
Expand Down
7 changes: 6 additions & 1 deletion src/misc/util.lua → spec/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ function util.get_file_contents(file_name)
end

-- Quotes a command-line argument according to POSIX shell syntax.
-- Uses a whitelist of safe chars to avoid quoting too much
function util.shell_quote(str)
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
if string.match(str, "^[%w./_-]+$") then
return str
else
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
end
end

function util.execute(cmd)
Expand Down

0 comments on commit bbc022f

Please sign in to comment.