summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Mascarenhas <mascarenhas@gmail.com>2011-10-01 11:53:55 -0700
committerFabio Mascarenhas <mascarenhas@gmail.com>2011-10-01 11:53:55 -0700
commit875148a62871a55df95f80bdaa999d3fbdea3c90 (patch)
tree00dfc3d47a07573a8284242ff1bf277858b75afc
parentb641b3f51c074a959ef0395452e33a185c255c69 (diff)
parent0420c607c406804f86fe23c5c0cb4def29519d7a (diff)
downloadluacov-875148a62871a55df95f80bdaa999d3fbdea3c90.tar.gz
Merge pull request #1 from akavel/master
Improved code locality + disabled collecting coverage of luacov itself.
-rwxr-xr-xsrc/bin/luacov2
-rw-r--r--src/luacov.lua122
-rw-r--r--src/luacov/stats.lua46
3 files changed, 99 insertions, 71 deletions
diff --git a/src/bin/luacov b/src/bin/luacov
index 5eb912d..1a22947 100755
--- a/src/bin/luacov
+++ b/src/bin/luacov
@@ -2,7 +2,7 @@
local luacov = require("luacov.stats")
-local data, most_hits = luacov.load_stats()
+local data, most_hits = luacov.load()
if not data then
print("Could not load stats file "..luacov.statsfile..".")
diff --git a/src/luacov.lua b/src/luacov.lua
index a2334e3..a44333a 100644
--- a/src/luacov.lua
+++ b/src/luacov.lua
@@ -1,83 +1,95 @@
-module("luacov", package.seeall)
+local M = {}
local stats = require("luacov.stats")
+local data = stats.load()
+local statsfile = stats.start()
+M.statsfile = statsfile
-data = stats.load_stats()
-
-statsfile = stats.start_stats()
+local tick = package.loaded["luacov.tick"]
+local ctr = 0
+local luacovlock = os.tmpname()
-tick = package.loaded["luacov.tick"]
-ctr = 0
+local booting = true
+local skip = {}
+M.skip = skip
local function on_line(_, line_nr)
if tick then
ctr = ctr + 1
if ctr == 100 then
ctr = 0
- stats.save_stats(data, statsfile)
+ stats.save(data, statsfile)
end
end
+ -- get name of processed file; ignore Lua code loaded from raw strings
local name = debug.getinfo(2, "S").source
- if name:match("^@") then
- name = name:sub(2)
- local file = data[name]
- if not file then
- file = {}
- file.max = 0
- data[name] = file
- end
- if line_nr > file.max then
- file.max = line_nr
- end
- local current = file[line_nr]
- if not current then
- file[line_nr] = 1
- else
- file[line_nr] = current + 1
- end
+ if not name:match("^@") then
+ return
end
-end
+ name = name:sub(2)
-local luacovlock = os.tmpname()
+ -- skip 'luacov.lua' in coverage report
+ if booting then
+ skip[name] = true
+ booting = false
+ end
-function on_exit()
- os.remove(luacovlock)
- stats.save_stats(data, statsfile)
- stats.stop_stats(statsfile)
+ if skip[name] then
+ return
+ end
+
+ local file = data[name]
+ if not file then
+ file = {max=0}
+ data[name] = file
+ end
+ if line_nr > file.max then
+ file.max = line_nr
+ end
+ file[line_nr] = (file[line_nr] or 0) + 1
end
-if not tick then
- on_exit_trick = io.open(luacovlock, "w")
- debug.setmetatable(on_exit_trick, { __gc = on_exit } )
+local function on_exit()
+ os.remove(luacovlock)
+ stats.save(data, statsfile)
+ stats.stop(statsfile)
end
-debug.sethook(on_line, "l")
+local function init()
+ if not tick then
+ M.on_exit_trick = io.open(luacovlock, "w")
+ debug.setmetatable(M.on_exit_trick, { __gc = on_exit } )
+ end
-rawcoroutinecreate = coroutine.create
+ debug.sethook(on_line, "l")
-function coroutine.create(...)
- local co = rawcoroutinecreate(...)
- debug.sethook(co, on_line, "l")
- return co
-end
+ local rawcoroutinecreate = coroutine.create
+ coroutine.create = function(...)
+ local co = rawcoroutinecreate(...)
+ debug.sethook(co, on_line, "l")
+ return co
+ end
+ coroutine.wrap = function(...)
+ local co = rawcoroutinecreate(...)
+ debug.sethook(co, on_line, "l")
+ return function()
+ local r = { coroutine.resume(co) }
+ if not r[1] then
+ error(r[2])
+ end
+ return unpack(r, 2)
+ end
+ end
-function coroutine.wrap(...)
- local co = rawcoroutinecreate(...)
- debug.sethook(co, on_line, "l")
- return function()
- local r = { coroutine.resume(co) }
- if not r[1] then
- error(r[2])
- end
- return unpack(r, 2)
- end
+ local rawexit = os.exit
+ os.exit = function(...)
+ on_exit()
+ rawexit(...)
+ end
end
+init()
-local rawexit = os.exit
-function os.exit(...)
- on_exit()
- rawexit(...)
-end \ No newline at end of file
+return M
diff --git a/src/luacov/stats.lua b/src/luacov/stats.lua
index 3fa5916..5390c75 100644
--- a/src/luacov/stats.lua
+++ b/src/luacov/stats.lua
@@ -1,26 +1,40 @@
-module("luacov.stats", package.seeall)
+local M = {}
local statsfile = "luacov.stats.out"
+local stats
-function load_stats()
+function M.load()
local data, most_hits = {}, 0
- local stats = io.open(statsfile, "r")
- if not stats then return data end
+ stats = io.open(statsfile, "r")
+ if not stats then
+ return data
+ end
while true do
local nlines = stats:read("*n")
- if not nlines then break end
+ if not nlines then
+ break
+ end
local skip = stats:read(1)
- if skip ~= ":" then break end
+ if skip ~= ":" then
+ break
+ end
local filename = stats:read("*l")
- if not filename then break end
- data[filename] = {}
- data[filename].max = nlines
+ if not filename then
+ break
+ end
+ data[filename] = {
+ max=nlines
+ }
for i = 1, nlines do
local hits = stats:read("*n")
- if not hits then break end
+ if not hits then
+ break
+ end
local skip = stats:read(1)
- if skip ~= " " then break end
+ if skip ~= " " then
+ break
+ end
if hits > 0 then
data[filename][i] = hits
most_hits = math.max(most_hits, hits)
@@ -31,19 +45,19 @@ function load_stats()
return data, most_hits
end
-function start_stats()
+function M.start()
return io.open(statsfile, "w")
end
-function stop_stats(stats)
+function M.stop(stats)
stats:close()
end
-function save_stats(data, stats)
+function M.save(data, stats)
stats:seek("set")
for filename, filedata in pairs(data) do
local max = filedata.max
- stats:write(filedata.max, ":", filename, "\n")
+ stats:write(max, ":", filename, "\n")
for i = 1, max do
local hits = filedata[i]
if not hits then
@@ -55,3 +69,5 @@ function save_stats(data, stats)
end
stats:flush()
end
+
+return M