Skip to content

Commit

Permalink
[feat]: os module
Browse files Browse the repository at this point in the history
  • Loading branch information
castlele committed Jan 24, 2025
1 parent c87ead3 commit ae0661f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
11 changes: 10 additions & 1 deletion build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -32,4 +40,5 @@ conf = {
tableTest = runTestsCommand("table_utils*"),
hashmapTest = runTestsCommand("*hashmap*"),
linkedlistTest = runTestsCommand("linkedlist*"),
osTest = runTestsCommand("os*"),
}
24 changes: 15 additions & 9 deletions cluautils-1.18-1.rockspec → cluautils-1.19-0.rockspec
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -29,17 +29,23 @@ 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 },
},
["cluautils.thread"] = {
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 = {
Expand All @@ -51,5 +57,5 @@ build = {
copy_directories = {
"tests",
"examples",
}
},
}
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/os/os.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <string.h>

#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;
}

12 changes: 12 additions & 0 deletions tests/os_tests.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ae0661f

Please sign in to comment.