aboutsummaryrefslogtreecommitdiff
path: root/spec/03-environment_spec.lua
blob: 96f4a7882593b896437cd0522e787efd5bf0ce4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- Import the library that contains the environment-related functions
local system = require("system")
require("spec.helpers")

describe("Environment Variables:", function()

  describe("setenv()", function()

    it("should set an environment variable", function()
      assert.is_true(system.setenv("TEST_VAR", "test_value"))
      assert.is_equal("test_value", system.getenv("TEST_VAR"))
    end)


    -- Windows will unset a variable if set as an empty string
    nix_it("should set an empty environment variable value", function()
      assert.is_true(system.setenv("TEST_VAR", ""))
      assert.is_equal("", system.getenv("TEST_VAR"))
    end)


    it("should unset an environment variable on nil", function()
      assert.is_true(system.setenv("TEST_VAR", "test_value"))
      assert.is_equal("test_value", system.getenv("TEST_VAR"))

      assert.is_true(system.setenv("TEST_VAR", nil))
      assert.is_nil(system.getenv("TEST_VAR"))
    end)


    it("should error on input bad type", function()
      assert.has_error(function()
        system.setenv("TEST_VAR", {})
      end)
      assert.has_error(function()
        system.setenv({}, "test_value")
      end)
    end)


    it("should return success on deleting a variable that doesn't exist", function()
      if system.getenv("TEST_VAR") ~= nil then
        -- clear if it was already set
        assert.is_true(system.setenv("TEST_VAR", nil))
      end

      assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail
    end)

  end)



  describe("getenvs()", function()

    it("should list environment variables", function()
      assert.is_true(system.setenv("TEST_VAR1", nil))
      assert.is_true(system.setenv("TEST_VAR2", nil))
      assert.is_true(system.setenv("TEST_VAR3", nil))
      local envVars1 = system.getenvs()
      assert.is_true(system.setenv("TEST_VAR1", "test_value1"))
      assert.is_true(system.setenv("TEST_VAR2", "test_value2"))
      assert.is_true(system.setenv("TEST_VAR3", "test_value3"))
      local envVars2 = system.getenvs()
      assert.is_true(system.setenv("TEST_VAR1", nil))
      assert.is_true(system.setenv("TEST_VAR2", nil))
      assert.is_true(system.setenv("TEST_VAR3", nil))

      for k,v in pairs(envVars1) do
        envVars2[k] = nil
      end
      assert.are.same({
        TEST_VAR1 = "test_value1",
        TEST_VAR2 = "test_value2",
        TEST_VAR3 = "test_value3",
      }, envVars2)
    end)

  end)

end)