Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding nameparser configuration entry. #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/luacov/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ return {
-- Default: false.
includeuntestedfiles = false,

--- Provides a function to parse filenames.
-- This function is used to parse filenames in the debug hook.
-- It is passed the filename as an argument and should return the parsed filename.
-- The default parser removes leading `@` and replaces path separators with `/`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description here doesn't really match the behavior in the code below:

  • in the code below, the @-stripping parser still takes precedence over the user's custom nameparser whenever a name starts with @, meaning that it is not only a "default" behavior that can be overridden with nameparser, but instead nameparser is just a fallback
  • the slash-conversion behavior happens after the namesparser as well.

I think a less surprising behavior would be:

  • priority order to be that the nameparser takes precedence over the @-stripping (that is, match what's implied in the docs when it calls the @-stripping the "default")
  • slash-conversion can remain after nameparser entries for simplicity (that is, match what is in the code), but that needs to be documented

-- @usage
-- nameparser = function(name)
-- return name:match("^@?(.*)")
-- end
nameparser = nil

}
4 changes: 4 additions & 0 deletions src/luacov/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ function hook.new(runner)
-- Get name of processed file.
local name = debug.getinfo(level, "S").source
local prefixed_name = string.match(name, "^@(.*)")
local name_parser = runner.configuration.nameparser
local parsed_name = name_parser and name_parser(name)
if prefixed_name then
name = prefixed_name:gsub("^%.[/\\]", ""):gsub("[/\\]", dir_sep)
elseif parsed_name then
name = parsed_name:gsub("^%.[/\\]", ""):gsub("[/\\]", dir_sep)
elseif not runner.configuration.codefromstrings then
-- Ignore Lua code loaded from raw strings by default.
return
Expand Down
4 changes: 4 additions & 0 deletions src/luacov/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ local function getsourcefile(func)
if d and d:sub(1, 1) == "@" then
return d:sub(2)
end
local name_parser = runner.configuration.nameparser
if d and name_parser then
return name_parser(d)
end
end

-- Looks for a function inside a table.
Expand Down