From 3f0f3028321154cbacecd6690833d0e8c805ccd8 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 28 Feb 2024 11:49:34 -0300 Subject: feat: more informative reports no bad LUA_{INC,LIB}DIR configs --- spec/cmd_spec.lua | 6 --- spec/quick/cmd.q | 36 ++++++++++++++++++ spec/quick/config.q | 90 ++++++++++++++++++++++++++++++++++++++++++--- spec/util/quick.lua | 2 +- spec/util/test_env.lua | 17 +++++---- src/luarocks/cmd.lua | 10 ++--- src/luarocks/cmd/config.lua | 86 +++++++++++++++++++++++++++++++++++++++++-- src/luarocks/deps.lua | 12 +++--- 8 files changed, 225 insertions(+), 34 deletions(-) create mode 100644 spec/quick/cmd.q diff --git a/spec/cmd_spec.lua b/spec/cmd_spec.lua index 9067a160..20248af7 100644 --- a/spec/cmd_spec.lua +++ b/spec/cmd_spec.lua @@ -56,12 +56,6 @@ describe("LuaRocks command line #integration", function() assert.match("malformed", output, 1, true) end) - it("warns but continues if given an invalid version", function() - local output = run.luarocks("--lua-version=1.0") - assert.match("LUA *: %(not found%)", output) - assert.match("Version%s*:%s*1.0", output) - end) - it("sets the version independently of project tree", function() test_env.run_in_tmp(function(tmpdir) assert.truthy(run.luarocks_bool("init --lua-version=" .. test_env.lua_version .. " --lua-versions=" .. test_env.lua_version)) diff --git a/spec/quick/cmd.q b/spec/quick/cmd.q new file mode 100644 index 00000000..acde92b4 --- /dev/null +++ b/spec/quick/cmd.q @@ -0,0 +1,36 @@ +SUITE: luarocks CLI + +================================================================================ +TEST: warns but continues if given an invalid version + +RUN: luarocks --lua-version 1.0 + +STDOUT: +-------------------------------------------------------------------------------- +Version : 1.0 +LUA : (interpreter not found) +-------------------------------------------------------------------------------- + + + +================================================================================ +TEST: reports if lua.h header is not found + +RUN: luarocks LUA_INCDIR=/bad/dir + +STDOUT: +-------------------------------------------------------------------------------- +LUA_INCDIR : /bad/dir (lua.h not found) +-------------------------------------------------------------------------------- + + + +================================================================================ +TEST: reports if Lua library is not found + +RUN: luarocks LUA_LIBDIR=/bad/dir + +STDOUT: +-------------------------------------------------------------------------------- +LUA_LIBDIR : /bad/dir (Lua library itself not found) +-------------------------------------------------------------------------------- diff --git a/spec/quick/config.q b/spec/quick/config.q index 3150a9ce..d6230567 100644 --- a/spec/quick/config.q +++ b/spec/quick/config.q @@ -1,13 +1,11 @@ -================================================================================ -TEST: luarocks config --system-config shows the path of the system config +SUITE: luarocks config -MKDIR: %{testing_lrprefix}/etc/luarocks +================================================================================ +TEST: --system-config shows the path of the system config FILE: %{testing_lrprefix}/etc/luarocks/config-%{LUA_VERSION}.lua -------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- - RUN: luarocks config --system-config STDOUT: @@ -15,3 +13,85 @@ STDOUT: %{path(%{testing_lrprefix}/etc/luarocks/config-%{LUA_VERSION}.lua)} -------------------------------------------------------------------------------- + + +================================================================================ +TEST: reports when setting a bad LUA_LIBDIR + +RUN: luarocks config variables.LUA_LIBDIR /some/bad/path + +LuaRocks writes configuration values as they are given, without auto-conversion +of slashes for Windows: + +STDOUT: +-------------------------------------------------------------------------------- +Wrote +variables.LUA_LIBDIR = "/some/bad/path" +-------------------------------------------------------------------------------- + +STDERR: +-------------------------------------------------------------------------------- +Warning: Failed finding the Lua library. +Tried: + +LuaRocks may not work correctly when building C modules using this configuration. +-------------------------------------------------------------------------------- + + + +================================================================================ +TEST: reports when setting a bad LUA_INCDIR + +RUN: luarocks config variables.LUA_INCDIR /some/bad/path + +STDOUT: +-------------------------------------------------------------------------------- +Wrote +variables.LUA_INCDIR = "/some/bad/path" +-------------------------------------------------------------------------------- + +LuaRocks uses configuration values as they are given, without auto-conversion +of slashes for Windows: + +STDERR: +-------------------------------------------------------------------------------- +Warning: Failed finding Lua header lua.h (searched at /some/bad/path). You may need to install Lua development headers. + +LuaRocks may not work correctly when building C modules using this configuration. +-------------------------------------------------------------------------------- + + + +================================================================================ +TEST: rejects setting bad lua_dir + +RUN: luarocks config lua_dir /some/bad/dir +EXIT: 1 + +STDERR: +-------------------------------------------------------------------------------- +Lua interpreter not found +-------------------------------------------------------------------------------- + + + +================================================================================ +TEST: reports when setting a bad LUA_INCDIR + +RUN: luarocks config variables.LUA_INCDIR /some/bad/path + +STDOUT: +-------------------------------------------------------------------------------- +Wrote +variables.LUA_INCDIR = "/some/bad/path" +-------------------------------------------------------------------------------- + +LuaRocks uses configuration values as they are given, without auto-conversion +of slashes for Windows: + +STDERR: +-------------------------------------------------------------------------------- +Warning: Failed finding Lua header lua.h (searched at /some/bad/path). You may need to install Lua development headers. + +LuaRocks may not work correctly when building C modules using this configuration. +-------------------------------------------------------------------------------- diff --git a/spec/util/quick.lua b/spec/util/quick.lua index cb4bb4cf..c313f575 100644 --- a/spec/util/quick.lua +++ b/spec/util/quick.lua @@ -347,7 +347,7 @@ function quick.compile(filename, env) write(([=[ ok, err = make_dir(%q) ]=]):format(op.file)) write(([=[ assert.truthy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file)) elseif op.op == "RUN" then - local cmd_helper = cmd_helpers[op.program] or op.program + local cmd_helper = cmd_helpers[op.program] or ("%q"):format(op.program) local redirs = " 1>stdout.txt 2>stderr.txt " write(([=[ local ok, _, code = os.execute(%s .. " " .. %q .. %q) ]=]):format(cmd_helper, op.args, redirs)) write([=[ if type(ok) == "number" then code = (ok >= 256 and ok / 256 or ok) end ]=]) diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 24a1de9e..8ccb494b 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -207,13 +207,16 @@ function test_env.run_in_tmp(f, finally) end fs.change_dir(tmpdir) - if finally then - finally(function() - lfs.chdir(olddir) - lfs.rmdir(tmpdir) - fs.change_dir(olddir) - end) - end + local lr_config = test_env.env_variables.LUAROCKS_CONFIG + + test_env.copy(lr_config, lr_config .. ".bak") + + finally(function() + test_env.copy(lr_config .. ".bak", lr_config) + lfs.chdir(olddir) + lfs.rmdir(tmpdir) + fs.change_dir(olddir) + end) f(tmpdir) end diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 643023b8..452237d2 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -337,8 +337,8 @@ local lua_example = package.config:sub(1, 1) == "\\" and "" or "" -local function show_status(file, status) - return (file and file .. " " or "") .. (status and "(ok)" or "(not found)") +local function show_status(file, status, err) + return (file and file .. " " or "") .. (status and "(ok)" or ("(" .. (err or "not found") ..")")) end local function use_to_fix_location(key, what) @@ -363,15 +363,15 @@ local function get_config_text(cfg) -- luacheck: ignore 431 if cfg.luajit_version then buf = buf.." LuaJIT : "..cfg.luajit_version.."\n" end - buf = buf.." LUA : "..show_status(cfg.variables.LUA, lua_ok).."\n" + buf = buf.." LUA : "..show_status(cfg.variables.LUA, lua_ok, "interpreter not found").."\n" if not lua_ok then buf = buf .. use_to_fix_location("variables.LUA", lua_example) end - buf = buf.." LUA_INCDIR : "..show_status(cfg.variables.LUA_INCDIR, incdir_ok).."\n" + buf = buf.." LUA_INCDIR : "..show_status(cfg.variables.LUA_INCDIR, incdir_ok, "lua.h not found").."\n" if lua_ok and not incdir_ok then buf = buf .. use_to_fix_location("variables.LUA_INCDIR") end - buf = buf.." LUA_LIBDIR : "..show_status(cfg.variables.LUA_LIBDIR, libdir_ok).."\n" + buf = buf.." LUA_LIBDIR : "..show_status(cfg.variables.LUA_LIBDIR, libdir_ok, "Lua library itself not found").."\n" if lua_ok and not libdir_ok then buf = buf .. use_to_fix_location("variables.LUA_LIBDIR") end diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index a01d33a9..26d60f09 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua @@ -243,11 +243,59 @@ local function get_scope(args) or "user" end +local function report_on_lua_incdir_config(value, lua_version) + local variables = { + ["LUA_DIR"] = cfg.variables.LUA_DIR, + ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, + ["LUA_INCDIR"] = value, + ["LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, + ["LUA"] = cfg.variables.LUA, + } + + local ok, err = deps.check_lua_incdir(variables, lua_version) + if not ok then + util.printerr() + util.warning((err:gsub(" You can use.*", ""))) + end + return ok +end + +local function report_on_lua_libdir_config(value, lua_version) + local variables = { + ["LUA_DIR"] = cfg.variables.LUA_DIR, + ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, + ["LUA_INCDIR"] = cfg.variables.LUA_INCDIR, + ["LUA_LIBDIR"] = value, + ["LUA"] = cfg.variables.LUA, + } + + local ok, err, _, err_files = deps.check_lua_libdir(variables, lua_version) + if not ok then + util.printerr() + util.warning((err:gsub(" You can use.*", ""))) + util.printerr("Tried:") + for _, l in pairs(err_files) do + for _, d in ipairs(l) do + util.printerr("\t" .. d) + end + end + end + return ok +end + +local function warn_bad_c_config() + util.printerr() + util.printerr("LuaRocks may not work correctly when building C modules using this configuration.") + util.printerr() +end + --- Driver function for "config" command. -- @return boolean: True if succeeded, nil on errors. function config_cmd.command(args) - deps.check_lua_incdir(cfg.variables, args.lua_version or cfg.lua_version) - deps.check_lua_libdir(cfg.variables, args.lua_version or cfg.lua_version) + local lua_version = args.lua_version or cfg.lua_version + + deps.check_lua_incdir(cfg.variables, lua_version) + deps.check_lua_libdir(cfg.variables, lua_version) -- deprecated flags if args.lua_incdir then @@ -312,13 +360,43 @@ function config_cmd.command(args) local prefix = dir.dir_name(cfg.config_files[scope].file) persist.save_default_lua_version(prefix, args.lua_version) end - return write_entries(keys, scope, args.unset) + local ok, err = write_entries(keys, scope, args.unset) + if ok then + local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR, lua_version) + local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR, lua_version) + if not (inc_ok and lib_ok) then + warn_bad_c_config() + end + end + + return ok, err end if args.key then + if args.key:match("^[A-Z]") then + args.key = "variables." .. args.key + end + if args.value or args.unset then local scope = get_scope(args) - return write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) + + local ok, err = write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) + + if ok then + if args.key == "variables.LUA_INCDIR" then + local ok = report_on_lua_incdir_config(args.value, lua_version) + if not ok then + warn_bad_c_config() + end + elseif args.key == "variables.LUA_LIBDIR" then + local ok = report_on_lua_libdir_config(args.value, lua_version) + if not ok then + warn_bad_c_config() + end + end + end + + return ok, err else return print_entry(args.key, cfg, args.json) end diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 16631c58..1cd500ca 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -703,10 +703,10 @@ local function lua_h_exists(d, luaver) if data:match("LUA_VERSION_NUM%s*" .. tostring(luanum)) then return d end - return nil, "Lua header found at " .. d .. " does not match Lua version " .. luaver .. ". You may want to override this by configuring LUA_INCDIR.", "dependency", 2 + return nil, "Lua header lua.h found at " .. d .. " does not match Lua version " .. luaver .. ". You can use `luarocks config variables.LUA_INCDIR ` to set the correct location.", "dependency", 2 end - return nil, "Failed finding Lua header files (searched at " .. d .. "). You may need to install them or configure LUA_INCDIR.", "dependency", 1 + return nil, "Failed finding Lua header lua.h (searched at " .. d .. "). You may need to install Lua development headers. You can use `luarocks config variables.LUA_INCDIR ` to set the correct location.", "dependency", 1 end local function find_lua_incdir(prefix, luaver, luajitver) @@ -793,7 +793,7 @@ function deps.check_lua_libdir(vars) end local cache = {} local save_LUA_INCDIR = vars.LUA_INCDIR - local ok = check_external_dependency("LUA", { library = libnames }, vars, "build", cache) + local ok, _, _, errfiles = check_external_dependency("LUA", { library = libnames }, vars, "build", cache) vars.LUA_INCDIR = save_LUA_INCDIR local err if ok then @@ -806,7 +806,7 @@ function deps.check_lua_libdir(vars) ok = txt:match("Lua " .. cfg.lua_version, 1, true) or txt:match("lua" .. (cfg.lua_version:gsub("%.", "")), 1, true) if not ok then - err = "Lua library at " .. filename .. " does not match Lua version " .. cfg.lua_version .. ". You may want to override this by configuring LUA_LIBDIR." + err = "Lua library at " .. filename .. " does not match Lua version " .. cfg.lua_version .. ". You can use `luarocks config variables.LUA_LIBDIR ` to set the correct location." end end @@ -819,8 +819,8 @@ function deps.check_lua_libdir(vars) vars.LUA_LIBDIR_OK = true return true else - err = err or "Failed finding Lua library. You may need to configure LUA_LIBDIR." - return nil, err, "dependency" + err = err or "Failed finding the Lua library. You can use `luarocks config variables.LUA_LIBDIR ` to set the correct location." + return nil, err, "dependency", errfiles end end -- cgit v1.2.3-55-g6feb