summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2016-06-18 00:02:29 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2016-06-18 00:44:56 +0300
commitd1b01f1cb7fca50cec816b52a3debfb8c47d7f8b (patch)
treec37d68a663a6724cf8d56262db16e2634466cb3a
parent2e09976f66431277202970df70a51af2f9423f34 (diff)
downloadluacov-d1b01f1cb7fca50cec816b52a3debfb8c47d7f8b.tar.gz
Add cluacov support for deepactivelines
-rw-r--r--src/luacov/hook.lua2
-rw-r--r--src/luacov/reporter.lua101
2 files changed, 72 insertions, 31 deletions
diff --git a/src/luacov/hook.lua b/src/luacov/hook.lua
index 58cd911..0c070c1 100644
--- a/src/luacov/hook.lua
+++ b/src/luacov/hook.lua
@@ -28,7 +28,7 @@ function hook.new(runner)
if prefixed_name then
name = prefixed_name
elseif not runner.configuration.codefromstrings then
- -- Ignore Lua code loaded from raw strings bu default.
+ -- Ignore Lua code loaded from raw strings by default.
return
end
diff --git a/src/luacov/reporter.lua b/src/luacov/reporter.lua
index 7bc98dd..81406bb 100644
--- a/src/luacov/reporter.lua
+++ b/src/luacov/reporter.lua
@@ -5,6 +5,7 @@
local reporter = {}
local luacov = require("luacov.runner")
+local load = loadstring or load -- luacheck: compat
-- Raw version of string.gsub
local function replace(s, old, new)
@@ -460,45 +461,85 @@ end
-- luacheck: pop
-function ReporterBase:run()
- self:on_start()
+local cluacov_ok = pcall(require, "cluacov.version")
+local deepactivelines
- for _, filename in ipairs(self:files()) do
- local file = io.open(filename, "r")
+if cluacov_ok then
+ deepactivelines = require("cluacov.deepactivelines")
+end
- if not file then
- print("Could not open file " .. filename)
- else
- self:on_new_file(filename)
- local file_hits, file_miss = 0, 0
- local filedata = self:stats(filename)
+function ReporterBase:_run_file(filename)
+ local file = io.open(filename)
- local line_nr = 1
- local scanner = LineScanner:new()
+ if not file then
+ print("Could not open file " .. filename)
+ return
+ end
- while true do
- local line = file:read("*l")
- if not line then break end
+ local active_lines
- local always_excluded, excluded_when_not_hit = scanner:consume(line)
- local hits = filedata[line_nr] or 0
+ if cluacov_ok then
+ local src = file:read("*a")
- if always_excluded or (excluded_when_not_hit and hits == 0) then
- self:on_empty_line(filename, line_nr, line)
- elseif hits == 0 then
- self:on_mis_line(filename, line_nr, line)
- file_miss = file_miss + 1
- else
- self:on_hit_line(filename, line_nr, line, hits)
- file_hits = file_hits + 1
- end
+ if not src then
+ print("Could not read file " .. filename)
+ return
+ end
- line_nr = line_nr + 1
- end
+ local func = load(src)
+
+ if not func then
+ print("Could not load file " .. filename)
+ return
+ end
+
+ active_lines = deepactivelines.get(func)
+ file:seek("set")
+ end
+
+ self:on_new_file(filename)
+ local file_hits, file_miss = 0, 0
+ local filedata = self:stats(filename)
+
+ local line_nr = 1
+ local scanner = LineScanner:new()
- file:close()
- self:on_end_file(filename, file_hits, file_miss)
+ while true do
+ local line = file:read("*l")
+ if not line then break end
+
+ local always_excluded, excluded_when_not_hit = scanner:consume(line)
+ local hits = filedata[line_nr] or 0
+ local included = not always_excluded and (not excluded_when_not_hit or hits ~= 0)
+
+ if cluacov_ok then
+ included = included and active_lines[line_nr]
+ end
+
+ if included then
+ if hits == 0 then
+ self:on_mis_line(filename, line_nr, line)
+ file_miss = file_miss + 1
+ else
+ self:on_hit_line(filename, line_nr, line, hits)
+ file_hits = file_hits + 1
+ end
+ else
+ self:on_empty_line(filename, line_nr, line)
end
+
+ line_nr = line_nr + 1
+ end
+
+ file:close()
+ self:on_end_file(filename, file_hits, file_miss)
+end
+
+function ReporterBase:run()
+ self:on_start()
+
+ for _, filename in ipairs(self:files()) do
+ self:_run_file(filename)
end
self:on_end()