Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Compiler flag to specify --code-coverage #2822

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ julia = "^1.6"

[extras]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -77,4 +78,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"

[targets]
test = ["DataFrames", "OffsetArrays", "Random", "Sockets", "Test", "TimerOutputs", "Memoize"]
test = ["DataFrames", "OffsetArrays", "Random", "Sockets", "Test", "TimerOutputs", "Memoize", "LibGit2"]
12 changes: 12 additions & 0 deletions src/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ const MATH_MODE_DEFAULT = nothing
const STARTUP_FILE_DEFAULT = "no"
const HISTORY_FILE_DEFAULT = "no"
const HEAP_SIZE_HINT_DEFAULT = nothing
const CODE_COVERAGE_TRACK_DEFAULT = nothing
const CODE_COVERAGE_FILE_DEFAULT = nothing

function roughly_the_number_of_physical_cpu_cores()
# https://gist.github.com/fonsp/738fe244719cae820245aa479e7b4a8d
Expand Down Expand Up @@ -234,6 +236,8 @@ These options will be passed as command line argument to newly launched processe
- `inline::Union{Nothing,String} = $INLINE_DEFAULT`
- `check_bounds::Union{Nothing,String} = $CHECK_BOUNDS_DEFAULT`
- `math_mode::Union{Nothing,String} = $MATH_MODE_DEFAULT`
- `code_coverage_track::Union{Nothing,String} = "$CODE_COVERAGE_TRACK_DEFAULT"`. This specifies what should be tracked. It corresponds to the `--code-coverage[={none*|user|all}]` (or `--code-coverage=@<path>` for julia 1.8 and above) command line option.
- `code_coverage_file::Union{Nothing,String} = "$CODE_COVERAGE_FILE_DEFAULT"`. This specifies the file where code coverage is recorded if provided. It corresponds to the `--code-coverage=filename` command line option.
- `heap_size_hint::Union{Nothing,String} = $HEAP_SIZE_HINT_DEFAULT`
- `startup_file::Union{Nothing,String} = "$STARTUP_FILE_DEFAULT"` By default, the startup file isn't loaded in notebooks.
- `history_file::Union{Nothing,String} = "$HISTORY_FILE_DEFAULT"` By default, the history isn't loaded in notebooks.
Expand All @@ -257,6 +261,9 @@ These options will be passed as command line argument to newly launched processe
math_mode::Union{Nothing,String} = MATH_MODE_DEFAULT
heap_size_hint::Union{Nothing,String} = HEAP_SIZE_HINT_DEFAULT

code_coverage_track::Union{String, Nothing} = CODE_COVERAGE_TRACK_DEFAULT
code_coverage_file::Union{String, Nothing} = CODE_COVERAGE_FILE_DEFAULT

# notebook specified options
# the followings are different from
# the default julia compiler options
Expand Down Expand Up @@ -323,6 +330,8 @@ function from_flat_kwargs(;
check_bounds::Union{Nothing,String} = CHECK_BOUNDS_DEFAULT,
math_mode::Union{Nothing,String} = MATH_MODE_DEFAULT,
heap_size_hint::Union{Nothing,String} = HEAP_SIZE_HINT_DEFAULT,
code_coverage_track::Union{String, Nothing} = CODE_COVERAGE_TRACK_DEFAULT,
code_coverage_file::Union{String, Nothing} = CODE_COVERAGE_FILE_DEFAULT,
startup_file::Union{Nothing,String} = STARTUP_FILE_DEFAULT,
history_file::Union{Nothing,String} = HISTORY_FILE_DEFAULT,
threads::Union{Nothing,String,Int} = default_number_of_threads(),
Expand Down Expand Up @@ -375,6 +384,8 @@ function from_flat_kwargs(;
check_bounds,
math_mode,
heap_size_hint,
code_coverage_track,
code_coverage_file,
startup_file,
history_file,
threads,
Expand Down Expand Up @@ -410,6 +421,7 @@ function _convert_to_flags(options::CompilerOptions)::Vector{String}

for name in fieldnames(CompilerOptions)
flagname = string("--", replace(String(name), "_" => "-"))
flagname = startswith(flagname, "--code-coverage") ? "--code-coverage" : flagname
value = getfield(options, name)
if value !== nothing && flagname ∉ exclude_list
push!(option_list, string(flagname, "=", value))
Expand Down
39 changes: 39 additions & 0 deletions test/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Pluto: ServerSession, ClientSession, SessionActions
using Pluto.Configuration
using Pluto.Configuration: notebook_path_suggestion, from_flat_kwargs, _convert_to_flags
using Pluto.WorkspaceManager: poll
using LibGit2
import URIs

@testset "Configurations" begin
Expand Down Expand Up @@ -54,6 +55,9 @@ end

@test _convert_to_flags(Configuration.CompilerOptions(compile="min")) ⊇
["--compile=min", "--startup-file=no", "--history-file=no"]

@test _convert_to_flags(Configuration.CompilerOptions(; code_coverage_file = "coverage.info", code_coverage_track = "user")) ⊇
["--code-coverage=coverage.info", "--code-coverage=user"]
end

@testset "Authentication" begin
Expand Down Expand Up @@ -246,4 +250,39 @@ end
cleanup(🍭, notebook)
end

@testset "Code Coverage" begin
mktempdir() do path
cd(path) do
# We develop Example.jl in this folder
example_path = joinpath(path, "Example")
LibGit2.clone("https://github.com/JuliaLang/Example.jl", example_path)
example_src = joinpath(example_path, "src", "Example.jl")

options = Pluto.Configuration.from_flat_kwargs(; launch_browser=false)
🍭 = ServerSession(; options)
🍭.options.compiler.code_coverage_track = "user"
🍭.options.compiler.code_coverage_file = "coverage.info"
if VERSION >= v"1.8"
# We change to track only this directory
🍭.options.compiler.code_coverage_track = "@$(path)"
end

covfile = joinpath(path, "coverage.info")

nb = Pluto.Notebook([
Pluto.Cell("""import Pkg; Pkg.activate(raw"$(example_path)")""")
Pluto.Cell("using Example")
Pluto.Cell("""hello("Pluto")""")
Pluto.Cell("domath(5)")
])
sn = (🍭, nb)

Pluto.update_run!(🍭, nb, nb.cells)
Pluto.WorkspaceManager.unmake_workspace(sn) # We need to close the workspace for coverage files to be generated

@test contains(read(covfile, String), example_src)
end
end
end

end
Loading