Skip to content

Commit c3bc36c

Browse files
authored
Merge pull request #4 from disberd/PrettyTables_fix
2 parents 299212c + 61279c5 commit c3bc36c

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name = "ExtendedLocalCoverage"
22
uuid = "eb248270-a497-541c-8f75-6bb2aa2715dc"
3+
version = "0.1.2"
34
authors = ["Alberto Mengali <[email protected]>"]
4-
version = "0.1.1"
55

66
[deps]
77
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
88
CoverageTools = "c36e975a-824b-4404-a568-ef97ca766997"
99
LocalCoverage = "5f6e1e16-694c-5876-87ef-16b5274f298e"
1010
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
11+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
1112
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
1213
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
1314
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
@@ -17,6 +18,7 @@ CondaPkg = "0.2.24"
1718
CoverageTools = "1.3.2"
1819
LocalCoverage = "0.8.2"
1920
Pkg = "1"
21+
PrettyTables = "2, 3"
2022
PythonCall = "0.9.23"
2123
Revise = "3.7.1"
2224
TOML = "1.0.3"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
[![Lint workflow Status](https://github.com/disberd/ExtendedLocalCoverage.jl/actions/workflows/Lint.yml/badge.svg?branch=main)](https://github.com/disberd/ExtendedLocalCoverage.jl/actions/workflows/Lint.yml?query=branch%3Amain)
88
[![Docs workflow Status](https://github.com/disberd/ExtendedLocalCoverage.jl/actions/workflows/Docs.yml/badge.svg?branch=main)](https://github.com/disberd/ExtendedLocalCoverage.jl/actions/workflows/Docs.yml?query=branch%3Amain)
99
[![Coverage](https://codecov.io/gh/disberd/ExtendedLocalCoverage.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/disberd/ExtendedLocalCoverage.jl)
10-
[![DOI](https://zenodo.org/badge/DOI/FIXME)](https://doi.org/FIXME)
1110
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
1211
[![All Contributors](https://img.shields.io/github/all-contributors/disberd/ExtendedLocalCoverage.jl?labelColor=5e1ec7&color=c0ffee&style=flat-square)](#contributors)
1312
[![BestieTemplate](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/JuliaBesties/BestieTemplate.jl/main/docs/src/assets/badge.json)](https://github.com/JuliaBesties/BestieTemplate.jl)
@@ -17,6 +16,9 @@ This package simply extends the functionality of [LocalCoverage.jl](https://gith
1716
- Automatically create an xml cobertura coverage and an html report using `pycobertura`.
1817
- The `pycobertura` library is automatically installed and used thanks to `CondaPkg.jl` and `PythonCall.jl`, not requiring the user to manually install python or `lcov` which is only available on non-windows systems.
1918

19+
This package was mainly created to provide easier access to code coverage within a private self-hosted gitlab instance, where codecov can not be used as is, but an HTML coverage report can be directly integrated within the gitlab web interface.
20+
21+
It is also convenient for getting an HTML coverage report locally without always relying on codecov from GH actions.
2022

2123
You can see an example of the generated HTML coverage report [here](https://disberd.github.io/ExtendedLocalCoverage.jl/coverage_example/), which was generated by calling `generate_package_coverage(; force_paths_relative = true)` on the [PlutoPlotly.jl](https://github.com/JuliaPluto/PlutoPlotly.jl) package.
2224

@@ -35,7 +37,7 @@ Pkg.add(url="https://github.com/disberd/ExtendedLocalCoverage.jl")
3537
For the basic usage which automatically extracts source files (including extensions) and generates the coverage report both in xml and html format, you can simply do the following (within the env of the package you are developing and making sure you have ExtendedLocalCoverage.jl in the `LOAD_PATH`):
3638
```julia
3739
using ExtendedLocalCoverage
38-
generate_package_coverage()
40+
cov_data = generate_package_coverage();
3941
```
4042

4143
See the [developer documentation](https://disberd.github.io/ExtendedLocalCoverage.jl/dev) for more details.

src/ExtendedLocalCoverage.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import Pkg
1010

1111
export generate_package_coverage, generate_html_report
1212

13+
# This is a temporary fix to fix PrettyTables issues until https://github.com/JuliaCI/LocalCoverage.jl/pull/68 is merged.
14+
include("show_fix.jl")
15+
1316
function extract_package_info(pkg_dir)
1417
project_toml = TOML.tryparsefile(joinpath(pkg_dir, "Project.toml"))
1518
pkg_name = project_toml["name"]
@@ -105,7 +108,7 @@ function generate_package_coverage(pkg = nothing; use_existing_lcov = false, run
105108
return true
106109
end
107110
LocalCoverage.generate_coverage(pkg; run_test, test_args, folder_list=[], file_list)
108-
end
111+
end |> WrappedPackageCoverage
109112
if print_to_stdout
110113
show(IOContext(stdout, :print_gaps => true), cov)
111114
end

src/show_fix.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using LocalCoverage: PackageCoverage, format_line
2+
using PrettyTables: PrettyTables, pretty_table
3+
4+
"""
5+
WrappedPackageCoverage(summary::PackageCoverage)
6+
7+
Structure wrapping the `PackageCoverage` struct to add a custom `show` method for fixing PrettyTables issues until https://github.com/JuliaCI/LocalCoverage.jl/pull/68 is merged.
8+
"""
9+
struct WrappedPackageCoverage
10+
summary::PackageCoverage
11+
end
12+
13+
function Base.show(io::IO, wrapped::WrappedPackageCoverage)
14+
(; summary) = wrapped
15+
(; files, package_dir) = summary
16+
row_data = map(format_line, files)
17+
push!(row_data, format_line(summary))
18+
row_coverage = map(x -> x.coverage_percentage, row_data)
19+
rows = map(row_data) do row
20+
(; name, total, hit, missed, coverage_percentage, gaps) = row
21+
percentage = isnan(coverage_percentage) ? "-" : "$(round(Int, coverage_percentage))%"
22+
(; name, total, hit, missed, percentage, gaps)
23+
end
24+
header = ["Filename", "Lines", "Hit", "Miss", "%"]
25+
percentage_column = length(header)
26+
alignment = [:l, :r, :r, :r, :r]
27+
columns_width = fill(-1, 5) # We need strictly negative number to autosize in PrettyTables 3.0, but this also works in v2
28+
if get(io, :print_gaps, false)
29+
push!(header, "Gaps")
30+
push!(alignment, :l)
31+
display_cols = last(get(io, :displaysize, 100))
32+
push!(columns_width, display_cols - 45)
33+
else
34+
rows = map(row -> Base.structdiff(row, NamedTuple{(:gaps,)}), rows)
35+
end
36+
# PrettyTables 3.0 changed Highlighter to TextHighlighter, which up to currently published version (v3.10) does not provide the kwargs constructor (despite having it documented). We create here a patch to handle both cases
37+
Highlighter(f; kwargs...) = @static if pkgversion(PrettyTables) < v"3.0.0"
38+
PrettyTables.Highlighter(f; kwargs...)
39+
else
40+
PrettyTables.TextHighlighter(f, PrettyTables.Crayon(;kwargs...))
41+
end
42+
43+
highlighters = (
44+
Highlighter(
45+
(data, i, j) -> j == percentage_column && row_coverage[i] <= 50,
46+
bold = true,
47+
foreground = :red,
48+
),
49+
Highlighter((data, i, j) -> j == percentage_column && row_coverage[i] <= 70,
50+
foreground = :yellow),
51+
Highlighter((data, i, j) -> j == percentage_column && row_coverage[i] >= 90,
52+
foreground = :green),
53+
)
54+
55+
# Kwargs of `pretty_table` itself also changed in PrettyTables 3.0, so we have to branch here as well
56+
@static if pkgversion(PrettyTables) < v"3.0.0"
57+
pretty_table(
58+
io,
59+
rows;
60+
title = "Coverage of $(package_dir)",
61+
header,
62+
alignment,
63+
crop = :none,
64+
linebreaks = true,
65+
columns_width,
66+
autowrap = true,
67+
highlighters,
68+
body_hlines = [length(rows) - 1],
69+
)
70+
else
71+
pretty_table(
72+
io,
73+
rows;
74+
title = "Coverage of $(package_dir)",
75+
column_labels = [header],
76+
alignment,
77+
# The crop kwarg is not present anymore, split into the next two ones
78+
fit_table_in_display_horizontally = false,
79+
fit_table_in_display_vertically = false,
80+
line_breaks = true,
81+
fixed_data_column_widths = columns_width,
82+
auto_wrap = true,
83+
highlighters = collect(highlighters), # v3 expects a vector instead of a Tuple
84+
table_format = PrettyTables.TextTableFormat(;
85+
horizontal_lines_at_data_rows = [length(rows) - 1],
86+
),
87+
)
88+
end
89+
end

0 commit comments

Comments
 (0)