Skip to content

Commit

Permalink
Validate the coverage directory.
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
mblayman committed Jul 16, 2023
1 parent f342d3b commit bdf6649
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.2 - To Be Released

* Validate the tests directory before trying collection.
* Validate the coverage directory before initializing coverage.
* Prefer source files over a local installation in a LuaRocks tree.

## v0.1 - 2023-07-14
Expand Down
16 changes: 15 additions & 1 deletion lua/luatest/coverage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ local runner = require "luacov.runner"
local dir = require "pl.dir"
local path = require "pl.path"

local Reporter = require "luatest.reporter"

-- Validate that the coverage directory exists.
local function validate_coverage_directory(coverage_directory)
if not path.isdir(coverage_directory) then
Reporter.fatal_early(coverage_directory ..
" is not a valid directory for coverage.")
end
end

-- Set all the files that should be measured for coverage.
local function set_included_files(config, configuration)
-- Include all source files in luacov format
-- (i.e. with forward slash and no extension)
local cwd = path.currentdir()
local src = path.join(cwd, config.cov)

validate_coverage_directory(src)

for root, _, files in dir.walk(src) do
for file_ in files:iter() do
local filepath = path.join(root, file_)
Expand Down Expand Up @@ -50,5 +63,6 @@ local function finalize_coverage() runner.shutdown() end
return {
initialize_coverage = initialize_coverage,
finalize_coverage = finalize_coverage,
set_included_files = set_included_files
set_included_files = set_included_files,
validate_coverage_directory = validate_coverage_directory
}
10 changes: 10 additions & 0 deletions lua/luatest/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ function Reporter.fatal(self, message)
os.exit(1)
end

-- Show a fatal error message and exit *early*.
-- This exists to support error messaging before the reporter is started.
-- It is *not* an instance method.
function Reporter.fatal_early(message)
-- luacov: disable
print(ansicolors("%{red}" .. message))
os.exit(1)
-- luacov: enable
end

--
-- Collection hooks
--
Expand Down
16 changes: 16 additions & 0 deletions tests/test_coverage.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local assert = require "luassert"
local stub = require "luassert.stub"
local path = require "pl.path"

local coverage = require "luatest.coverage"

Expand All @@ -23,4 +25,18 @@ function tests.test_set_included_files()
assert.is_same({}, expected)
end

-- The coverage directory exists or is an error.
function tests.test_coverage_directory_exists()
local cwd = path.currentdir()
local src = path.join(cwd, "not/real")
local Reporter = require "luatest.reporter"
stub(Reporter, "fatal_early")

coverage.validate_coverage_directory(src)

local expected = src .. " is not a valid directory for coverage."
assert.stub(Reporter.fatal_early).was_called_with(expected)
Reporter.fatal_early:revert()
end

return tests

0 comments on commit bdf6649

Please sign in to comment.