|
| 1 | +empty!(Base.DEPOT_PATH) |
| 2 | +push!(Base.DEPOT_PATH, mktempdir(; cleanup = true)) |
| 3 | + |
| 4 | +import Pkg |
| 5 | +import Logging |
| 6 | +import TOML |
| 7 | + |
| 8 | +Pkg.add(; name = "Coverage", uuid = "a2441757-f6aa-5fb2-8edb-039e3f45d037", version = "1") |
| 9 | +Pkg.precompile() |
| 10 | + |
| 11 | +import Coverage |
| 12 | + |
| 13 | +function get_external_stdlib_names(stdlib_dir::AbstractString) |
| 14 | + filename_list = filter(x -> isfile(joinpath(stdlib_dir, x)), readdir(stdlib_dir)) |
| 15 | + # find all of the files like `Pkg.version`, `Statistics.version`, etc. |
| 16 | + regex_matches_or_nothing = match.(Ref(r"^([\w].*?)\.version$"), filename_list) |
| 17 | + regex_matches = filter(x -> x !== nothing, regex_matches_or_nothing) |
| 18 | + # get the names of the external stdlibs, like `Pkg`, `Statistics`, etc. |
| 19 | + external_stdlib_names = only.(regex_matches) |
| 20 | + unique!(external_stdlib_names) |
| 21 | + sort!(external_stdlib_names) |
| 22 | + @info "# Begin list of external stdlibs" |
| 23 | + for (i, x) in enumerate(external_stdlib_names) |
| 24 | + @info "$(i). $(x)" |
| 25 | + end |
| 26 | + @info "# End list of external stdlibs" |
| 27 | + return external_stdlib_names |
| 28 | +end |
| 29 | + |
| 30 | +function get_external_stdlib_prefixes(stdlib_dir::AbstractString) |
| 31 | + external_stdlib_names = get_external_stdlib_names(stdlib_dir) |
| 32 | + prefixes_1 = joinpath.(Ref(stdlib_dir), external_stdlib_names, Ref("")) |
| 33 | + prefixes_2 = joinpath.(Ref(stdlib_dir), string.(external_stdlib_names, Ref("-"))) |
| 34 | + prefixes = vcat(prefixes_1, prefixes_2) |
| 35 | + unique!(prefixes) |
| 36 | + sort!(prefixes) |
| 37 | + # example of what `prefixes` might look like: |
| 38 | + # 4-element Vector{String}: |
| 39 | + # "stdlib/Pkg-" |
| 40 | + # "stdlib/Pkg/" |
| 41 | + # "stdlib/Statistics-" |
| 42 | + # "stdlib/Statistics/" |
| 43 | + return prefixes |
| 44 | +end |
| 45 | + |
| 46 | +function print_coverage_summary(fc::Coverage.FileCoverage) |
| 47 | + cov_lines, tot_lines = Coverage.get_summary(fc) |
| 48 | + if cov_lines == tot_lines == 0 |
| 49 | + cov_pct = 0 |
| 50 | + else |
| 51 | + cov_pct = floor(Int, cov_lines/tot_lines * 100) |
| 52 | + end |
| 53 | + pad_1 = 71 |
| 54 | + pad_2 = 15 |
| 55 | + pad_3 = 15 |
| 56 | + col_1 = rpad(fc.filename, pad_1) |
| 57 | + col_2 = rpad(string(cov_pct, " %"), pad_2) |
| 58 | + col_3 = string( |
| 59 | + rpad(string(cov_lines), pad_3), |
| 60 | + string(tot_lines), |
| 61 | + ) |
| 62 | + @info "$(col_1) $(col_2) $(col_3)" |
| 63 | + return nothing |
| 64 | +end |
| 65 | + |
| 66 | +function print_coverage_summary( |
| 67 | + fcs::Vector{Coverage.FileCoverage}, description::AbstractString, |
| 68 | + ) |
| 69 | + cov_lines, tot_lines = Coverage.get_summary(fcs) |
| 70 | + if cov_lines == tot_lines == 0 |
| 71 | + cov_pct = 0 |
| 72 | + else |
| 73 | + cov_pct = floor(Int, cov_lines/tot_lines * 100) |
| 74 | + end |
| 75 | + @info "$(description): $(cov_pct)% ($(cov_lines)/$(tot_lines))" |
| 76 | + return nothing |
| 77 | +end |
| 78 | + |
| 79 | +# `Coverage.process_folder` will have a LOT of `@info` statements that will make the log |
| 80 | +# way too long. So before we run `Coverage.process_folder`, we disable logging for `@info` |
| 81 | +# statements. After we run `Coverage.process_folder`, we re-enable logging for `@info` |
| 82 | +# statements. |
| 83 | +Logging.disable_logging(Logging.Info) |
| 84 | +const fcs = Coverage.merge_coverage_counts( |
| 85 | + Coverage.process_folder("base"), |
| 86 | + Coverage.process_folder("stdlib"), |
| 87 | +); |
| 88 | +Logging.disable_logging(Logging.Debug) |
| 89 | + |
| 90 | +# Only include source code files. Exclude test files, benchmarking files, etc. |
| 91 | +filter!(fcs) do fc |
| 92 | + occursin(r"^base\/", fc.filename) || occursin("/src/", fc.filename) |
| 93 | +end; |
| 94 | + |
| 95 | +# Exclude all external stdlibs (stdlibs that live in external repos). |
| 96 | +const external_stdlib_prefixes = get_external_stdlib_prefixes("stdlib") |
| 97 | +filter!(fcs) do fc |
| 98 | + all(x -> !startswith(fc.filename, x), external_stdlib_prefixes) |
| 99 | +end; |
| 100 | + |
| 101 | +# Exclude all stdlib JLLs (stdlibs of the form `stdlib/*_jll/`). |
| 102 | +filter!(fcs) do fc |
| 103 | + !occursin(r"^stdlib\/[A-Za-z0-9]*?_jll\/", fc.filename) |
| 104 | +end |
| 105 | + |
| 106 | +sort!(fcs; by = fc -> fc.filename) |
| 107 | + |
| 108 | +print_coverage_summary.(fcs); |
| 109 | +print_coverage_summary(fcs, "Total") |
| 110 | + |
| 111 | +# In order to upload to Codecov, you need to have the `CODECOV_TOKEN` environment variable defined. |
| 112 | +Coverage.Codecov.submit_local(fcs) |
| 113 | + |
| 114 | +# In order to upload to Coveralls, you need to have the `COVERALLS_TOKEN` environment variable defined. |
| 115 | +Coverage.Coveralls.submit_local(fcs) |
0 commit comments