aboutsummaryrefslogtreecommitdiff
path: root/spec/util_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util_spec.lua')
-rw-r--r--spec/util_spec.lua161
1 files changed, 0 insertions, 161 deletions
diff --git a/spec/util_spec.lua b/spec/util_spec.lua
index abc0da57..0f199c9c 100644
--- a/spec/util_spec.lua
+++ b/spec/util_spec.lua
@@ -1,8 +1,6 @@
1local test_env = require("spec.util.test_env") 1local test_env = require("spec.util.test_env")
2local lfs = require("lfs") 2local lfs = require("lfs")
3local run = test_env.run 3local run = test_env.run
4local testing_paths = test_env.testing_paths
5local P = test_env.P
6 4
7describe("Basic tests #integration", function() 5describe("Basic tests #integration", function()
8 6
@@ -55,162 +53,3 @@ describe("Basic tests #integration", function()
55 end) 53 end)
56 54
57end) 55end)
58
59test_env.unload_luarocks()
60local util = require("luarocks.util")
61local core_util = require("luarocks.core.util")
62
63describe("luarocks.util #unit", function()
64 local runner
65
66 setup(function()
67 runner = require("luacov.runner")
68 runner.init(testing_paths.testrun_dir .. "/luacov.config")
69 runner.tick = true
70 end)
71
72 teardown(function()
73 runner.shutdown()
74 end)
75
76 describe("util.variable_substitutions", function()
77 it("replaces variables", function()
78 local t = {
79 ["hello"] = "$(KIND) world",
80 }
81 util.variable_substitutions(t, {
82 ["KIND"] = "happy",
83 })
84 assert.are.same({
85 ["hello"] = "happy world",
86 }, t)
87 end)
88
89 it("missing variables are empty", function()
90 local t = {
91 ["hello"] = "$(KIND) world",
92 }
93 util.variable_substitutions(t, {
94 })
95 assert.are.same({
96 ["hello"] = " world",
97 }, t)
98 end)
99 end)
100
101 describe("util.sortedpairs", function()
102 local function collect(iter, state, var)
103 local collected = {}
104
105 while true do
106 local returns = {iter(state, var)}
107
108 if returns[1] == nil then
109 return collected
110 else
111 table.insert(collected, returns)
112 var = returns[1]
113 end
114 end
115 end
116
117 it("default sort", function()
118 assert.are.same({}, collect(util.sortedpairs({})))
119 assert.are.same({
120 {1, "v1"},
121 {2, "v2"},
122 {3, "v3"},
123 {"bar", "v5"},
124 {"foo", "v4"}
125 }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"})))
126 end)
127
128 it("sort by function", function()
129 local function compare(a, b) return a > b end
130 assert.are.same({}, collect(util.sortedpairs({}, compare)))
131 assert.are.same({
132 {3, "v3"},
133 {2, "v2"},
134 {1, "v1"}
135 }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare)))
136 end)
137
138 it("sort by priority table", function()
139 assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"})))
140 assert.are.same({
141 {"k3", "v3"},
142 {"k2", "v2", {"sub order"}},
143 {"k1", "v1"},
144 {"k4", "v4"},
145 {"k5", "v5"},
146 }, collect(util.sortedpairs({
147 k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5"
148 }, {"k3", {"k2", {"sub order"}}, "k1"})))
149 end)
150 end)
151
152 describe("core.util.show_table", function()
153 it("returns a pretty-printed string containing the representation of the given table", function()
154 local result
155
156 local t1 = {1, 2, 3}
157 result = core_util.show_table(t1)
158 assert.truthy(result:find("[1] = 1", 1, true))
159 assert.truthy(result:find("[2] = 2", 1, true))
160 assert.truthy(result:find("[3] = 3", 1, true))
161
162 local t2 = {a = 1, b = 2, c = 3}
163 result = core_util.show_table(t2)
164 assert.truthy(result:find("[\"a\"] = 1", 1, true))
165 assert.truthy(result:find("[\"b\"] = 2", 1, true))
166 assert.truthy(result:find("[\"c\"] = 3", 1, true))
167
168 local t3 = {a = 1, b = "2", c = {3}}
169 result = core_util.show_table(t3)
170 assert.truthy(result:find("[\"a\"] = 1", 1, true))
171 assert.truthy(result:find("[\"b\"] = \"2\"", 1, true))
172 assert.truthy(result:find("[\"c\"] = {", 1, true))
173 assert.truthy(result:find("[1] = 3", 1, true))
174
175 local t4 = {a = 1, b = {c = 2, d = {e = "4"}}}
176 result = core_util.show_table(t4)
177 assert.truthy(result:find("[\"a\"] = 1", 1, true))
178 assert.truthy(result:find("[\"b\"] = {", 1, true))
179 assert.truthy(result:find("[\"c\"] = 2", 1, true))
180 assert.truthy(result:find("[\"d\"] = {", 1, true))
181 assert.truthy(result:find("[\"e\"] = \"4\"", 1, true))
182 end)
183 end)
184
185 describe("core.util.cleanup_path", function()
186 it("does not change order of existing items of prepended path", function()
187 local sys_path = P'/usr/local/bin;/usr/bin'
188 local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
189 local path = lr_path .. ';' .. sys_path
190
191 local result = core_util.cleanup_path(path, ';', '5.3', false)
192 assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result)
193 end)
194
195 it("does not change order of existing items of appended path", function()
196 local sys_path = P'/usr/local/bin;/usr/bin'
197 local lr_path = P'/home/user/.luarocks/bin;/usr/bin'
198 local path = sys_path .. ';' .. lr_path
199
200 local result = core_util.cleanup_path(path, ';', '5.3', true)
201 assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result)
202 end)
203
204 it("rewrites versions that do not match the provided version", function()
205 local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua'
206 local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3')
207 assert.are.equal(expected, result)
208 end)
209
210 it("does not rewrite versions for which the provided version is a substring", function()
211 local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua'
212 local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3')
213 assert.are.equal(expected, result)
214 end)
215 end)
216end)