From 73874505c05821494b79272e4e092ba86e20c39e Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sun, 17 Apr 2016 15:16:30 +0300 Subject: Don't clear statsfile after loading from it Don't drop old stats if the program crashes without triggering exit hooks after loading. TODO: convert stats file name to absolute on config load so that changing directories doesn't break stats saving. --- src/luacov/runner.lua | 8 +------- src/luacov/stats.lua | 22 ++++------------------ 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/src/luacov/runner.lua b/src/luacov/runner.lua index 140f314..48d7ba7 100644 --- a/src/luacov/runner.lua +++ b/src/luacov/runner.lua @@ -21,7 +21,6 @@ local function on_exit_wrap(fn) end local data -local statsfile local tick local paused = true local initialized = false @@ -386,8 +385,7 @@ function runner.pause() end paused = true - stats.save(data, statsfile) - stats.stop(statsfile) + stats.save(data) -- Reset data, so that after resuming it could be added to data loaded -- from the stats file, possibly updated from another process. data = {} @@ -416,10 +414,6 @@ function runner.resume() data = loaded end - statsfile = stats.start() - runner.statsfile = statsfile - - if not tick then -- As __gc hooks are called in reverse order of their creation, -- and stats file has a __gc hook closing it, diff --git a/src/luacov/stats.lua b/src/luacov/stats.lua index 30a7a48..526d974 100644 --- a/src/luacov/stats.lua +++ b/src/luacov/stats.lua @@ -54,26 +54,12 @@ function stats.load() return data end --------------------------------- --- Opens the statfile --- @return filehandle -function stats.start() - return io.open(stats.statsfile, "w") -end - --------------------------------- --- Closes the statfile --- @param fd filehandle to the statsfile -function stats.stop(fd) - fd:close() -end - -------------------------------- -- Saves data to the statfile -- @param data data to store --- @param fd filehandle where to store -function stats.save(data, fd) - fd:seek("set") +function stats.save(data) + local fd = io.open(stats.statsfile, "w") + for filename, filedata in pairs(data) do local max = filedata.max fd:write(max, ":", filename, "\n") @@ -86,7 +72,7 @@ function stats.save(data, fd) end fd:write("\n") end - fd:flush() + fd:close() end return stats -- cgit v1.2.1