From 135a469e9a5545bf2afd89dc05a63eec219b344b Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 21 Jan 2020 13:27:50 +0100 Subject: [PATCH] Add support for exclude markers. --- src/CoverageTools.jl | 20 ++++++++++++++++++++ test/exclusions.jl | 9 +++++++++ test/runtests.jl | 13 +++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/exclusions.jl diff --git a/src/CoverageTools.jl b/src/CoverageTools.jl index 35ea28d..e085345 100644 --- a/src/CoverageTools.jl +++ b/src/CoverageTools.jl @@ -201,6 +201,26 @@ module CoverageTools end end end + + # check for excluded lines + let io = IOBuffer(content) + excluded = false + for (l, line) in enumerate(eachline(io)) + # check for start/stop markers + if occursin("COV_EXCL_START", line) + excluded = true + elseif occursin("COV_EXCL_STOP", line) + excluded = false + end + + # also check for line markers + if excluded || occursin("COV_EXCL_LINE", line) + @debug "removing line $l: $line" + coverage[l] = nothing + end + end + end + nothing end diff --git a/test/exclusions.jl b/test/exclusions.jl new file mode 100644 index 0000000..c067b6d --- /dev/null +++ b/test/exclusions.jl @@ -0,0 +1,9 @@ +function main() + println(devnull, 1) + println(devnull, 2) # COV_EXCL_LINE + println(devnull, 3) + # COV_EXCL_START + println(devnull, 4) + # COV_EXCL_STOP + println(devnull, 5) +end diff --git a/test/runtests.jl b/test/runtests.jl index febb75e..9a64cf4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -151,6 +151,19 @@ end @test isfile(joinpath(datadir_temp, "CoverageTools.jl")) # tear down test data rm(datadir_temp; recursive=true) + + # test exclusion markers + srcname = joinpath("test", "exclusions.jl") + covname = srcname*".cov" + clean_file(srcname) + cmdstr = "include($(repr(srcname))); main()" + run(`$(Base.julia_cmd()) --startup-file=no --code-coverage=user -e $cmdstr`) + r = withenv("DISABLE_AMEND_COVERAGE_FROM_SRC" => "yes") do + process_file(srcname, "test") + end + @test r.coverage == [nothing, 2, 1, 1, nothing, 1, nothing, 1, nothing, nothing] + amend_coverage_from_src!(r.coverage, r.filename) + @test r.coverage == [nothing, 2, nothing, 1, nothing, nothing, nothing, 1, nothing, nothing] end end