Skip to content

Commit

Permalink
permit flexible use of amend_coverage_from_src!
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored and fingolfin committed Mar 15, 2019
1 parent 3c20a83 commit c2217ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/Coverage.jl
Expand Up @@ -147,29 +147,32 @@ module Coverage

"""
amend_coverage_from_src!(coverage::Vector{CovCount}, srcname)
amend_coverage_from_src!(fc::FileCoverage)
The code coverage functionality in Julia can miss code lines, which
will be incorrectly recorded as `nothing` but should instead be 0
This function takes a coverage count vector and a the filename for
a Julia code file, and updates the coverage vector in place.
The code coverage functionality in Julia only reports lines that have been
compiled. Unused functions (or discarded lines) therefore may be incorrectly
recorded as `nothing` but should instead be 0.
This function takes an existing result and updates the coverage vector
in-place to mark source lines that may be inside a function.
"""
function amend_coverage_from_src!(coverage::Vector{CovCount}, srcname)
amend_coverage_from_src!(coverage::Vector{CovCount}, srcname) = amend_coverage_from_src!(FileCoverage(srcname, read(srcname, String), coverage))
function amend_coverage_from_src!(fc::FileCoverage)
# The code coverage results produced by Julia itself report some
# lines as "null" (cannot be run), when they could have been run
# but were never compiled (thus should be 0).
# We use the Julia parser to augment the coverage results by identifying this code.
#
# To make sure things stay in sync, parse the file position
# corresponding to each new line
content, coverage = fc.source, fc.coverage
linepos = Int[]
open(srcname) do io
let io = IOBuffer(content)
while !eof(io)
push!(linepos, position(io))
readline(io)
end
push!(linepos, position(io))
end
content = read(srcname, String)
pos = 1
while pos <= length(content)
# We now want to convert the one-based offset pos into a line
Expand All @@ -189,7 +192,7 @@ module Coverage
flines .+= lineoffset
for l in flines
if l > length(coverage)
error("source file is longer than .cov file; source might have changed")
resize!(coverage, l)
end
if coverage[l] === nothing
coverage[l] = 0
Expand All @@ -212,10 +215,11 @@ module Coverage
function process_file(filename, folder)
@info "Coverage.process_file: Detecting coverage for $filename"
coverage = process_cov(filename, folder)
fc = FileCoverage(filename, read(filename, String), coverage)
if get(ENV, "DISABLE_AMEND_COVERAGE_FROM_SRC", "no") != "yes"
amend_coverage_from_src!(coverage, filename)
amend_coverage_from_src!(fc)
end
return FileCoverage(filename, read(filename, String), coverage)
return fc
end
process_file(filename) = process_file(filename, splitdir(filename)[1])

Expand Down
7 changes: 6 additions & 1 deletion test/runtests.jl
Expand Up @@ -117,7 +117,8 @@ end
run(`$(Base.julia_cmd()) --startup-file=no --code-coverage=user -e $cmdstr`)
r = process_file(srcname, datadir)

target = Union{Int64,Nothing}[nothing, 2, nothing, 0, nothing, 0, nothing, nothing, nothing, nothing, 0, nothing, nothing, nothing, nothing, nothing, 0, nothing, nothing, 0, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing]
target = Coverage.CovCount[nothing, 2, nothing, 0, nothing, 0, nothing, nothing, nothing, nothing, 0, nothing, nothing, nothing, nothing, nothing, 0, nothing, nothing, 0, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing]
target_disabled = map(x -> (x !== nothing && x > 0) ? x : nothing, target)
@test r.coverage == target

covtarget = (sum(x->x !== nothing && x > 0, target), sum(x->x !== nothing, target))
Expand All @@ -128,6 +129,10 @@ end
process_file(srcname, datadir)
end

@test r_disabled.coverage == target_disabled
amend_coverage_from_src!(r_disabled.coverage, r_disabled.filename)
@test r_disabled.coverage == target

# Handle an empty coverage vector
emptycov = FileCoverage("", "", [])
@test get_summary(emptycov) == (0, 0)
Expand Down

0 comments on commit c2217ef

Please sign in to comment.