diff options
| -rw-r--r-- | .lush/scripts/example.lua | 4 | ||||
| -rw-r--r-- | src/builtins.c | 50 | ||||
| -rw-r--r-- | src/lua_api.c | 6 | ||||
| -rw-r--r-- | test/chaining_test.lua | 24 | ||||
| -rw-r--r-- | test/history_test.lua | 6 | ||||
| -rw-r--r-- | test/run_tests.lua | 26 |
6 files changed, 102 insertions, 14 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua index b3de3cb..e7cd210 100644 --- a/.lush/scripts/example.lua +++ b/.lush/scripts/example.lua @@ -106,3 +106,7 @@ if textFiles ~= nil then print(textFiles[i]) end end + +-- the exit function is used to make the program quit in the case of an error +print("making an error with lush.exit()") +lush.exit() diff --git a/src/builtins.c b/src/builtins.c index 835e7b9..48bfa1c 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -93,11 +93,57 @@ int lush_help(lua_State *L, char ***args) { printf("Lunar Shell, version %s\n", LUSH_VERSION); #endif printf("These shell commands are defined internally. Type 'help' at any " - "time to reference this list.\n"); - printf("Available commands: \n"); + "time to reference this list.\n\n"); + printf("Available commands:\n"); for (int i = 0; i < lush_num_builtins(); i++) { printf("- %s %s\n", builtin_strs[i], builtin_usage[i]); } + + char *api_strs[] = {"exec(string command)", + "getcwd()", + "debug(boolean isOn)", + "cd(string path)", + "exists(string path)", + "isFile(string path)", + "isDir(string path)", + "isReadable(string path)", + "isWriteable(string path)", + "lastHistory()", + "getHistory(int index)", + "getenv(string envar)", + "setenv(string envar, string val)", + "unsetenv(string envar)", + "setPrompt(string prompt)", + "alias(string alias, string command)", + "termCols()", + "termRows()", + "glob(string extension)", + "exit()"}; + char *api_usage[] = { + "executes the command line chain given", + "gets current working directory", + "sets debug mode", + "changed current working directory to path given", + "checks if a file/directory exists at path", + "checks if given path is a file", + "checks if given path is a directory", + "checks if given path is readable", + "checks if given path is writeable", + "returns last history element", + "returns history at an index, 1 is most recent", + "returns value of an environment variable", + "sets the value of an environment variable", + "unsets the value of an environment variable", + "sets the prompt for the shell", + "sets an alias for a command", + "returns present number of columns in terminal", + "returns present number of rows in terminal", + "returns an array of filenames that have a given extension", + "ends the current process erroneously"}; + printf("\nLunar Shell Lua API:\n\n"); + for (int i = 0; i < sizeof(api_strs) / sizeof(char *); i++) { + printf("\033[1;32mlush.%s: \033[0m%s\n", api_strs[i], api_usage[i]); + } return 0; } diff --git a/src/lua_api.c b/src/lua_api.c index f726e44..e9473a0 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -110,7 +110,7 @@ static int execute_command(lua_State *L, const char *line) { if (status == -1) { fprintf(stderr, "lush: Expected end of quoted string\n"); } else if (lush_run(L, args, status) != 0) { - exit(1); + return -1; } for (int i = 0; args[i]; i++) { @@ -118,7 +118,7 @@ static int execute_command(lua_State *L, const char *line) { } free(args); free(commands); - return status; + return 0; } static char *get_expanded_path(const char *check_item) { @@ -155,7 +155,7 @@ static char *get_expanded_path(const char *check_item) { static int l_execute_command(lua_State *L) { const char *command = luaL_checkstring(L, 1); int status = execute_command(L, command); - bool rc = status != -1 ? true : false; + bool rc = status == 0 ? true : false; if (debug_mode) { if (rc) diff --git a/test/chaining_test.lua b/test/chaining_test.lua index 411dcdd..31ede87 100644 --- a/test/chaining_test.lua +++ b/test/chaining_test.lua @@ -15,7 +15,6 @@ 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 @@ -53,18 +52,37 @@ else lush.exit() end -if lush.exec("echo hi > test.txt") then +if + lush.exec( + "echo hi > test.txt; echo hi 1> test.txt; echo this wont redirect 2> test.txt; echo but this will &> 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 +if + lush.exec( + "echo hi >> test.txt; echo hi 1>> test.txt; echo this wont append 2>> test.txt; echo but this will &>> test.txt" + ) +then print("append test passed ✅\n") else print("append test failed ❌\n") lush.exit() end +if + lush.exec( + 'cat "example.lua" | grep "hello" | sort | uniq && echo hi >> test.txt; echo this should print && cd lol || echo lol doesnt exist &> test.txt' + ) +then + print("complex chain test passed ✅\n") +else + print("complex chain test failed ❌\n") + lush.exit() +end + lush.cd(cwd) diff --git a/test/history_test.lua b/test/history_test.lua index 50be957..621f22b 100644 --- a/test/history_test.lua +++ b/test/history_test.lua @@ -26,7 +26,11 @@ if lush.getHistory(1) == lush.lastHistory() then if lush.getHistory(9) == 'cat "example.lua" | grep "hello" | sort | uniq' then -- ensure args history is stored correctly if lush.getHistory(11) == "args_test.lua testarg1 testarg2 testarg3" then - print("getHistory test passed ✅\n") + if lush.getHistory(0) == nil and lush.getHistory(-1) == nil then + print("getHistory test passed ✅\n") + else + print("getHistory test failed at zero and negative history ❌\n") + end else print("getHistory test failed at args history ❌\n") lush.exit() diff --git a/test/run_tests.lua b/test/run_tests.lua index 39749d3..0f5c5bd 100644 --- a/test/run_tests.lua +++ b/test/run_tests.lua @@ -19,18 +19,34 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. print("Starting Lunar Shell End-to-End Testing...\n") print("Entering Debug Mode...") lush.debug(true) +local rc = true print("Testing Args...") -lush.exec("args_test.lua testarg1 testarg2 testarg3") +rc = lush.exec("args_test.lua testarg1 testarg2 testarg3") +if rc == false then + lush.exit() +end print("\nTesting Chaining...") -lush.exec("chaining_test.lua") +rc = lush.exec("chaining_test.lua") +if rc == false then + lush.exit() +end print("\nTesting File Checks...") -lush.exec("filecheck_test.lua") +rc = lush.exec("filecheck_test.lua") +if rc == false then + lush.exit() +end print("\nTesting History...") -lush.exec("history_test.lua") +rc = lush.exec("history_test.lua") +if rc == false then + lush.exit() +end print("\nTesting Environment Variables...") -lush.exec("env_test.lua") +rc = lush.exec("env_test.lua") +if rc == false then + lush.exit() +end |
