aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/util_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/util_spec.lua')
-rw-r--r--spec/unit/util_spec.lua161
1 files changed, 161 insertions, 0 deletions
diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua
new file mode 100644
index 00000000..0e380f2a
--- /dev/null
+++ b/spec/unit/util_spec.lua
@@ -0,0 +1,161 @@
1local test_env = require("spec.util.test_env")
2local testing_paths = test_env.testing_paths
3local P = test_env.P
4
5local util = require("luarocks.util")
6local core_util = require("luarocks.core.util")
7
8describe("luarocks.util #unit", function()
9 local runner
10
11 setup(function()
12 runner = require("luacov.runner")
13 runner.init(testing_paths.testrun_dir .. "/luacov.config")
14 runner.tick = true
15 end)
16
17 teardown(function()
18 runner.shutdown()
19 end)
20
21 describe("util.variable_substitutions", function()
22 it("replaces variables", function()
23 local t = {
24 ["hello"] = "$(KIND) world",
25 }
26 util.variable_substitutions(t, {
27 ["KIND"] = "happy",
28 })
29 assert.are.same({
30 ["hello"] = "happy world",
31 }, t)
32 end)
33
34 it("missing variables are empty", function()
35 local t = {
36 ["hello"] = "$(KIND) world",
37 }
38 util.variable_substitutions(t, {
39 })
40 assert.are.same({
41 ["hello"] = " world",
42 }, t)
43 end)
44 end)
45
46 describe("util.sortedpairs", function()
47 local function collect(iter, state, var)
48 local collected = {}
49
50 while true do
51 local returns = {iter(state, var)}
52
53 if returns[1] == nil then
54 return collected
55 else
56 table.insert(collected, returns)
57 var = returns[1]
58 end
59 end
60 end
61
62 it("default sort", function()
63 assert.are.same({}, collect(util.sortedpairs({})))
64 assert.are.same({
65 {1, "v1"},
66 {2, "v2"},
67 {3, "v3"},
68 {"bar", "v5"},
69 {"foo", "v4"}
70 }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"})))
71 end)
72
73 it("sort by function", function()
74 local function compare(a, b) return a > b end
75 assert.are.same({}, collect(util.sortedpairs({}, compare)))
76 assert.are.same({
77 {3, "v3"},
78 {2, "v2"},
79 {1, "v1"}
80 }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare)))
81 end)
82
83 it("sort by priority table", function()
84 assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"})))
85 assert.are.same({
86 {"k3", "v3"},
87 {"k2", "v2", {"sub order"}},
88 {"k1", "v1"},
89 {"k4", "v4"},
90 {"k5", "v5"},
91 }, collect(util.sortedpairs({
92 k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5"
93 }, {"k3", {"k2", {"sub order"}}, "k1"})))
94 end)
95 end)
96
97 describe("core.util.show_table", function()
98 it("returns a pretty-printed string containing the representation of the given table", function()
99 local result
100
101 local t1 = {1, 2, 3}
102 result = core_util.show_table(t1)
103 assert.truthy(result:find("[1] = 1", 1, true))
104 assert.truthy(result:find("[2] = 2", 1, true))
105 assert.truthy(result:find("[3] = 3", 1, true))
106
107 local t2 = {a = 1, b = 2, c = 3}
108 result = core_util.show_table(t2)
109 assert.truthy(result:find("[\"a\"] = 1", 1, true))
110 assert.truthy(result:find("[\"b\"] = 2", 1, true))
111 assert.truthy(result:find("[\"c\"] = 3", 1, true))
112
113 local t3 = {a = 1, b = "2", c = {3}}
114 result = core_util.show_table(t3)
115 assert.truthy(result:find("[\"a\"] = 1", 1, true))
116 assert.truthy(result:find("[\"b\"] = \"2\"", 1, true))
117 assert.truthy(result:find("[\"c\"] = {", 1, true))
118 assert.truthy(result:find("[1] = 3", 1, true))
119
120 local t4 = {a = 1, b = {c = 2, d = {e = "4"}}}
121 result = core_util.show_table(t4)
122 assert.truthy(result:find("[\"a\"] = 1", 1, true))
123 assert.truthy(result:find("[\"b\"] = {", 1, true))
124 assert.truthy(result:find("[\"c\"] = 2", 1, true))
125 assert.truthy(result:find("[\"d\"] = {", 1, true))
126 assert.truthy(result:find("[\"e\"] = \"4\"", 1, true))
127 end)
128 end)
129
130 describe("core.util.cleanup_path", function()
131 it("does not change order of existing items of prepended path", function()
132 local sys_path = P'/usr/local/bin;/usr/bin'
133 local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
134 local path = lr_path .. ';' .. sys_path
135
136 local result = core_util.cleanup_path(path, ';', '5.3', false)
137 assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result)
138 end)
139
140 it("does not change order of existing items of appended path", function()
141 local sys_path = P'/usr/local/bin;/usr/bin'
142 local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
143 local path = sys_path .. ';' .. lr_path
144
145 local result = core_util.cleanup_path(path, ';', '5.3', true)
146 assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result)
147 end)
148
149 it("rewrites versions that do not match the provided version", function()
150 local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua'
151 local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3')
152 assert.are.equal(expected, result)
153 end)
154
155 it("does not rewrite versions for which the provided version is a substring", function()
156 local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua'
157 local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3')
158 assert.are.equal(expected, result)
159 end)
160 end)
161end)