summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2016-06-26 18:46:40 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2016-06-26 18:50:16 +0300
commit58bd20ade56d28ada53adea4d953c1eb2479c09f (patch)
treec350cf34ae208a1c0bacb05d180af7329a78937a
parentf81bcd7b18b12c7c2a02efa19a7572a72c2bda09 (diff)
downloadluacov-58bd20ade56d28ada53adea4d953c1eb2479c09f.tar.gz
Add :on_file_error stub method
Called on error when opening, reading or loading a file. Default implementation reports the error to stderr.
-rw-r--r--src/luacov/reporter.lua27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/luacov/reporter.lua b/src/luacov/reporter.lua
index 81406bb..4e83a33 100644
--- a/src/luacov/reporter.lua
+++ b/src/luacov/reporter.lua
@@ -425,6 +425,13 @@ end
function ReporterBase:on_new_file(filename)
end
+--- Stub method called if a file couldn't be processed due to an error.
+-- @param filename name of the file.
+-- @param error_type "open", "read" or "load".
+-- @param message error message.
+function ReporterBase:on_file_error(filename, error_type, message)
+end
+
--- Stub method called for each empty source line
-- and other lines that can't be hit.
-- @param filename name of the file.
@@ -469,27 +476,31 @@ if cluacov_ok then
end
function ReporterBase:_run_file(filename)
- local file = io.open(filename)
+ local file, open_err = io.open(filename)
if not file then
- print("Could not open file " .. filename)
+ open_err = open_err:gsub("^" .. filename:gsub("%p", "%%%0") .. ": ", "")
+ self:on_file_error(filename, "open", open_err)
return
end
local active_lines
if cluacov_ok then
- local src = file:read("*a")
+ local src, read_err = file:read("*a")
if not src then
- print("Could not read file " .. filename)
+ self:on_file_error(filename, "read", read_err)
return
end
- local func = load(src)
+ local func, load_err = load(src, "@file")
if not func then
- print("Could not load file " .. filename)
+ local line_number
+ line_number, load_err = load_err:match("^file:(%d+): (.*)")
+ self:on_file_error(filename, "load",
+ load_err and ("line %d: %s"):format(line_number, load_err) or "error")
return
end
@@ -570,6 +581,10 @@ function DefaultReporter:on_new_file(filename)
self:write(("="):rep(78), "\n")
end
+function DefaultReporter:on_file_error(filename, error_type, message) --luacheck: no self
+ io.stderr:write(("Couldn't %s %s: %s\n"):format(error_type, filename, message))
+end
+
function DefaultReporter:on_empty_line(_, _, line)
if line == "" then
self:write("\n")