From ae0661faac1e0f65f08139569bdf5a709c1b60d3 Mon Sep 17 00:00:00 2001 From: castlele Date: Fri, 24 Jan 2025 11:06:28 +0300 Subject: [PATCH] [feat]: os module --- build.lua | 11 ++++- ...18-1.rockspec => cluautils-1.19-0.rockspec | 24 ++++++---- run_tests.sh | 2 +- src/os/os.c | 46 +++++++++++++++++++ tests/os_tests.lua | 12 +++++ 5 files changed, 84 insertions(+), 11 deletions(-) rename cluautils-1.18-1.rockspec => cluautils-1.19-0.rockspec (79%) create mode 100644 src/os/os.c create mode 100644 tests/os_tests.lua diff --git a/build.lua b/build.lua index 809e674..38b2937 100644 --- a/build.lua +++ b/build.lua @@ -12,9 +12,17 @@ local function getCurrentFileName() return pathComponents[#pathComponents] end +local function install() + if require("cluautils.os").getName() == "MacOS" then + return "sudo luarocks make" + end + + return "luarocks make" +end + ---@diagnostic disable-next-line conf = { - install = "sudo luarocks make", + install = install(), remove = "sudo luarocks remove cluautils", currentTest = runTestsCommand(getCurrentFileName()), allTest = runTestsCommand("*.lua"), @@ -32,4 +40,5 @@ conf = { tableTest = runTestsCommand("table_utils*"), hashmapTest = runTestsCommand("*hashmap*"), linkedlistTest = runTestsCommand("linkedlist*"), + osTest = runTestsCommand("os*"), } diff --git a/cluautils-1.18-1.rockspec b/cluautils-1.19-0.rockspec similarity index 79% rename from cluautils-1.18-1.rockspec rename to cluautils-1.19-0.rockspec index 591fc15..828da4c 100644 --- a/cluautils-1.18-1.rockspec +++ b/cluautils-1.19-0.rockspec @@ -1,16 +1,16 @@ package = "cluautils" -version = "1.18-1" +version = "1.19-0" source = { - url = "git://github.com/castlele/cluautils.git", - tag = "1.18.1" + url = "git://github.com/castlele/cluautils.git", + tag = "1.19.0", } description = { homepage = "*** please enter a project homepage ***", - license = "MIT" + license = "MIT", } dependencies = { - "lua ~> 5.1", - "json-lua >= 0.1" + "lua ~> 5.1", + "json-lua >= 0.1", } local incDir = "./internal" @@ -29,9 +29,15 @@ build = { ["cluautils.table_utils"] = "src/table_utils/table_utils.lua", ["cluautils.functions"] = "src/functions/functions.lua", ["cluautils.cobject"] = "src/cobject.lua", + ["cluautils.os"] = { + sources = { + "./src/os/os.c", + }, + incdirs = { incDir }, + }, ["cluautils.memory"] = { sources = { - "./src/memory/memory.c" + "./src/memory/memory.c", }, incdirs = { incDir }, }, @@ -39,7 +45,7 @@ build = { sources = { "src/threads/thread.c", "src/threads/internal/cthread.c", - "src/threads/internal/queue.c" + "src/threads/internal/queue.c", }, libdirs = { "src/threads/bin/" }, incdirs = { @@ -51,5 +57,5 @@ build = { copy_directories = { "tests", "examples", - } + }, } diff --git a/run_tests.sh b/run_tests.sh index 5658915..0b507f2 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -4,7 +4,7 @@ tests_dir=./tests FILTER=$1 IS_ANY_ERROR=0 -for test_file in $(find "$tests_dir" -name "$FILTER" -type f -depth 1); do +for test_file in $(find "$tests_dir" -mindepth 1 -name "$FILTER" -type f); do lua $test_file if [[ $? -ne 0 ]]; then diff --git a/src/os/os.c b/src/os/os.c new file mode 100644 index 0000000..8b52058 --- /dev/null +++ b/src/os/os.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +#include "lualibutils.h" + +char *getOsName() +{ +#ifdef _WIN32 + return "Windows"; +#elif _WIN64 + return "Windows"; +#elif __APPLE__ || __MACH__ + return "MacOS"; +#elif __linux__ + return "Linux"; +#elif __FreeBSD__ + return "FreeBSD"; +#elif __unix || __unix__ + return "Unix"; +#else + return "Other"; +#endif +} + +static int luaGetOsName(lua_State *L) +{ + lua_pushstring(L, getOsName()); + + return 1; +} + +static const struct luaL_Reg luaOS[] = { + {"getName", luaGetOsName}, + {NULL, NULL}, +}; + +int luaopen_cluautils_os(lua_State *L) +{ + lua_createtable(L, 0, 1); + registerFields(L, luaOS); + + return 1; +} + diff --git a/tests/os_tests.lua b/tests/os_tests.lua new file mode 100644 index 0000000..eef416a --- /dev/null +++ b/tests/os_tests.lua @@ -0,0 +1,12 @@ +local t = require("src.tests") +local os = require("cluautils.os") +local strutils = require("src.string_utils.string_utils") + +t.describe("OS module tests", function () + t.it("os module has name function", function () + local name = os.getName() + + t.expect(type(name) == "string", "os type should be string") + t.expect(not strutils.isEmpty(name), "os type shouldn't be empty") + end) +end)