Skip to content

Commit

Permalink
external commands should propagate exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Jul 11, 2024
1 parent 9954f55 commit 8750365
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
13 changes: 10 additions & 3 deletions src/ami/internals/exec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local exec = {}
---@field injectArgsAfter string[]?
---@field stdio ActionStdioType
---@field environment table<string, string>?
---@field shouldReturn boolean?

---@param destTable string[]
---@param toAppend string[]
Expand All @@ -33,10 +34,10 @@ local function _append_strings(destTable, toAppend)
end

---Executes external program with all arguments passed
---does not return on successful execution
---@param cmd string
---@param args CliArg[]
---@param options ExternalActionOptions
---@return integer
function exec.external_action(cmd, args, options)
local _args = {}
if type(options) ~= "table" then options = {} end
Expand Down Expand Up @@ -64,7 +65,10 @@ function exec.external_action(cmd, args, options)
end
local _ok, _result = proc.safe_exec(cmd .. " " .. execArgs)
ami_assert(_ok, "Failed to execute external action - " .. tostring(_result) .. "!")
return _result.exitcode
if options.shouldReturn then
return _result.exitcode
end
os.exit(_result.exitcode)
end

local desiredStdio = "inherit"
Expand All @@ -74,7 +78,10 @@ function exec.external_action(cmd, args, options)

local _ok, _result = proc.safe_spawn(cmd, _args, { wait = true, stdio = desiredStdio, env = options.environment })
ami_assert(_ok, "Failed to execute external action - " .. tostring(_result) .. "!")
return _result.exitcode
if options.shouldReturn then
return _result.exitcode
end
os.exit(_result.exitcode)
end

---@class ExecNativeActionOptions
Expand Down
2 changes: 1 addition & 1 deletion src/version-info.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local AM_VERSION = "0.27.1"
local AM_VERSION = "0.27.2"

return {
VERSION = AM_VERSION,
Expand Down
70 changes: 50 additions & 20 deletions tests/test/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ test["process cli (extension)"] = function()
end

test["process cli (external)"] = function()
local osExit = os.exit

local recordedExitCode = nil
os.exit = function(exitcode)
recordedExitCode = exitcode
end


local _cli = {
title = "test cli2",
description = "test cli description",
Expand All @@ -241,21 +249,21 @@ test["process cli (external)"] = function()

local _argListInit = _isUnixLike and { "test", "-c" } or { "test", "/c" }
local _argList = util.merge_arrays(_argListInit, { "exit 0" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 0)

local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 0)

local _argList = util.merge_arrays(_argListInit, { "exit 179" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 179)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 179)

proc.EPROC = false
local _argList = util.merge_arrays(_argListInit, { "exit 0" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 0)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 0)
local _argList = util.merge_arrays(_argListInit, { "exit 179" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 179)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 179)
proc.EPROC = true

_cli = {
Expand All @@ -266,6 +274,12 @@ test["process cli (external)"] = function()
exec = _isUnixLike and "sh" or "cmd",
description = "test cli test command",
type = "external"
},
test2 = {
exec = _isUnixLike and "sh" or "cmd",
description = "test cli test command",
type = "external",
shouldReturn = true
}
},
action = function(_, command, args, _)
Expand All @@ -277,26 +291,40 @@ test["process cli (external)"] = function()
end
}

local _argList = util.merge_arrays(_argListInit, { "exit 0" })
local _argListInit2 = _isUnixLike and { "test2", "-c" } or { "test2", "/c" }
local _argList = util.merge_arrays(_argListInit2, { "exit 0" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 0)

local _argList = util.merge_arrays(_argListInit, { "exit 0" })
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 0)

local _argList = util.merge_arrays(_argListInit, { "exit 179" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 179)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 179)

proc.EPROC = false
local _argList = util.merge_arrays(_argListInit, { "exit 0" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 0)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 0)

local _argList = util.merge_arrays(_argListInit, { "exit 179" })
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 179)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 179)
proc.EPROC = true

os.exit = osExit
end

test["process cli (external - custom env)"] = function()
local osExit = os.exit

local recordedExitCode = nil
os.exit = function(exitcode)
recordedExitCode = exitcode
end

local _cli = {
title = "test cli2",
description = "test cli description",
Expand All @@ -320,12 +348,14 @@ test["process cli (external - custom env)"] = function()
}

local _argList = _isUnixLike and { "test", "-c", "exit $EXIT_CODE" } or { "test", "/c", "exit %EXIT_CODE%" }
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 179)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 179)

_cli.commands.test.environment.EXIT_CODE = 175
local _ok, _result = pcall(am.execute, _cli, _argList)
test.assert(_ok and _result == 175)
local _ok = pcall(am.execute, _cli, _argList)
test.assert(_ok and recordedExitCode == 175)

os.exit = osExit
end

test["process cli (no-command)"] = function()
Expand Down

0 comments on commit 8750365

Please sign in to comment.