aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2019-07-12 17:38:00 -0300
committerHisham Muhammad <hisham@gobolinux.org>2019-07-12 17:38:00 -0300
commitec78ef6b0f4dc866a803b4e6d23ad16db12934d6 (patch)
tree82ddf444389ea579d2fc5431e08893bd0624bbcf
parent6c7dfb00fa6d097319630fcc38123c30a34d1784 (diff)
downloadluarocks-ec78ef6b0f4dc866a803b4e6d23ad16db12934d6.tar.gz
luarocks-ec78ef6b0f4dc866a803b4e6d23ad16db12934d6.tar.bz2
luarocks-ec78ef6b0f4dc866a803b4e6d23ad16db12934d6.zip
which: search in package.path and cpath as well
Also, add some tests.
-rw-r--r--spec/which_spec.lua47
-rw-r--r--src/luarocks/cmd/which.lua25
2 files changed, 67 insertions, 5 deletions
diff --git a/spec/which_spec.lua b/spec/which_spec.lua
new file mode 100644
index 00000000..d5bcfab9
--- /dev/null
+++ b/spec/which_spec.lua
@@ -0,0 +1,47 @@
1local test_env = require("spec.util.test_env")
2local lfs = require("lfs")
3local run = test_env.run
4local testing_paths = test_env.testing_paths
5local env_variables = test_env.env_variables
6local write_file = test_env.write_file
7local hardcoded
8
9test_env.unload_luarocks()
10
11local extra_rocks = {
12 "/say-1.2-1.src.rock",
13}
14
15describe("LuaRocks which tests #integration", function()
16
17 setup(function()
18 test_env.setup_specs(extra_rocks)
19 test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs
20 hardcoded = require("luarocks.core.hardcoded")
21 end)
22
23 it("fails on missing arguments", function()
24 local output = run.luarocks("which")
25 assert.match("Missing module name", output, 1, true)
26 end)
27
28 it("finds modules found in package.path", function()
29 assert.is_true(run.luarocks_bool("install say 1.2"))
30 local output = run.luarocks("which say")
31 assert.match("say/init.lua", output, 1, true)
32 assert.match("provided by say 1.2-1", output, 1, true)
33 end)
34
35 it("finds modules found in package.path", function()
36 run.luarocks("install ")
37 local output = run.luarocks("which luarocks.loader")
38 assert.match("luarocks/loader.lua", output, 1, true)
39 assert.match("not installed as a rock", output, 1, true)
40 end)
41
42 it("report modules not found", function()
43 local output = run.luarocks("which asdfgaoeui")
44 assert.match("Module 'asdfgaoeui' not found", output, 1, true)
45 end)
46
47end)
diff --git a/src/luarocks/cmd/which.lua b/src/luarocks/cmd/which.lua
index 89e6db56..56db5254 100644
--- a/src/luarocks/cmd/which.lua
+++ b/src/luarocks/cmd/which.lua
@@ -6,6 +6,7 @@ local which_cmd = {}
6local loader = require("luarocks.loader") 6local loader = require("luarocks.loader")
7local cfg = require("luarocks.core.cfg") 7local cfg = require("luarocks.core.cfg")
8local util = require("luarocks.util") 8local util = require("luarocks.util")
9local fs = require("luarocks.fs")
9 10
10which_cmd.help_summary = "Tell which file corresponds to a given module name." 11which_cmd.help_summary = "Tell which file corresponds to a given module name."
11which_cmd.help_arguments = "<modname>" 12which_cmd.help_arguments = "<modname>"
@@ -21,12 +22,26 @@ function which_cmd.command(_, modname)
21 return nil, "Missing module name. " .. util.see_help("which") 22 return nil, "Missing module name. " .. util.see_help("which")
22 end 23 end
23 local pathname, rock_name, rock_version = loader.which(modname) 24 local pathname, rock_name, rock_version = loader.which(modname)
24 if not pathname then 25
25 return nil, "Module '" .. modname .. "' not found by luarocks.loader." 26 if pathname then
27 util.printout(pathname)
28 util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")")
29 return true
30 end
31
32 local modpath = modname:gsub("%.", "/")
33 for _, v in ipairs({"path", "cpath"}) do
34 for p in package[v]:gmatch("([^;]+)") do
35 local pathname = p:gsub("%?", modpath)
36 if fs.exists(pathname) then
37 util.printout(pathname)
38 util.printout("(found directly via package." .. v .. " -- not installed as a rock?)")
39 return true
40 end
41 end
26 end 42 end
27 util.printout(pathname) 43
28 util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") 44 return nil, "Module '" .. modname .. "' not found."
29 return true
30end 45end
31 46
32return which_cmd 47return which_cmd