aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/util_spec.lua
blob: 8b234b2e168520bdd5ed1cfa01d1705e20100988 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
local test_env = require("spec.util.test_env")
local testing_paths = test_env.testing_paths
local P = test_env.P

local util = require("luarocks.util")
local core_util = require("luarocks.core.util")

describe("luarocks.util #unit", function()
   local runner

   lazy_setup(function()
      runner = require("luacov.runner")
      runner.init(testing_paths.testrun_dir .. "/luacov.config")
   end)

   lazy_teardown(function()
      runner.save_stats()
   end)

   describe("util.variable_substitutions", function()
      it("replaces variables", function()
         local t = {
            ["hello"] = "$(KIND) world",
         }
         util.variable_substitutions(t, {
            ["KIND"] = "happy",
         })
         assert.are.same({
            ["hello"] = "happy world",
         }, t)
      end)

      it("missing variables are empty", function()
         local t = {
            ["hello"] = "$(KIND) world",
         }
         util.variable_substitutions(t, {
         })
         assert.are.same({
            ["hello"] = " world",
         }, t)
      end)
   end)

   describe("util.sortedpairs", function()
      local function collect(iter, state, var)
         local collected = {}

         while true do
            local returns = {iter(state, var)}

            if returns[1] == nil then
               return collected
            else
               table.insert(collected, returns)
               var = returns[1]
            end
         end
      end

      it("default sort", function()
         assert.are.same({}, collect(util.sortedpairs({})))
         assert.are.same({
            {1, "v1"},
            {2, "v2"},
            {3, "v3"},
            {"bar", "v5"},
            {"foo", "v4"}
         }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"})))
      end)

      it("sort by function", function()
         local function compare(a, b) return a > b end
         assert.are.same({}, collect(util.sortedpairs({}, compare)))
         assert.are.same({
            {3, "v3"},
            {2, "v2"},
            {1, "v1"}
         }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare)))
      end)

      it("sort by priority table", function()
         assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"})))
         assert.are.same({
            {"k3", "v3"},
            {"k2", "v2", {"sub order"}},
            {"k1", "v1"},
            {"k4", "v4"},
            {"k5", "v5"},
         }, collect(util.sortedpairs({
            k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5"
         }, {"k3", {"k2", {"sub order"}}, "k1"})))
      end)
   end)

   describe("core.util.show_table", function()
      it("returns a pretty-printed string containing the representation of the given table", function()
         local result

         local t1 = {1, 2, 3}
         result = core_util.show_table(t1)
         assert.truthy(result:find("[1] = 1", 1, true))
         assert.truthy(result:find("[2] = 2", 1, true))
         assert.truthy(result:find("[3] = 3", 1, true))

         local t2 = {a = 1, b = 2, c = 3}
         result = core_util.show_table(t2)
         assert.truthy(result:find("[\"a\"] = 1", 1, true))
         assert.truthy(result:find("[\"b\"] = 2", 1, true))
         assert.truthy(result:find("[\"c\"] = 3", 1, true))

         local t3 = {a = 1, b = "2", c = {3}}
         result = core_util.show_table(t3)
         assert.truthy(result:find("[\"a\"] = 1", 1, true))
         assert.truthy(result:find("[\"b\"] = \"2\"", 1, true))
         assert.truthy(result:find("[\"c\"] = {", 1, true))
         assert.truthy(result:find("[1] = 3", 1, true))

         local t4 = {a = 1, b = {c = 2, d = {e = "4"}}}
         result = core_util.show_table(t4)
         assert.truthy(result:find("[\"a\"] = 1", 1, true))
         assert.truthy(result:find("[\"b\"] = {", 1, true))
         assert.truthy(result:find("[\"c\"] = 2", 1, true))
         assert.truthy(result:find("[\"d\"] = {", 1, true))
         assert.truthy(result:find("[\"e\"] = \"4\"", 1, true))
      end)
   end)

   describe("core.util.cleanup_path", function()
      it("does not change order of existing items of prepended path", function()
         local sys_path = P'/usr/local/bin;/usr/bin'
         local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
         local path = lr_path .. ';' .. sys_path

         local result = core_util.cleanup_path(path, ';', '5.3', false)
         assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result)
      end)

      it("does not change order of existing items of appended path", function()
         local sys_path = P'/usr/local/bin;/usr/bin'
         local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
         local path = sys_path .. ';' .. lr_path

         local result = core_util.cleanup_path(path, ';', '5.3', true)
         assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result)
      end)

      it("rewrites versions that do not match the provided version", function()
         local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua'
         local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3')
         assert.are.equal(expected, result)
      end)

      it("does not rewrite versions for which the provided version is a substring", function()
         local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua'
         local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3')
         assert.are.equal(expected, result)
      end)
   end)
end)