From d043da47e5d98e99e515edcdab951abe40be4681 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 29 Jun 2016 16:31:58 +0300 Subject: Convert statsfile and reportfile paths to absolute on config load If a program using LuaCov changes its directory, particularly if it uses `luacov.tick`, stats may be partially saved relatively to one of intermediate current directories instead of the starting one. The fix is to convert paths in config to absolute on load. `pwd` (`cd` on Windows) is used to get current directory, so, no dependency on lfs is introduced. Older versions of LuaCov opened statsfile on config load, which avoids this problem. --- src/luacov/runner.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/luacov/runner.lua b/src/luacov/runner.lua index ab02953..c5a2a00 100644 --- a/src/luacov/runner.lua +++ b/src/luacov/runner.lua @@ -151,6 +151,10 @@ end local dir_sep = package.config:sub(1, 1) local wildcard_expansion = "[^/]+" +if not dir_sep:find("[/\\]") then + dir_sep = "/" +end + local function escape_module_punctuation(ch) if ch == "." then return "/" @@ -287,6 +291,32 @@ local luacov_excludes = { "cluacov/version$" } +local function is_absolute(path) + if path:sub(1, 1) == dir_sep or path:sub(1, 1) == "/" then + return true + end + + if dir_sep == "\\" and path:find("^%a:") then + return true + end + + return false +end + +local function get_cur_dir() + local pwd_cmd = dir_sep == "\\" and "cd 2>nul" or "pwd 2>/dev/null" + local handler = io.popen(pwd_cmd, "r") + local cur_dir = handler:read("*a") + handler:close() + cur_dir = cur_dir:gsub("\r?\n$", "") + + if cur_dir:sub(-1) ~= dir_sep and cur_dir:sub(-1) ~= "/" then + cur_dir = cur_dir .. dir_sep + end + + return cur_dir +end + -- Sets configuration. If some options are missing, default values are used instead. local function set_config(configuration) runner.configuration = {} @@ -299,6 +329,19 @@ local function set_config(configuration) runner.configuration[option] = value end + -- Program using LuaCov may change directory during its execution. + -- Convert path options to absolute paths to use correct paths anyway. + local cur_dir + + for _, option in ipairs({"statsfile", "reportfile"}) do + local path = runner.configuration[option] + + if not is_absolute(path) then + cur_dir = cur_dir or get_cur_dir() + runner.configuration[option] = cur_dir .. path + end + end + acknowledge_modules() for _, patt in ipairs(luacov_excludes) do -- cgit v1.2.1