From ec78ef6b0f4dc866a803b4e6d23ad16db12934d6 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad <hisham@gobolinux.org> Date: Fri, 12 Jul 2019 17:38:00 -0300 Subject: which: search in package.path and cpath as well Also, add some tests. --- spec/which_spec.lua | 47 ++++++++++++++++++++++++++++++++++++++++++++++ src/luarocks/cmd/which.lua | 25 +++++++++++++++++++----- 2 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 spec/which_spec.lua 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 @@ +local test_env = require("spec.util.test_env") +local lfs = require("lfs") +local run = test_env.run +local testing_paths = test_env.testing_paths +local env_variables = test_env.env_variables +local write_file = test_env.write_file +local hardcoded + +test_env.unload_luarocks() + +local extra_rocks = { + "/say-1.2-1.src.rock", +} + +describe("LuaRocks which tests #integration", function() + + setup(function() + test_env.setup_specs(extra_rocks) + test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs + hardcoded = require("luarocks.core.hardcoded") + end) + + it("fails on missing arguments", function() + local output = run.luarocks("which") + assert.match("Missing module name", output, 1, true) + end) + + it("finds modules found in package.path", function() + assert.is_true(run.luarocks_bool("install say 1.2")) + local output = run.luarocks("which say") + assert.match("say/init.lua", output, 1, true) + assert.match("provided by say 1.2-1", output, 1, true) + end) + + it("finds modules found in package.path", function() + run.luarocks("install ") + local output = run.luarocks("which luarocks.loader") + assert.match("luarocks/loader.lua", output, 1, true) + assert.match("not installed as a rock", output, 1, true) + end) + + it("report modules not found", function() + local output = run.luarocks("which asdfgaoeui") + assert.match("Module 'asdfgaoeui' not found", output, 1, true) + end) + +end) 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 = {} local loader = require("luarocks.loader") local cfg = require("luarocks.core.cfg") local util = require("luarocks.util") +local fs = require("luarocks.fs") which_cmd.help_summary = "Tell which file corresponds to a given module name." which_cmd.help_arguments = "<modname>" @@ -21,12 +22,26 @@ function which_cmd.command(_, modname) return nil, "Missing module name. " .. util.see_help("which") end local pathname, rock_name, rock_version = loader.which(modname) - if not pathname then - return nil, "Module '" .. modname .. "' not found by luarocks.loader." + + if pathname then + util.printout(pathname) + util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") + return true + end + + local modpath = modname:gsub("%.", "/") + for _, v in ipairs({"path", "cpath"}) do + for p in package[v]:gmatch("([^;]+)") do + local pathname = p:gsub("%?", modpath) + if fs.exists(pathname) then + util.printout(pathname) + util.printout("(found directly via package." .. v .. " -- not installed as a rock?)") + return true + end + end end - util.printout(pathname) - util.printout("(provided by " .. tostring(rock_name) .. " " .. tostring(rock_version) .. ")") - return true + + return nil, "Module '" .. modname .. "' not found." end return which_cmd -- cgit v1.2.3-55-g6feb