Skip to content
Merged

test #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,50 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0

- name: Install Nix
if: runner.os != 'Windows'
uses: DeterminateSystems/nix-installer-action@main

- name: Run checks
if: runner.os != 'Windows'
run: nix flake check -Lvv --log-format bar-with-logs

- name: Set up windows msvc compiler
uses: step-security/msvc-dev-cmd@v1
- name: Setup Lua (only 1 version because windows)
if: runner.os == 'Windows'
uses: leafo/gh-actions-lua@v13
with:
luaVersion: "5.4"

- name: Install LuaRocks
if: runner.os == 'Windows'
shell: powershell
run: |
Invoke-WebRequest `
-Uri "https://luarocks.github.io/luarocks/releases/luarocks-3.13.0-windows-64.zip" `
-OutFile "luarocks.zip"

$dir = Join-Path $env:RUNNER_TEMP "luarocks"
New-Item -ItemType Directory -Force -Path "$dir"
Expand-Archive luarocks.zip -DestinationPath "$dir"
$root = Join-Path $dir "luarocks-3.13.0-windows-64"
$link = "./.lua/bin"
New-Item -ItemType SymbolicLink -Target "$root/luarocks.exe" -Path "$link/luarocks.exe"

- name: Build via luarocks
if: runner.os == 'Windows'
run: |
tree /f .
luarocks make --verbose
tree /f .
- name: Test via luarocks
if: runner.os == 'Windows'
run: luarocks test --verbose
32 changes: 29 additions & 3 deletions osenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <windows.h>
#endif

static void clear_process_env(void) {
static void clear_process_env(lua_State *L) {
#ifdef _WIN32
LPCH env = GetEnvironmentStringsA();
if (!env) return;
Expand All @@ -19,11 +19,17 @@ static void clear_process_env(void) {

size_t len = (size_t)(eq - p);

char key[len + 1];
char *key = (char *)malloc(len + 1);
if (!key) continue;
memcpy(key, p, len);
key[len] = '\0';

SetEnvironmentVariableA(key, NULL);
// sync CRT
if (_putenv_s(key, "") != 0)
luaL_error(L, "failed to sync CRT env (putenv)");

free(key);
}
FreeEnvironmentStringsA(env);
#else
Expand Down Expand Up @@ -57,6 +63,9 @@ static int env_unset(lua_State *L, const char *key) {
#ifdef _WIN32
if (!SetEnvironmentVariableA(key, NULL))
return luaL_error(L, "failed to unset env var");
// sync CRT
if (_putenv_s(key, "") != 0)
return luaL_error(L, "failed to sync CRT env (putenv)");
#else
if (unsetenv(key) != 0)
return luaL_error(L, "failed to unset env var");
Expand All @@ -73,6 +82,9 @@ static int env_set(lua_State *L, const char *key, const char *val, const int for
}
if (!SetEnvironmentVariableA(key, val))
return luaL_error(L, "failed to set env var");
// sync CRT
if (_putenv_s(key, val) != 0)
return luaL_error(L, "failed to sync CRT env (putenv)");
#else
if (setenv(key, val, force) != 0)
return luaL_error(L, "failed to set env var");
Expand Down Expand Up @@ -201,11 +213,25 @@ static int env__newindex(lua_State *L) {
static int env__index(lua_State *L) {
lua_settop(L, 2);
const char *key = luaL_checkstring(L, 2);
#ifdef _WIN32
DWORD size = GetEnvironmentVariableA(key, NULL, 0);
if (size == 0) {
lua_pushnil(L);
return 1;
}
char *buf = (char *)malloc(size);
if (!buf) return luaL_error(L, "out of memory");
GetEnvironmentVariableA(key, buf, size);
lua_pushstring(L, buf);
free(buf);
#else
const char *val = getenv(key);
if (val)
lua_pushstring(L, val);
else
lua_pushnil(L);
#endif

return 1;
}

Expand All @@ -221,7 +247,7 @@ static int env__call(lua_State *L) {
// env(table)
luaL_checktype(L, 2, LUA_TTABLE);

if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env();
if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env(L);

lua_pushnil(L);
while (lua_next(L, 2) != 0) {
Expand Down
18 changes: 9 additions & 9 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ end

---

test("osenv: environment clearing and restoration", function()
local osenv = require("osenv")
local oldenv = osenv()
ok(type(oldenv) == "table", "osenv() should return a table")
osenv({}, true)
ok(next(osenv()) == nil, "env should be empty after osenv({}, true)")
osenv(oldenv)
end)

test("osenv: get and set with various types", function()
local osenv = require("osenv")
local oldenv = osenv()
Expand Down Expand Up @@ -124,6 +115,15 @@ test("set if unset tests __call", function()
ok(os.getenv("TESTVARIABLE2") == nil and os.getenv("TESTVARIABLE") == nil, "both vars should be nil after restore")
end)

test("osenv: environment clearing and restoration", function()
local osenv = require("osenv")
local oldenv = osenv()
ok(type(oldenv) == "table", "osenv() should return a table")
osenv({}, true)
ok(next(osenv()) == nil, "env should be empty after osenv({}, true)")
osenv(oldenv)
end)

---

test.await(function(self)
Expand Down
Loading