Skip to content

Commit d48a8f7

Browse files
authored
Merge pull request #18 from BirdeeHub/tests
test
2 parents 20e391e + 018a185 commit d48a8f7

3 files changed

Lines changed: 76 additions & 13 deletions

File tree

.github/workflows/nix-build.yml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,50 @@ jobs:
1010
test:
1111
strategy:
1212
matrix:
13-
os: [ubuntu-latest, macos-latest]
13+
os: [ubuntu-latest, macos-latest, windows-latest]
1414
runs-on: ${{ matrix.os }}
1515
steps:
1616
- uses: actions/checkout@v6
1717
with:
1818
fetch-depth: 0
19+
1920
- name: Install Nix
21+
if: runner.os != 'Windows'
2022
uses: DeterminateSystems/nix-installer-action@main
23+
2124
- name: Run checks
25+
if: runner.os != 'Windows'
2226
run: nix flake check -Lvv --log-format bar-with-logs
27+
28+
- name: Set up windows msvc compiler
29+
uses: step-security/msvc-dev-cmd@v1
30+
- name: Setup Lua (only 1 version because windows)
31+
if: runner.os == 'Windows'
32+
uses: leafo/gh-actions-lua@v13
33+
with:
34+
luaVersion: "5.4"
35+
36+
- name: Install LuaRocks
37+
if: runner.os == 'Windows'
38+
shell: powershell
39+
run: |
40+
Invoke-WebRequest `
41+
-Uri "https://luarocks.github.io/luarocks/releases/luarocks-3.13.0-windows-64.zip" `
42+
-OutFile "luarocks.zip"
43+
44+
$dir = Join-Path $env:RUNNER_TEMP "luarocks"
45+
New-Item -ItemType Directory -Force -Path "$dir"
46+
Expand-Archive luarocks.zip -DestinationPath "$dir"
47+
$root = Join-Path $dir "luarocks-3.13.0-windows-64"
48+
$link = "./.lua/bin"
49+
New-Item -ItemType SymbolicLink -Target "$root/luarocks.exe" -Path "$link/luarocks.exe"
50+
51+
- name: Build via luarocks
52+
if: runner.os == 'Windows'
53+
run: |
54+
tree /f .
55+
luarocks make --verbose
56+
tree /f .
57+
- name: Test via luarocks
58+
if: runner.os == 'Windows'
59+
run: luarocks test --verbose

osenv.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <windows.h>
77
#endif
88

9-
static void clear_process_env(void) {
9+
static void clear_process_env(lua_State *L) {
1010
#ifdef _WIN32
1111
LPCH env = GetEnvironmentStringsA();
1212
if (!env) return;
@@ -19,11 +19,17 @@ static void clear_process_env(void) {
1919

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

22-
char key[len + 1];
22+
char *key = (char *)malloc(len + 1);
23+
if (!key) continue;
2324
memcpy(key, p, len);
2425
key[len] = '\0';
2526

2627
SetEnvironmentVariableA(key, NULL);
28+
// sync CRT
29+
if (_putenv_s(key, "") != 0)
30+
luaL_error(L, "failed to sync CRT env (putenv)");
31+
32+
free(key);
2733
}
2834
FreeEnvironmentStringsA(env);
2935
#else
@@ -57,6 +63,9 @@ static int env_unset(lua_State *L, const char *key) {
5763
#ifdef _WIN32
5864
if (!SetEnvironmentVariableA(key, NULL))
5965
return luaL_error(L, "failed to unset env var");
66+
// sync CRT
67+
if (_putenv_s(key, "") != 0)
68+
return luaL_error(L, "failed to sync CRT env (putenv)");
6069
#else
6170
if (unsetenv(key) != 0)
6271
return luaL_error(L, "failed to unset env var");
@@ -73,6 +82,9 @@ static int env_set(lua_State *L, const char *key, const char *val, const int for
7382
}
7483
if (!SetEnvironmentVariableA(key, val))
7584
return luaL_error(L, "failed to set env var");
85+
// sync CRT
86+
if (_putenv_s(key, val) != 0)
87+
return luaL_error(L, "failed to sync CRT env (putenv)");
7688
#else
7789
if (setenv(key, val, force) != 0)
7890
return luaL_error(L, "failed to set env var");
@@ -201,11 +213,25 @@ static int env__newindex(lua_State *L) {
201213
static int env__index(lua_State *L) {
202214
lua_settop(L, 2);
203215
const char *key = luaL_checkstring(L, 2);
216+
#ifdef _WIN32
217+
DWORD size = GetEnvironmentVariableA(key, NULL, 0);
218+
if (size == 0) {
219+
lua_pushnil(L);
220+
return 1;
221+
}
222+
char *buf = (char *)malloc(size);
223+
if (!buf) return luaL_error(L, "out of memory");
224+
GetEnvironmentVariableA(key, buf, size);
225+
lua_pushstring(L, buf);
226+
free(buf);
227+
#else
204228
const char *val = getenv(key);
205229
if (val)
206230
lua_pushstring(L, val);
207231
else
208232
lua_pushnil(L);
233+
#endif
234+
209235
return 1;
210236
}
211237

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

224-
if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env();
250+
if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env(L);
225251

226252
lua_pushnil(L);
227253
while (lua_next(L, 2) != 0) {

test.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ end
3232

3333
---
3434

35-
test("osenv: environment clearing and restoration", function()
36-
local osenv = require("osenv")
37-
local oldenv = osenv()
38-
ok(type(oldenv) == "table", "osenv() should return a table")
39-
osenv({}, true)
40-
ok(next(osenv()) == nil, "env should be empty after osenv({}, true)")
41-
osenv(oldenv)
42-
end)
43-
4435
test("osenv: get and set with various types", function()
4536
local osenv = require("osenv")
4637
local oldenv = osenv()
@@ -124,6 +115,15 @@ test("set if unset tests __call", function()
124115
ok(os.getenv("TESTVARIABLE2") == nil and os.getenv("TESTVARIABLE") == nil, "both vars should be nil after restore")
125116
end)
126117

118+
test("osenv: environment clearing and restoration", function()
119+
local osenv = require("osenv")
120+
local oldenv = osenv()
121+
ok(type(oldenv) == "table", "osenv() should return a table")
122+
osenv({}, true)
123+
ok(next(osenv()) == nil, "env should be empty after osenv({}, true)")
124+
osenv(oldenv)
125+
end)
126+
127127
---
128128

129129
test.await(function(self)

0 commit comments

Comments
 (0)