diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2019-03-14 23:18:53 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-04-03 10:44:51 -0300 |
| commit | a21fcdc434f7135b3be215bbab5ad08f1873d300 (patch) | |
| tree | fba8bd24af0e34804954455acb567467d8586b13 /spec | |
| parent | 9c03508958522b2beeaa2aa289ad82e444d902d8 (diff) | |
| download | luarocks-a21fcdc434f7135b3be215bbab5ad08f1873d300.tar.gz luarocks-a21fcdc434f7135b3be215bbab5ad08f1873d300.tar.bz2 luarocks-a21fcdc434f7135b3be215bbab5ad08f1873d300.zip | |
config: add modes for reading, writing and unsetting individual entries
Includes special config keys `lua_dir` and `lua_version`,
which are essentially persistent versions of --lua-dir
and --lua-version:
* `lua_dir` writes a number of LUA_* variables and
cfg.lua_interpreter in the current scope's config file.
* `lua_version` writes default-lua-version.lua to the
given scope.
Also deprecates the "flag" versions for various getters:
* `--lua-incdir`: use `luarocks config variables.LUA_INCDIR`
* `--lua-libdir`: use `luarocks config variables.LUA_LIBDIR`
* `--lua-ver`: use `luarocks config lua_version`
* `--system-config`: use `luarocks config config_files.system.file`
* `--user-config`: use `luarocks config config_files.user.file`
* `--rock-trees`: use `luarocks config rocks_trees`
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/config_spec.lua | 241 |
1 files changed, 176 insertions, 65 deletions
diff --git a/spec/config_spec.lua b/spec/config_spec.lua index defc1d2f..2e76ae0c 100644 --- a/spec/config_spec.lua +++ b/spec/config_spec.lua | |||
| @@ -10,28 +10,58 @@ test_env.unload_luarocks() | |||
| 10 | 10 | ||
| 11 | describe("LuaRocks config tests #integration", function() | 11 | describe("LuaRocks config tests #integration", function() |
| 12 | 12 | ||
| 13 | before_each(function() | 13 | setup(function() |
| 14 | test_env.setup_specs() | 14 | test_env.setup_specs() |
| 15 | test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs | 15 | test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs |
| 16 | hardcoded = require("luarocks.core.hardcoded") | 16 | hardcoded = require("luarocks.core.hardcoded") |
| 17 | end) | 17 | end) |
| 18 | 18 | ||
| 19 | describe("LuaRocks config - basic tests", function() | 19 | describe("full configuration query", function() |
| 20 | it("LuaRocks config with no flags/arguments", function() | 20 | it("no flags/arguments", function() |
| 21 | assert.match("rocks_servers", run.luarocks("config")) | 21 | assert.match("rocks_servers = {", run.luarocks("config")) |
| 22 | end) | 22 | end) |
| 23 | 23 | ||
| 24 | it("LuaRocks config include dir returns a subdir of LUA_DIR", function() | 24 | it("--json", function() |
| 25 | assert.is_true(run.luarocks_nocov("install dkjson")) | ||
| 26 | finally(function() | ||
| 27 | assert.is_true(run.luarocks_nocov("remove dkjson")) | ||
| 28 | end) | ||
| 29 | assert.match('"rocks_servers":[', run.luarocks("config --json"), 1, true) | ||
| 30 | end) | ||
| 31 | |||
| 32 | it("--json fails without a json library", function() | ||
| 33 | assert.falsy(run.luarocks_bool("config --json")) | ||
| 34 | end) | ||
| 35 | |||
| 36 | it("with --tree respects custom config", function() | ||
| 37 | write_file("my_config.lua", [[ | ||
| 38 | rocks_trees = { | ||
| 39 | { | ||
| 40 | name = "system", | ||
| 41 | root = "/example/tree", | ||
| 42 | lua_dir = "/example/luadir", | ||
| 43 | }, | ||
| 44 | } | ||
| 45 | ]], finally) | ||
| 46 | local output = run.luarocks("config", {LUAROCKS_CONFIG = "my_config.lua"}) | ||
| 47 | assert.match([[deploy_lua_dir = "/example/luadir"]], output) | ||
| 48 | output = run.luarocks("config --tree=system", {LUAROCKS_CONFIG = "my_config.lua"}) | ||
| 49 | assert.match([[deploy_lua_dir = "/example/luadir"]], output) | ||
| 50 | end) | ||
| 51 | end) | ||
| 52 | |||
| 53 | describe("query flags", function() | ||
| 54 | it("--lua-incdir returns a subdir of LUA_DIR", function() | ||
| 25 | local output = run.luarocks("config --lua-incdir") | 55 | local output = run.luarocks("config --lua-incdir") |
| 26 | assert.match(hardcoded.LUA_DIR, output, 1, true) | 56 | assert.match(hardcoded.LUA_DIR, output, 1, true) |
| 27 | end) | 57 | end) |
| 28 | 58 | ||
| 29 | it("LuaRocks config library dir returns a subdir of LUA_DIR", function() | 59 | it("--lua-libdir returns a subdir of LUA_DIR", function() |
| 30 | local output = run.luarocks("config --lua-libdir") | 60 | local output = run.luarocks("config --lua-libdir") |
| 31 | assert.match(hardcoded.LUA_DIR, output, 1, true) | 61 | assert.match(hardcoded.LUA_DIR, output, 1, true) |
| 32 | end) | 62 | end) |
| 33 | 63 | ||
| 34 | it("LuaRocks config lua version", function() | 64 | it("--lua-ver returns the Lua version", function() |
| 35 | local output = run.luarocks("config --lua-ver") | 65 | local output = run.luarocks("config --lua-ver") |
| 36 | local lua_version = _VERSION:gsub("Lua ", "") | 66 | local lua_version = _VERSION:gsub("Lua ", "") |
| 37 | if test_env.LUAJIT_V then | 67 | if test_env.LUAJIT_V then |
| @@ -40,77 +70,158 @@ describe("LuaRocks config tests #integration", function() | |||
| 40 | assert.are.same(lua_version, output) | 70 | assert.are.same(lua_version, output) |
| 41 | end) | 71 | end) |
| 42 | 72 | ||
| 43 | it("LuaRocks config rock trees", function() | 73 | it("--rock-trees lists rock trees", function() |
| 44 | assert.is_true(run.luarocks_bool("config --rock-trees")) | 74 | assert.is_true(run.luarocks_bool("config --rock-trees")) |
| 45 | end) | 75 | end) |
| 46 | 76 | ||
| 47 | it("LuaRocks config user config", function() | 77 | describe("--user-config", function() |
| 48 | local user_config_path = run.luarocks("config --user-config") | 78 | it("returns user config dir", function() |
| 49 | assert.is.truthy(lfs.attributes(user_config_path)) | 79 | local user_config_path = run.luarocks("config --user-config") |
| 50 | end) | 80 | assert.is.truthy(lfs.attributes(user_config_path)) |
| 81 | end) | ||
| 51 | 82 | ||
| 52 | it("LuaRocks config missing user config", function() | 83 | it("handles a missing user config", function() |
| 53 | local output = run.luarocks("config --user-config", {LUAROCKS_CONFIG = "missing_file.lua"}) | 84 | local output = run.luarocks("config --user-config", {LUAROCKS_CONFIG = "missing_file.lua"}) |
| 54 | assert.truthy(output:match("Warning")) | 85 | assert.match("Warning", output) |
| 86 | end) | ||
| 55 | end) | 87 | end) |
| 56 | 88 | ||
| 57 | it("LuaRocks config with --tree respects custom config", function() | 89 | describe("--system-config", function() |
| 58 | write_file("my_config.lua", [[ | 90 | local scdir = testing_paths.testing_lrprefix .. "/etc/luarocks" |
| 59 | rocks_trees = { | 91 | local configfile = scdir .. "/config-" .. env_variables.LUA_VERSION .. ".lua" |
| 60 | { | 92 | |
| 61 | name = "system", | 93 | it("fails if system config doesn't exist", function() |
| 62 | root = "/example/tree", | 94 | os.rename(configfile, configfile .. ".bak") |
| 63 | lua_dir = "/example/luadir", | 95 | finally(function() |
| 64 | }, | 96 | os.rename(configfile .. ".bak", configfile) |
| 65 | } | 97 | end) |
| 66 | ]], finally) | 98 | assert.is_false(run.luarocks_bool("config --system-config")) |
| 67 | local output = run.luarocks("config", {LUAROCKS_CONFIG = "my_config.lua"}) | 99 | end) |
| 68 | assert.match([[deploy_lua_dir = "/example/luadir"]], output) | 100 | |
| 69 | output = run.luarocks("config --tree=system", {LUAROCKS_CONFIG = "my_config.lua"}) | 101 | it("outputs the path of the system config", function() |
| 70 | assert.match([[deploy_lua_dir = "/example/luadir"]], output) | 102 | lfs.mkdir(testing_paths.testing_lrprefix) |
| 103 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") | ||
| 104 | lfs.mkdir(scdir) | ||
| 105 | |||
| 106 | local sysconfig = io.open(configfile, "w+") | ||
| 107 | sysconfig:write(" ") | ||
| 108 | sysconfig:close() | ||
| 109 | finally(function() | ||
| 110 | os.remove(configfile) | ||
| 111 | end) | ||
| 112 | |||
| 113 | local output = run.luarocks("config --system-config") | ||
| 114 | assert.are.same(configfile, output) | ||
| 115 | end) | ||
| 116 | |||
| 117 | it("fails if system config is invalid", function() | ||
| 118 | lfs.mkdir(testing_paths.testing_lrprefix) | ||
| 119 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") | ||
| 120 | lfs.mkdir(scdir) | ||
| 121 | |||
| 122 | local sysconfig = io.open(configfile, "w+") | ||
| 123 | sysconfig:write("if if if") | ||
| 124 | sysconfig:close() | ||
| 125 | finally(function() | ||
| 126 | os.remove(configfile) | ||
| 127 | end) | ||
| 128 | assert.is_false(run.luarocks_bool("config --system-config")) | ||
| 129 | end) | ||
| 71 | end) | 130 | end) |
| 72 | end) | 131 | end) |
| 73 | 132 | ||
| 74 | describe("LuaRocks config - more complex tests", function() | 133 | describe("read config keys", function() |
| 75 | local scdir = testing_paths.testing_lrprefix .. "/etc/luarocks" | 134 | it("reads a simple config key", function() |
| 76 | local configfile = scdir .. "/config-" .. env_variables.LUA_VERSION .. ".lua" | 135 | local output = run.luarocks("config user_agent") |
| 136 | assert.match("LuaRocks/", output) | ||
| 137 | end) | ||
| 77 | 138 | ||
| 78 | it("LuaRocks fail system config", function() | 139 | it("reads an array config key", function() |
| 79 | os.rename(configfile, configfile .. ".bak") | 140 | local output = run.luarocks("config rocks_trees[2]") |
| 80 | finally(function() | 141 | assert.match("{%s*name", output) |
| 81 | os.rename(configfile .. ".bak", configfile) | ||
| 82 | end) | ||
| 83 | assert.is_false(run.luarocks_bool("config --system-config")) | ||
| 84 | end) | 142 | end) |
| 85 | 143 | ||
| 86 | it("LuaRocks system config", function() | 144 | it("can read as JSON", function() |
| 87 | lfs.mkdir(testing_paths.testing_lrprefix) | 145 | assert.is_true(run.luarocks_nocov("install dkjson")) |
| 88 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") | ||
| 89 | lfs.mkdir(scdir) | ||
| 90 | |||
| 91 | local sysconfig = io.open(configfile, "w+") | ||
| 92 | sysconfig:write(" ") | ||
| 93 | sysconfig:close() | ||
| 94 | finally(function() | 146 | finally(function() |
| 95 | os.remove(configfile) | 147 | assert.is_true(run.luarocks_nocov("remove dkjson")) |
| 96 | end) | 148 | end) |
| 97 | 149 | local output = run.luarocks("config rocks_trees --json") | |
| 98 | local output = run.luarocks("config --system-config") | 150 | assert.match('^%["', output) |
| 99 | assert.are.same(configfile, output) | ||
| 100 | end) | 151 | end) |
| 101 | 152 | ||
| 102 | it("LuaRocks fail system config invalid", function() | 153 | it("--json does not work without a json library", function() |
| 103 | lfs.mkdir(testing_paths.testing_lrprefix) | 154 | assert.is_false(run.luarocks_bool("config rocks_trees --json")) |
| 104 | lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/") | 155 | end) |
| 105 | lfs.mkdir(scdir) | 156 | |
| 106 | 157 | it("reads an array -> hash config key", function() | |
| 107 | local sysconfig = io.open(configfile, "w+") | 158 | local output = run.luarocks("config rocks_trees[2].name") |
| 108 | sysconfig:write("if if if") | 159 | assert.match("[a-z]+", output) |
| 109 | sysconfig:close() | ||
| 110 | finally(function() | ||
| 111 | os.remove(configfile) | ||
| 112 | end) | ||
| 113 | assert.is_false(run.luarocks_bool("config --system-config")) | ||
| 114 | end) | 160 | end) |
| 161 | |||
| 162 | it("reads a hash config key", function() | ||
| 163 | local output = run.luarocks("config variables.ICACLS") | ||
| 164 | assert.same("icacls", output) | ||
| 165 | end) | ||
| 166 | |||
| 167 | it("fails on invalid config key", function() | ||
| 168 | local output = run.luarocks("config xyz") | ||
| 169 | assert.match("Error: Unknown entry xyz", output) | ||
| 170 | end) | ||
| 171 | end) | ||
| 172 | |||
| 173 | describe("write config keys", function() | ||
| 174 | it("rejects invalid --scope", function() | ||
| 175 | assert.is_false(run.luarocks_bool("config web_browser foo --scope=foo")) | ||
| 176 | end) | ||
| 177 | |||
| 178 | it("reads an array config key", function() | ||
| 179 | local output = run.luarocks("config rocks_trees[2]") | ||
| 180 | assert.match("{%s*name", output) | ||
| 181 | end) | ||
| 182 | |||
| 183 | it("writes a simple config key", function() | ||
| 184 | test_env.run_in_tmp(function(tmpdir) | ||
| 185 | local myproject = tmpdir .. "/myproject" | ||
| 186 | lfs.mkdir(myproject) | ||
| 187 | lfs.chdir(myproject) | ||
| 188 | |||
| 189 | assert(run.luarocks("init")) | ||
| 190 | assert.truthy(run.luarocks_bool("config web_browser foo --scope=project")) | ||
| 191 | |||
| 192 | local output = run.luarocks("config web_browser") | ||
| 193 | assert.match("foo", output) | ||
| 194 | end, finally) | ||
| 195 | end) | ||
| 196 | |||
| 197 | it("writes a hash config key", function() | ||
| 198 | test_env.run_in_tmp(function(tmpdir) | ||
| 199 | local myproject = tmpdir .. "/myproject" | ||
| 200 | lfs.mkdir(myproject) | ||
| 201 | lfs.chdir(myproject) | ||
| 202 | |||
| 203 | assert(run.luarocks("init")) | ||
| 204 | assert.truthy(run.luarocks_bool("config variables.FOO_DIR /foo/bar --scope=project")) | ||
| 205 | |||
| 206 | local output = run.luarocks("config variables.FOO_DIR") | ||
| 207 | assert.match("/foo/bar", output) | ||
| 208 | end, finally) | ||
| 209 | end) | ||
| 210 | |||
| 211 | it("writes an array config key", function() | ||
| 212 | test_env.run_in_tmp(function(tmpdir) | ||
| 213 | local myproject = tmpdir .. "/myproject" | ||
| 214 | lfs.mkdir(myproject) | ||
| 215 | lfs.chdir(myproject) | ||
| 216 | |||
| 217 | assert(run.luarocks("init")) | ||
| 218 | assert.truthy(run.luarocks_bool("config external_deps_patterns.lib[1] testtest --scope=project")) | ||
| 219 | |||
| 220 | local output = run.luarocks("config external_deps_patterns.lib[1]") | ||
| 221 | assert.match("testtest", output) | ||
| 222 | end, finally) | ||
| 223 | end) | ||
| 224 | |||
| 115 | end) | 225 | end) |
| 226 | |||
| 116 | end) | 227 | end) |
