Skip to content

Commit ebed19a

Browse files
committed
implement the fix from JuliaCI/LocalCoverage.jl#68
1 parent 299212c commit ebed19a

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
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"

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)