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