diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-09 23:03:21 +0100 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-15 19:17:57 +0100 |
| commit | d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2 (patch) | |
| tree | 2d4f86ec87eb87a77f6663924aaaa9286756ce3e /spec/03-environment_spec.lua | |
| parent | d4222ce6da2a2d7179fc79f9d0cc65fd6c09a686 (diff) | |
| download | luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.gz luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.bz2 luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.zip | |
feat(*): add environment variable and random functions
Diffstat (limited to 'spec/03-environment_spec.lua')
| -rw-r--r-- | spec/03-environment_spec.lua | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/spec/03-environment_spec.lua b/spec/03-environment_spec.lua new file mode 100644 index 0000000..842ed6f --- /dev/null +++ b/spec/03-environment_spec.lua | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | -- Import the library that contains the environment-related functions | ||
| 2 | local system = require("system") | ||
| 3 | |||
| 4 | describe("Environment Variables:", function() | ||
| 5 | |||
| 6 | describe("setenv()", function() | ||
| 7 | |||
| 8 | it("should set an environment variable", function() | ||
| 9 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
| 10 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
| 11 | end) | ||
| 12 | |||
| 13 | |||
| 14 | local func = system.windows and pending or it --pending on Windows | ||
| 15 | -- Windows will unset a variable if set as an empty string | ||
| 16 | func("should set an empty environment variable value", function() | ||
| 17 | assert.is_true(system.setenv("TEST_VAR", "")) | ||
| 18 | assert.is_equal("", system.getenv("TEST_VAR")) | ||
| 19 | end) | ||
| 20 | |||
| 21 | |||
| 22 | it("should unset an environment variable on nil", function() | ||
| 23 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
| 24 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
| 25 | |||
| 26 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
| 27 | assert.is_nil(system.getenv("TEST_VAR")) | ||
| 28 | end) | ||
| 29 | |||
| 30 | |||
| 31 | it("should error on input bad type", function() | ||
| 32 | assert.has_error(function() | ||
| 33 | system.setenv("TEST_VAR", {}) | ||
| 34 | end) | ||
| 35 | assert.has_error(function() | ||
| 36 | system.setenv({}, "test_value") | ||
| 37 | end) | ||
| 38 | end) | ||
| 39 | |||
| 40 | |||
| 41 | it("should return success on deleting a variable that doesn't exist", function() | ||
| 42 | if system.getenv("TEST_VAR") ~= nil then | ||
| 43 | -- clear if it was already set | ||
| 44 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
| 45 | end | ||
| 46 | |||
| 47 | assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail | ||
| 48 | end) | ||
| 49 | |||
| 50 | end) | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | describe("getenvs()", function() | ||
| 55 | |||
| 56 | it("should list environment variables", function() | ||
| 57 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
| 58 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
| 59 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
| 60 | local envVars1 = system.getenvs() | ||
| 61 | assert.is_true(system.setenv("TEST_VAR1", "test_value1")) | ||
| 62 | assert.is_true(system.setenv("TEST_VAR2", "test_value2")) | ||
| 63 | assert.is_true(system.setenv("TEST_VAR3", "test_value3")) | ||
| 64 | local envVars2 = system.getenvs() | ||
| 65 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
| 66 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
| 67 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
| 68 | |||
| 69 | for k,v in pairs(envVars1) do | ||
| 70 | envVars2[k] = nil | ||
| 71 | end | ||
| 72 | assert.are.same({ | ||
| 73 | TEST_VAR1 = "test_value1", | ||
| 74 | TEST_VAR2 = "test_value2", | ||
| 75 | TEST_VAR3 = "test_value3", | ||
| 76 | }, envVars2) | ||
| 77 | end) | ||
| 78 | |||
| 79 | end) | ||
| 80 | |||
| 81 | end) | ||
