Rename lush Lua API: command→capture, subcmd→run, getenv→envget, setenv→envset
Lua-facing names only; C internals unchanged.
lush.capture(str) — return {code, stdout, stderr} table
lush.run(str) — return stripped stdout string
lush.interactive(str) — run with TTY, set _, return result
lush.envget(name) — get environment variable
lush.envset(name,val) — set environment variable
This commit is contained in:
@@ -13,30 +13,30 @@ end
|
||||
-- === named functions are accessible ===
|
||||
|
||||
do
|
||||
assert(type(lush.command) == "function", "lush.command missing")
|
||||
assert(type(lush.capture) == "function", "lush.capture missing")
|
||||
assert(type(lush.interactive) == "function", "lush.interactive missing")
|
||||
assert(type(lush.getenv) == "function", "lush.getenv missing")
|
||||
assert(type(lush.setenv) == "function", "lush.setenv missing")
|
||||
assert(type(lush.subcmd) == "function", "lush.subcmd missing")
|
||||
assert(type(lush.run) == "function", "lush.run missing")
|
||||
assert(type(lush.envget) == "function", "lush.envget missing")
|
||||
assert(type(lush.envset) == "function", "lush.envset missing")
|
||||
end
|
||||
|
||||
|
||||
-- === lush.command works like backtick ===
|
||||
-- === lush.capture works like backtick ===
|
||||
|
||||
do
|
||||
local r = lush.command("echo hello")
|
||||
assert(r.code == 0, "lush.command failed")
|
||||
local r = lush.capture("echo hello")
|
||||
assert(r.code == 0, "lush.capture failed")
|
||||
assert(r.stdout == "hello\n", "expected 'hello\\n', got: " .. r.stdout)
|
||||
end
|
||||
|
||||
|
||||
-- === lush.getenv / lush.setenv ===
|
||||
-- === lush.envget / lush.envset ===
|
||||
|
||||
do
|
||||
lush.setenv("_LUSH_TEST_VAR", "42")
|
||||
assert(lush.getenv("_LUSH_TEST_VAR") == "42")
|
||||
lush.setenv("_LUSH_TEST_VAR", nil)
|
||||
assert(lush.getenv("_LUSH_TEST_VAR") == nil)
|
||||
lush.envset("_LUSH_TEST_VAR", "42")
|
||||
assert(lush.envget("_LUSH_TEST_VAR") == "42")
|
||||
lush.envset("_LUSH_TEST_VAR", nil)
|
||||
assert(lush.envget("_LUSH_TEST_VAR") == nil)
|
||||
end
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ do
|
||||
end
|
||||
|
||||
|
||||
-- === user-defined builtin via lush.command ===
|
||||
-- === user-defined builtin via lush.capture ===
|
||||
|
||||
do
|
||||
lush.builtins.myecho = function(cmd, ...)
|
||||
@@ -84,7 +84,7 @@ do
|
||||
return t
|
||||
end
|
||||
|
||||
local r = lush.command("myecho foo bar")
|
||||
local r = lush.capture("myecho foo bar")
|
||||
assert(r.code == 0)
|
||||
assert(r.stdout == "foo bar\n",
|
||||
"expected 'foo bar\\n', got: " .. r.stdout)
|
||||
@@ -162,11 +162,11 @@ do
|
||||
end
|
||||
|
||||
|
||||
-- === alias works with lush.command ===
|
||||
-- === alias works with lush.capture ===
|
||||
|
||||
do
|
||||
lush.aliases.myecho = "echo via-command"
|
||||
local r = lush.command("myecho")
|
||||
local r = lush.capture("myecho")
|
||||
assert(r.code == 0)
|
||||
assert(r.stdout == "via-command\n",
|
||||
"expected 'via-command\\n', got: " .. r.stdout)
|
||||
@@ -182,10 +182,10 @@ do
|
||||
assert(r.stdout == "works\n")
|
||||
|
||||
-- $VAR expansion
|
||||
lush.setenv("_LUSH_LIB_TEST", "yes")
|
||||
lush.envset("_LUSH_LIB_TEST", "yes")
|
||||
local val = $_LUSH_LIB_TEST
|
||||
assert(val == "yes", "expected 'yes', got: " .. tostring(val))
|
||||
lush.setenv("_LUSH_LIB_TEST", nil)
|
||||
lush.envset("_LUSH_LIB_TEST", nil)
|
||||
end
|
||||
|
||||
|
||||
@@ -247,30 +247,32 @@ do
|
||||
assert(r.code == 127, "removed command should not be found")
|
||||
end
|
||||
|
||||
-- user command via lush.command()
|
||||
-- user command via lush.capture()
|
||||
do
|
||||
lush.commands.apicmd = function(name, ...)
|
||||
print("api " .. table.concat({...}, " "))
|
||||
end
|
||||
local r = lush.command("apicmd x y")
|
||||
local r = lush.capture("apicmd x y")
|
||||
assert(r.stdout == "api x y\n",
|
||||
"expected 'api x y\\n', got: " .. r.stdout)
|
||||
lush.commands.apicmd = nil
|
||||
end
|
||||
|
||||
|
||||
-- === lush.subcmd() direct call ===
|
||||
-- === lush.run() direct call ===
|
||||
|
||||
do
|
||||
local result = lush.subcmd("echo hello")
|
||||
assert(result == "hello", "subcmd should return stdout without trailing newline, got: " .. tostring(result))
|
||||
local result = lush.run("echo hello")
|
||||
assert(result == "hello", "run should return stdout without trailing newline, got: " .. tostring(result))
|
||||
end
|
||||
|
||||
|
||||
-- === lush.interactive() direct call ===
|
||||
|
||||
do
|
||||
lush.interactive("true")
|
||||
local r = lush.interactive("true")
|
||||
assert(type(r) == "table")
|
||||
assert(r.code == 0)
|
||||
assert(type(_) == "table")
|
||||
assert(_.code == 0)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user