|  | 
|  | 1 | +local env = require("@env") | 
|  | 2 | + | 
|  | 3 | +testing:test("current_dir", function(t) | 
|  | 4 | +    local dir, err = env.current_dir() | 
|  | 5 | +    t.assert_eq(err, nil) | 
|  | 6 | +    t.assert(type(dir) == "string", "current_dir should return a string") | 
|  | 7 | +    t.assert(#dir > 0, "current_dir should not be empty") | 
|  | 8 | +end) | 
|  | 9 | + | 
|  | 10 | +testing:test("set_current_dir", function(t) | 
|  | 11 | +    local original_dir = env.current_dir() | 
|  | 12 | +    local parent_dir = original_dir .. "/.." | 
|  | 13 | +    local ok, err1 = env.set_current_dir(parent_dir) | 
|  | 14 | +    t.assert_eq(err1, nil) | 
|  | 15 | +    t.assert_eq(ok, true) | 
|  | 16 | + | 
|  | 17 | +    -- Verify the directory changed | 
|  | 18 | +    local new_dir, err2 = env.current_dir() | 
|  | 19 | +    t.assert_eq(err2, nil) | 
|  | 20 | +    t.assert(new_dir ~= original_dir, "directory should have changed") | 
|  | 21 | + | 
|  | 22 | +    -- Change back to original directory | 
|  | 23 | +    local _, err3 = env.set_current_dir(original_dir) | 
|  | 24 | +    t.assert_eq(err3, nil) | 
|  | 25 | + | 
|  | 26 | +    -- Test invalid directory | 
|  | 27 | +    local _, err5 = env.set_current_dir("/nonexistent/directory/path") | 
|  | 28 | +    t.assert(err5 ~= nil, "should fail for nonexistent directory") | 
|  | 29 | +    t.assert(type(err5) == "string", "error should be a string") | 
|  | 30 | +end) | 
|  | 31 | + | 
|  | 32 | +testing:test("current_exe", function(t) | 
|  | 33 | +    local exe, err = env.current_exe() | 
|  | 34 | +    t.assert_eq(err, nil) | 
|  | 35 | +    t.assert(type(exe) == "string", "current_exe should return a string") | 
|  | 36 | +    t.assert(#exe > 0, "current_exe should not be empty") | 
|  | 37 | +    -- The executable path should be a valid path (contains forward slash on Unix systems) | 
|  | 38 | +    t.assert(exe:match("/"), "executable should be a full path") | 
|  | 39 | +end) | 
|  | 40 | + | 
|  | 41 | +testing:test("home_dir", function(t) | 
|  | 42 | +    local home = env.home_dir() | 
|  | 43 | +    -- home_dir can return nil if home directory is not known | 
|  | 44 | +    if home ~= nil then | 
|  | 45 | +        t.assert(type(home) == "string", "home_dir should return a string when available") | 
|  | 46 | +        t.assert(#home > 0, "home_dir should not be empty when available") | 
|  | 47 | +    end | 
|  | 48 | +end) | 
|  | 49 | + | 
|  | 50 | +testing:test("var", function(t) | 
|  | 51 | +    -- Test getting a variable that likely doesn't exist | 
|  | 52 | +    local value = env.var("MLUA_STDLIB_NONEXISTENT_VAR") | 
|  | 53 | +    t.assert_eq(value, nil, "nonexistent variable should return nil") | 
|  | 54 | + | 
|  | 55 | +    -- Test getting PATH (should exist on most systems) | 
|  | 56 | +    local path = env.var("PATH") | 
|  | 57 | +    if path ~= nil then | 
|  | 58 | +        t.assert(type(path) == "string", "PATH should be a string") | 
|  | 59 | +        t.assert(#path > 0, "PATH should not be empty") | 
|  | 60 | +    end | 
|  | 61 | +end) | 
|  | 62 | + | 
|  | 63 | +testing:test("set_var", function(t) | 
|  | 64 | +    local test_key = "MLUA_STDLIB_TEST_VAR" | 
|  | 65 | +    local test_value = "test_value_123" | 
|  | 66 | + | 
|  | 67 | +    -- Ensure the variable doesn't exist initially | 
|  | 68 | +    local initial = env.var(test_key) | 
|  | 69 | +    t.assert_eq(initial, nil, "test variable should not exist initially") | 
|  | 70 | + | 
|  | 71 | +    -- Set the variable | 
|  | 72 | +    env.set_var(test_key, test_value) | 
|  | 73 | +    t.assert_eq(env.var(test_key), test_value, "variable should be set correctly") | 
|  | 74 | + | 
|  | 75 | +    -- Update the variable | 
|  | 76 | +    local new_value = "updated_value_456" | 
|  | 77 | +    env.set_var(test_key, new_value) | 
|  | 78 | +    t.assert_eq(env.var(test_key), new_value, "variable should be updated correctly") | 
|  | 79 | + | 
|  | 80 | +    -- Remove the variable | 
|  | 81 | +    env.set_var(test_key, nil) | 
|  | 82 | +    t.assert_eq(env.var(test_key), nil, "variable should be removed when set to nil") | 
|  | 83 | +end) | 
|  | 84 | + | 
|  | 85 | +testing:test("vars", function(t) | 
|  | 86 | +    local all_vars = env.vars() | 
|  | 87 | +    t.assert(type(all_vars) == "table", "vars should return a table") | 
|  | 88 | + | 
|  | 89 | +    -- Check that common environment variables exist | 
|  | 90 | +    local path = all_vars["PATH"] | 
|  | 91 | +    if path ~= nil then | 
|  | 92 | +        t.assert(type(path) == "string", "PATH in vars should be a string") | 
|  | 93 | +    end | 
|  | 94 | + | 
|  | 95 | +    -- Set a test variable and verify it appears in vars | 
|  | 96 | +    local test_key = "MLUA_STDLIB_TEST_VARS" | 
|  | 97 | +    local test_value = "test_vars_value" | 
|  | 98 | +    env.set_var(test_key, test_value) | 
|  | 99 | +    t.assert_eq(env.vars()[test_key], test_value, "test variable should appear in vars") | 
|  | 100 | + | 
|  | 101 | +    -- Clean up | 
|  | 102 | +    env.set_var(test_key, nil) | 
|  | 103 | +    t.assert_eq(env.vars()[test_key], nil, "test variable should be removed from vars") | 
|  | 104 | +end) | 
0 commit comments