diff options
author | Geoff Leyland <geoff_leyland@fastmail.fm> | 2009-11-06 12:46:04 -0300 |
---|---|---|
committer | Norman Clarke <norman@njclarke.com> | 2009-11-06 12:46:04 -0300 |
commit | 470f9e34dc7f833cc23496602da90903d4a0f5fe (patch) | |
tree | 867453d6cdfb3ab6e21e973390d9e2cc6d7c8715 | |
parent | d098f2128b8bd96376748024906c9804cfac9ce4 (diff) | |
download | luacov-470f9e34dc7f833cc23496602da90903d4a0f5fe.tar.gz |
Added options to exclude files, and ignores some code lines.
Signed-off-by: Norman Clarke <norman@njclarke.com>
-rwxr-xr-x | src/bin/luacov | 97 |
1 files changed, 85 insertions, 12 deletions
diff --git a/src/bin/luacov b/src/bin/luacov index 517680c..5eb912d 100755 --- a/src/bin/luacov +++ b/src/bin/luacov @@ -2,7 +2,7 @@ local luacov = require("luacov.stats") -local data = luacov.load_stats() +local data, most_hits = luacov.load_stats() if not data then print("Could not load stats file "..luacov.statsfile..".") @@ -12,13 +12,64 @@ end local report = io.open("luacov.report.out", "w") +-- only report on files specified on the command line +local patterns = {} +for i = 1, #arg do + patterns[i] = arg[i] +end + local names = {} for filename, _ in pairs(data) do - table.insert(names, filename) + if not patterns[1] then + table.insert(names, filename) + else + local path = filename:gsub("/", "."):gsub("%.lua$", "") + local include = false + for _, p in ipairs(patterns) do + if path:match(p) then + include = true + break + end + end + if include then + table.insert(names, filename) + end + end end table.sort(names) +local most_hits_length = ("%d"):format(most_hits):len() +local empty_format = (" "):rep(most_hits_length+1) +local false_negative_format = ("!%% %dd"):format(most_hits_length) +local zero_format = ("*"):rep(most_hits_length).."0" +local count_format = ("%% %dd"):format(most_hits_length+1) + +local exclusions = +{ + { false, "^#!" }, -- Unix hash-bang magic line + { true, "" }, -- Empty line + { true, "end,?" }, -- Single "end" + { true, "else" }, -- Single "else" + { true, "repeat" }, -- Single "repeat" + { true, "do" }, -- Single "do" + { true, "local%s+[%w_,%s]+" }, -- "local var1, ..., varN" + { true, "local%s+[%w_,%s]+%s*=" }, -- "local var1, ..., varN =" + { true, "local%s+function%s*%([%w_,%s]*%)" }, -- "local function(arg1, ..., argN)" + { true, "local%s+function%s+[%w_]*%s*%([%w_,%s]*%)" }, -- "local function f (arg1, ..., argN)" +} + +local function excluded(line) + for _, e in ipairs(exclusions) do + if e[1] then + if line:match("^%s*"..e[2].."%s*$") or line:match("^%s*"..e[2].."%s*%-%-") then return true end + else + if line:match(e[2]) then return true end + end + end + return false +end + for _, filename in ipairs(names) do local filedata = data[filename] local file = io.open(filename, "r") @@ -28,21 +79,43 @@ for _, filename in ipairs(names) do report:write(filename, "\n") report:write("==============================================================================\n") local line_nr = 1 + block_comment, equals = false, "" while true do local line = file:read("*l") if not line then break end - if line:match("^%s*%-%-") -- Comment line - or line:match("^%s*$") -- Empty line - or line:match("^%s*end,?%s*$") -- Single "end" - or line:match("^%s*else%s*$") -- Single "else" - or line:match("^#!") -- Unix hash-bang magic line - then - report:write("\t", line, "\n") + local true_line = line + + local new_block_comment = false + if not block_comment then + local l, equals = line:match("^(.*)%-%-%[(=*)%[") + if l then + line = l + new_block_comment = true + end + else + local l = line:match("%]"..equals.."%](.*)$") + if l then + line = l + block_comment = false + end + end + + local hits = filedata[line_nr] or 0 + if block_comment or excluded(line) then + if hits > 0 then + report:write(false_negative_format:format(hits)) + else + report:write(empty_format) + end else - local hits = filedata[line_nr] - if not hits then hits = 0 end - report:write(hits, "\t", line, "\n") + if hits == 0 then + report:write(zero_format) + else + report:write(count_format:format(hits)) + end end + report:write("\t", true_line, "\n") + if new_block_comment then block_comment = true end line_nr = line_nr + 1 end end |