From 1b9a85ae3ffb5410786c037033a9a226700388a2 Mon Sep 17 00:00:00 2001 From: inedited Date: Mon, 8 Apr 2019 02:18:36 +0200 Subject: [PATCH] Added minimum coverage constraint feauture --- src/luacov/defaults.lua | 118 +++++++++++++++++++++------------------- src/luacov/reporter.lua | 17 ++++++ 2 files changed, 78 insertions(+), 57 deletions(-) diff --git a/src/luacov/defaults.lua b/src/luacov/defaults.lua index 66d8fd6..9d01454 100644 --- a/src/luacov/defaults.lua +++ b/src/luacov/defaults.lua @@ -6,71 +6,75 @@ -- @name luacov.defaults return { - --- Filename to store collected stats. Default: "luacov.stats.out". - statsfile = "luacov.stats.out", + --- Filename to store collected stats. Default: "luacov.stats.out". + statsfile = "luacov.stats.out", - --- Filename to store report. Default: "luacov.report.out". - reportfile = "luacov.report.out", + --- Filename to store report. Default: "luacov.report.out". + reportfile = "luacov.report.out", - --- Enable saving coverage data after every `savestepsize` lines? - -- Setting this flag to `true` in config is equivalent to running LuaCov - -- using `luacov.tick` module. Default: false. - tick = false, + --- Enable saving coverage data after every `savestepsize` lines? + -- Setting this flag to `true` in config is equivalent to running LuaCov + -- using `luacov.tick` module. Default: false. + tick = false, - --- Stats file updating frequency for `luacov.tick`. - -- The lower this value - the more frequently results will be written out to the stats file. - -- You may want to reduce this value (to, for example, 2) to avoid losing coverage data in - -- case your program may terminate without triggering luacov exit hooks that are supposed - -- to save the data. Default: 100. - savestepsize = 100, + --- Minimum coverage requirement required to pass the coverage test. + --- Default is 0. + minimumcoverage = 0, - --- Run reporter on completion? Default: false. - runreport = false, + --- Stats file updating frequency for `luacov.tick`. + -- The lower this value - the more frequently results will be written out to the stats file. + -- You may want to reduce this value (to, for example, 2) to avoid losing coverage data in + -- case your program may terminate without triggering luacov exit hooks that are supposed + -- to save the data. Default: 100. + savestepsize = 100, - --- Delete stats file after reporting? Default: false. - deletestats = false, + --- Run reporter on completion? Default: false. + runreport = false, - --- Process Lua code loaded from raw strings? - -- That is, when the 'source' field in the debug info - -- does not start with '@'. Default: false. - codefromstrings = false, + --- Delete stats file after reporting? Default: false. + deletestats = false, - --- Lua patterns for files to include when reporting. - -- All will be included if nothing is listed. - -- Do not include the '.lua' extension. Path separator is always '/'. - -- Overruled by `exclude`. - -- @usage - -- include = { - -- "mymodule$", -- the main module - -- "mymodule%/.+$", -- and everything namespaced underneath it - -- } - include = {}, + --- Process Lua code loaded from raw strings? + -- That is, when the 'source' field in the debug info + -- does not start with '@'. Default: false. + codefromstrings = false, - --- Lua patterns for files to exclude when reporting. - -- Nothing will be excluded if nothing is listed. - -- Do not include the '.lua' extension. Path separator is always '/'. - -- Overrules `include`. - exclude = {}, + --- Lua patterns for files to include when reporting. + -- All will be included if nothing is listed. + -- Do not include the '.lua' extension. Path separator is always '/'. + -- Overruled by `exclude`. + -- @usage + -- include = { + -- "mymodule$", -- the main module + -- "mymodule%/.+$", -- and everything namespaced underneath it + -- } + include = {}, - --- Table mapping names of modules to be included to their filenames. - -- Has no effect if empty. - -- Real filenames mentioned here will be used for reporting - -- even if the modules have been installed elsewhere. - -- Module name can contain '*' wildcard to match groups of modules, - -- in this case corresponding path will be used as a prefix directory - -- where modules from the group are located. - -- @usage - -- modules = { - -- ["some_rock"] = "src/some_rock.lua", - -- ["some_rock.*"] = "src" - -- } - modules = {}, + --- Lua patterns for files to exclude when reporting. + -- Nothing will be excluded if nothing is listed. + -- Do not include the '.lua' extension. Path separator is always '/'. + -- Overrules `include`. + exclude = {}, - --- Enable including untested files in report. - -- If `true`, all untested files in "." will be included. - -- If it is a table with directory and file paths, all untested files in these paths will be included. - -- Note that you are not allowed to use patterns in these paths. - -- Default: false. - includeuntestedfiles = false, + --- Table mapping names of modules to be included to their filenames. + -- Has no effect if empty. + -- Real filenames mentioned here will be used for reporting + -- even if the modules have been installed elsewhere. + -- Module name can contain '*' wildcard to match groups of modules, + -- in this case corresponding path will be used as a prefix directory + -- where modules from the group are located. + -- @usage + -- modules = { + -- ["some_rock"] = "src/some_rock.lua", + -- ["some_rock.*"] = "src" + -- } + modules = {}, -} + --- Enable including untested files in report. + -- If `true`, all untested files in "." will be included. + -- If it is a table with directory and file paths, all untested files in these paths will be included. + -- Note that you are not allowed to use patterns in these paths. + -- Default: false. + includeuntestedfiles = false, + + } diff --git a/src/luacov/reporter.lua b/src/luacov/reporter.lua index 0aadce1..1a0d823 100644 --- a/src/luacov/reporter.lua +++ b/src/luacov/reporter.lua @@ -402,6 +402,16 @@ local function coverage_to_string(hits, missed) return ("%.2f%%"):format(hits/total*100.0) end +local function coverage_percentage(hits,missed) + local total = hits + missed + + if total == 0 then + total = 1 + end + + return (hits/total*100.0) +end + function DefaultReporter:on_end() self:write(("="):rep(78), "\n") self:write("Summary\n") @@ -466,6 +476,13 @@ function DefaultReporter:on_end() end end end + + local cover = coverage_percentage(total_hits,total_missed) + local required = self:config().minimumcoverage + if cover < required then + print(("Failed to hit the required minimum of %d%% coverage. Got %d%%."):format(required, cover)) + os.exit(1) + end end end