Skip to content

Commit 9a7f571

Browse files
authored
Merge pull request #56 from jagot/master
Allow passing custom CSS file to `html_coverage`
2 parents 33cac8d + 0f57ce8 commit 9a7f571

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LocalCoverage"
22
uuid = "5f6e1e16-694c-5876-87ef-16b5274f298e"
33
authors = ["Tamas K. Papp <[email protected]>"]
4-
version = "0.8.0"
4+
version = "0.8.1"
55

66
[deps]
77
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
@@ -23,6 +23,7 @@ Dates = "1.6"
2323
DefaultApplication = "1"
2424
DocStringExtensions = "0.8, 0.9"
2525
EzXML = "1"
26+
FileCmp = "1"
2627
LibGit2 = "1.6"
2728
OrderedCollections = "1"
2829
Pkg = "1.6"
@@ -32,6 +33,7 @@ julia = "1.6"
3233

3334
[extras]
3435
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
36+
FileCmp = "343a5541-a696-4ae7-8102-bc61c09e1896"
3537

3638
[targets]
37-
test = ["Test"]
39+
test = ["Test","FileCmp"]

src/LocalCoverage.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ end
281281
"""
282282
$(SIGNATURES)
283283
284-
Generate, and optionally open, the HTML coverage summary in a browser for `pkg`
285-
inside `dir`.
284+
Generate, and optionally open, the HTML coverage summary in a browser
285+
for `pkg` inside `dir`. The optional keyword argument `css` can be
286+
used to set the path to a custom CSS file styling the coverage report.
286287
287288
See [`generate_coverage`](@ref).
288289
"""
289-
function html_coverage(coverage::PackageCoverage; gitroot = ".", open = false, dir = tempdir())
290+
function html_coverage(coverage::PackageCoverage; gitroot = ".", open = false, dir = tempdir(), css::Union{Nothing,AbstractString}=nothing)
290291
cd(coverage.package_dir) do
291292
branch = try
292293
LibGit2.headname(GitRepo(gitroot))
@@ -298,7 +299,13 @@ function html_coverage(coverage::PackageCoverage; gitroot = ".", open = false, d
298299
tracefile = joinpath(COVDIR, LCOVINFO)
299300

300301
try
301-
run(`genhtml -t $(title) -o $(dir) $(tracefile)`)
302+
cmd = `genhtml -t $(title) -o $(dir) $(tracefile)`
303+
if !isnothing(css)
304+
css_file = abspath(css)
305+
isfile(css_file) || throw(ArgumentError("Could not find CSS file at $(css_file)"))
306+
cmd = `$(cmd) --css-file $(css_file)`
307+
end
308+
run(cmd)
302309
catch e
303310
error(
304311
"Failed to run genhtml. Check that lcov is installed (see the README).",
@@ -318,9 +325,10 @@ function html_coverage(pkg = nothing;
318325
dir = tempdir(),
319326
test_args = [""],
320327
folder_list = ["src"],
321-
file_list = [])
328+
file_list = [],
329+
css = nothing)
322330
gen_cov() = generate_coverage(pkg; test_args = test_args, folder_list = folder_list, file_list = file_list)
323-
html_coverage(gen_cov(); gitroot = gitroot, open = open, dir = dir)
331+
html_coverage(gen_cov(); gitroot = gitroot, open = open, dir = dir, css = css)
324332
end
325333

326334
"""

test/dummy.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
color: white;
3+
background-color: black;
4+
}

test/runtests.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using LocalCoverage, Test
2+
using FileCmp
3+
24
import Pkg
35

46
Pkg.activate("./DummyPackage/")
@@ -15,7 +17,8 @@ function test_coverage(pkg;
1517
run_test = true,
1618
test_args = [""],
1719
folder_list = ["src"],
18-
file_list = [])
20+
file_list = [],
21+
css = nothing)
1922
@info "Testing coverage for $pkg" test_args folder_list file_list
2023
clean_coverage(pkg)
2124
@test isdir(LocalCoverage.pkgdir(pkg))
@@ -47,8 +50,10 @@ function test_coverage(pkg;
4750

4851
if !isnothing(Sys.which("genhtml"))
4952
mktempdir() do dir
50-
html_coverage(pkg, dir = dir)
53+
html_coverage(pkg, dir = dir, css = css)
5154
@test isfile(joinpath(dir, "index.html"))
55+
isnothing(css) ||
56+
@test filecmp(joinpath(dir, "gcov.css"), css)
5257
end
5358
end
5459

@@ -83,6 +88,11 @@ end
8388
folder_list = [joinpath(dirname(@__FILE__), "DummyPackage", "src", "corge")],
8489
file_list = [joinpath(dirname(@__FILE__), "DummyPackage", "src", "qux.jl")])
8590
end
91+
92+
@testset "custom CSS" begin
93+
@test_throws TypeError test_coverage("DummyPackage", css=1)
94+
test_coverage("DummyPackage", css=joinpath(dirname(@__FILE__), "dummy.css"))
95+
end
8696
end
8797

8898
@test LocalCoverage.find_gaps([nothing, 0, 0, 0, 2, 3, 0, nothing, 0, 3, 0, 6, 2]) ==

0 commit comments

Comments
 (0)