diff options
Diffstat (limited to 'spec/util_spec.lua')
-rw-r--r-- | spec/util_spec.lua | 111 |
1 files changed, 93 insertions, 18 deletions
diff --git a/spec/util_spec.lua b/spec/util_spec.lua index 23e3ebd8..2779b1ce 100644 --- a/spec/util_spec.lua +++ b/spec/util_spec.lua | |||
@@ -27,7 +27,7 @@ describe("Basic tests #blackbox #b_util", function() | |||
27 | assert.is_false(run.luarocks_bool("invalid=5")) | 27 | assert.is_false(run.luarocks_bool("invalid=5")) |
28 | end) | 28 | end) |
29 | 29 | ||
30 | it("LuaRocks execute from not existing directory", function() | 30 | it("LuaRocks execute from not existing directory #unix", function() |
31 | local main_path = lfs.currentdir() | 31 | local main_path = lfs.currentdir() |
32 | assert.is_true(lfs.mkdir("idontexist")) | 32 | assert.is_true(lfs.mkdir("idontexist")) |
33 | assert.is_true(lfs.chdir("idontexist")) | 33 | assert.is_true(lfs.chdir("idontexist")) |
@@ -66,34 +66,109 @@ describe("Basic tests #blackbox #b_util", function() | |||
66 | assert.is.truthy(lfs.attributes("src/luarocks/site_config.lua")) | 66 | assert.is.truthy(lfs.attributes("src/luarocks/site_config.lua")) |
67 | end) | 67 | end) |
68 | 68 | ||
69 | describe("LuaRocks sysconfig fails", function() | 69 | -- Disable versioned config temporarily, because it always takes |
70 | local scdir = "" | 70 | -- precedence over config.lua (config-5.x.lua is installed by default on Windows, |
71 | 71 | -- but not on Unix, so on Unix the os.rename commands below will fail silently, but this is harmless) | |
72 | before_each(function() | 72 | describe("LuaRocks config - more complex tests", function() |
73 | scdir = testing_paths.testing_lrprefix .. "/etc/luarocks/" | 73 | local scdir = testing_paths.testing_lrprefix .. "/etc/luarocks" |
74 | local versioned_scname = scdir .. "/config-" .. env_variables.LUA_VERSION .. ".lua" | ||
75 | local scname = scdir .. "/config.lua" | ||
76 | |||
77 | local configfile | ||
78 | if test_env.TEST_TARGET_OS == "windows" then | ||
79 | configfile = versioned_scname | ||
80 | else | ||
81 | configfile = scname | ||
82 | end | ||
83 | |||
84 | it("LuaRocks fail system config", function() | ||
85 | os.rename(versioned_scname, versioned_scname .. "bak") | ||
86 | local ok = run.luarocks_bool("config --system-config") | ||
87 | os.rename(versioned_scname .. ".bak", versioned_scname) | ||
88 | assert.is_false(ok) | ||
89 | end) | ||
90 | |||
91 | it("LuaRocks system config", function() | ||
74 | lfs.mkdir(testing_paths.testing_lrprefix) | 92 | lfs.mkdir(testing_paths.testing_lrprefix) |
75 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") | 93 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") |
76 | lfs.mkdir(scdir) | 94 | lfs.mkdir(scdir) |
95 | |||
96 | local sysconfig = io.open(configfile, "w+") | ||
97 | sysconfig:write(" ") | ||
98 | sysconfig:close() | ||
99 | |||
100 | local output = run.luarocks("config --system-config") | ||
101 | os.remove(configfile) | ||
102 | assert.are.same(output, configfile) | ||
77 | end) | 103 | end) |
78 | 104 | ||
79 | after_each(function() | 105 | it("LuaRocks fail system config invalid", function() |
80 | test_env.remove_dir(testing_paths.testing_lrprefix) | 106 | lfs.mkdir(testing_paths.testing_lrprefix) |
81 | end) | 107 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") |
108 | lfs.mkdir(scdir) | ||
82 | 109 | ||
83 | it("LuaRocks sysconfig fail", function() | 110 | local sysconfig = io.open(configfile, "w+") |
84 | local sysconfig = io.open(scdir .. "/config.lua", "w+") | 111 | sysconfig:write("if if if") |
85 | sysconfig:write("aoeui") | ||
86 | sysconfig:close() | 112 | sysconfig:close() |
113 | local ok = run.luarocks_bool("config --system-config") | ||
114 | os.remove(configfile) | ||
115 | assert.is_false(ok) | ||
116 | end) | ||
117 | end) | ||
118 | end) | ||
87 | 119 | ||
88 | assert.is_false(run.luarocks_bool("list")) | 120 | test_env.unload_luarocks() |
121 | local util = require("luarocks.util") | ||
122 | |||
123 | describe("Luarocks util test #whitebox #w_util", function() | ||
124 | describe("util.sortedpairs", function() | ||
125 | local function collect(iter, state, var) | ||
126 | local collected = {} | ||
127 | |||
128 | while true do | ||
129 | local returns = {iter(state, var)} | ||
130 | |||
131 | if returns[1] == nil then | ||
132 | return collected | ||
133 | else | ||
134 | table.insert(collected, returns) | ||
135 | var = returns[1] | ||
136 | end | ||
137 | end | ||
138 | end | ||
139 | |||
140 | it("default sort", function() | ||
141 | assert.are.same({}, collect(util.sortedpairs({}))) | ||
142 | assert.are.same({ | ||
143 | {1, "v1"}, | ||
144 | {2, "v2"}, | ||
145 | {3, "v3"}, | ||
146 | {"bar", "v5"}, | ||
147 | {"foo", "v4"} | ||
148 | }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"}))) | ||
89 | end) | 149 | end) |
90 | 150 | ||
91 | it("LuaRocks sysconfig fail", function() | 151 | it("sort by function", function() |
92 | local sysconfig = io.open(scdir .. "/config-" .. env_variables.LUA_VERSION .. ".lua", "w+") | 152 | local function compare(a, b) return a > b end |
93 | sysconfig:write("aoeui") | 153 | assert.are.same({}, collect(util.sortedpairs({}, compare))) |
94 | sysconfig:close() | 154 | assert.are.same({ |
155 | {3, "v3"}, | ||
156 | {2, "v2"}, | ||
157 | {1, "v1"} | ||
158 | }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare))) | ||
159 | end) | ||
95 | 160 | ||
96 | assert.is_false(run.luarocks_bool("list")) | 161 | it("sort by priority table", function() |
162 | assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"}))) | ||
163 | assert.are.same({ | ||
164 | {"k3", "v3"}, | ||
165 | {"k2", "v2", {"sub order"}}, | ||
166 | {"k1", "v1"}, | ||
167 | {"k4", "v4"}, | ||
168 | {"k5", "v5"}, | ||
169 | }, collect(util.sortedpairs({ | ||
170 | k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5" | ||
171 | }, {"k3", {"k2", {"sub order"}}, "k1"}))) | ||
97 | end) | 172 | end) |
98 | end) | 173 | end) |
99 | end) | 174 | end) |