diff options
| author | 2024-09-27 09:33:01 -0400 | |
|---|---|---|
| committer | 2024-09-27 09:33:01 -0400 | |
| commit | 68ce253ca289030922581b10a34094750c34d729 (patch) | |
| tree | 24a8d2a158794a0e42b5a154d3cf595188bd7227 | |
| parent | fixed chaining execution attempting to execute operator (diff) | |
added exit api function
| -rw-r--r-- | src/builtins.c | 4 | ||||
| -rw-r--r-- | src/lua_api.c | 20 | ||||
| -rw-r--r-- | src/lua_api.h | 2 | ||||
| -rw-r--r-- | test/args_test.lua | 2 | ||||
| -rw-r--r-- | test/chaining_test.lua | 8 | ||||
| -rw-r--r-- | test/env_test.lua | 2 | ||||
| -rw-r--r-- | test/filecheck_test.lua | 5 | ||||
| -rw-r--r-- | test/history_test.lua | 3 | ||||
| -rw-r--r-- | test/run_tests.lua | 1 |
9 files changed, 40 insertions, 7 deletions
diff --git a/src/builtins.c b/src/builtins.c index 6579790..835e7b9 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -136,9 +136,9 @@ int lush_lua(lua_State *L, char ***args) { // move args forward to any command line args args[0]++; - lua_load_script(L, script, args[0]); + int rc = lua_load_script(L, script, args[0]); // return pointer back to lua file args[0]--; - return 0; + return rc; } diff --git a/src/lua_api.c b/src/lua_api.c index cd21064..f726e44 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -34,7 +34,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. static bool debug_mode = false; // -- script execution -- -void lua_load_script(lua_State *L, const char *script, char **args) { +int lua_load_script(lua_State *L, const char *script, char **args) { char script_path[512]; // check if script is in the current directory if (access(script, F_OK) == 0) { @@ -48,12 +48,12 @@ void lua_load_script(lua_State *L, const char *script, char **args) { if (access(script_path, F_OK) != 0) { // script not in either location fprintf(stderr, "[C] Script not found: %s\n", script); - return; + return -1; } } else { // HOME not set fprintf(stderr, "[C] HOME directory is not set.\n"); - return; + return -1; } } // add args global if args were passed @@ -66,22 +66,27 @@ void lua_load_script(lua_State *L, const char *script, char **args) { } lua_setglobal(L, "args"); } + + int rc = 0; // if we got here the file exists if (luaL_loadfile(L, script_path) == LUA_OK) { if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) { const char *error_msg = lua_tostring(L, -1); fprintf(stderr, "[C] Error executing script: %s\n", error_msg); lua_pop(L, 1); // remove error from stack + rc = -1; } } else { const char *error_msg = lua_tostring(L, -1); fprintf(stderr, "[C] Error loading script: %s\n", error_msg); lua_pop(L, 1); // remove error from stack + rc = -1; } // reset args after running or just keep it nil lua_pushnil(L); lua_setglobal(L, "args"); + return rc; } void lua_run_init(lua_State *L) { @@ -93,6 +98,7 @@ void lua_run_init(lua_State *L) { lua_load_script(L, script_path, NULL); } } + // -- C funtions -- static int execute_command(lua_State *L, const char *line) { int status = 0; @@ -402,6 +408,12 @@ static int l_glob(lua_State *L) { return 1; } +static int l_exit(lua_State *L) { + lua_pushstring(L, "program terminated by lush exit"); + lua_error(L); + return 0; +} + // -- register Lua functions -- void lua_register_api(lua_State *L) { @@ -446,6 +458,8 @@ void lua_register_api(lua_State *L) { lua_setfield(L, -2, "termRows"); lua_pushcfunction(L, l_glob); lua_setfield(L, -2, "glob"); + lua_pushcfunction(L, l_exit); + lua_setfield(L, -2, "exit"); // set the table as global lua_setglobal(L, "lush"); } diff --git a/src/lua_api.h b/src/lua_api.h index 9027040..a27bfd3 100644 --- a/src/lua_api.h +++ b/src/lua_api.h @@ -20,7 +20,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include <lua.h> -void lua_load_script(lua_State *L, const char *script, char **args); +int lua_load_script(lua_State *L, const char *script, char **args); void lua_run_init(lua_State *L); void lua_register_api(lua_State *L); diff --git a/test/args_test.lua b/test/args_test.lua index d14fd73..a698b36 100644 --- a/test/args_test.lua +++ b/test/args_test.lua @@ -17,7 +17,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. if args == nil then print("args test failed, no args found ❌\n") - return + lush.exit() else print("args:") for i = 1, #args do diff --git a/test/chaining_test.lua b/test/chaining_test.lua index 83f3e76..411dcdd 100644 --- a/test/chaining_test.lua +++ b/test/chaining_test.lua @@ -15,48 +15,56 @@ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ]] +-- TODO: Add more edge case tests for chaining operators local cwd = lush.getcwd() lush.cd("~/.lush/scripts") if lush.exec('cat "example.lua" | grep "hello" | sort | uniq') then print("piping test passed ✅\n") else print("piping test failed ❌\n") + lush.exit() end if lush.exec("echo hi && echo bye") then print("and test passed ✅\n") else print("and test failed ❌\n") + lush.exit() end if lush.exec("cd lol || echo lol does not exist") then print("or test passed ✅\n") else print("or test failed ❌\n") + lush.exit() end if lush.exec("sleep 2 &") then print("background test passed ✅\n") else print("background test failed ❌\n") + lush.exit() end if lush.exec("echo hi; echo bye") then print("semicolon test passed ✅\n") else print("semicolon test failed ❌\n") + lush.exit() end if lush.exec("echo hi > test.txt") then print("redirect test passed ✅\n") else print("redirect test failed ❌\n") + lush.exit() end if lush.exec("echo hi >> test.txt") then print("append test passed ✅\n") else print("append test failed ❌\n") + lush.exit() end lush.cd(cwd) diff --git a/test/env_test.lua b/test/env_test.lua index 972a730..4a0d9d2 100644 --- a/test/env_test.lua +++ b/test/env_test.lua @@ -20,6 +20,7 @@ if lush.getenv("ENVTEST") == "envtest" then print("setenv test passed ✅\n") else print("setenv test failed ❌\n") + lush.exit() end lush.unsetenv("ENVTEST") @@ -27,4 +28,5 @@ if lush.getenv("ENVTEST") == nil then print("unsetenv test passed ✅\n") else print("unsetenv test failed ❌\n") + lush.exit() end diff --git a/test/filecheck_test.lua b/test/filecheck_test.lua index af0c130..9a2ffd7 100644 --- a/test/filecheck_test.lua +++ b/test/filecheck_test.lua @@ -19,28 +19,33 @@ if lush.exists("~/.lush/scripts/example.lua") then print("exists test passed ✅\n") else print("exists test failed ❌\n") + lush.exit() end if lush.isFile("~/.lush/scripts/example.lua") then print("isFile test passed ✅\n") else print("isFile test failed ❌\n") + lush.exit() end if not lush.isDirectory("~/.lush/scripts/example.lua") then print("isDirectory test passed ✅\n") else print("isDirectory test failed ❌\n") + lush.exit() end if lush.isReadable("~/.lush/scripts/example.lua") then print("isReadable test passed ✅\n") else print("isReadable test failed ❌\n") + lush.exit() end if lush.isWriteable("~/.lush/scripts/example.lua") then print("isWriteable test passed ✅\n") else print("isWriteable test failed ❌\n") + lush.exit() end diff --git a/test/history_test.lua b/test/history_test.lua index 69d8287..50be957 100644 --- a/test/history_test.lua +++ b/test/history_test.lua @@ -29,10 +29,13 @@ if lush.getHistory(1) == lush.lastHistory() then print("getHistory test passed ✅\n") else print("getHistory test failed at args history ❌\n") + lush.exit() end else print("getHistory test failed at piping history ❌\n") + lush.exit() end else print("getHistory test failed at lastHistory ❌\n") + lush.exit() end diff --git a/test/run_tests.lua b/test/run_tests.lua index 6c54157..39749d3 100644 --- a/test/run_tests.lua +++ b/test/run_tests.lua @@ -15,6 +15,7 @@ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ]] +-- TODO: Add API method for asserting command output equals some string print("Starting Lunar Shell End-to-End Testing...\n") print("Entering Debug Mode...") lush.debug(true) |
