Skip to content
Open
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 for which files code coverage is 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
72 changes: 72 additions & 0 deletions test/Configuration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,76 @@ end
Pluto.WorkspaceManager.unmake_workspace((🍭, nb))
end

@testset "Code Coverage" begin
options = Pluto.Configuration.from_flat_kwargs(; launch_browser=false)

🍭 = ServerSession(; options)
nb = Pluto.Notebook([
Pluto.Cell("str(x) = x == C_NULL ? nothing : unsafe_string(x)")
Pluto.Cell("Base.JLOptions().code_coverage")
Pluto.Cell("let opt = Base.JLOptions()
hasproperty(opt, :tracked_path) ? opt.tracked_path |> str : nothing
end")
Pluto.Cell("Base.JLOptions().output_code_coverage |> str")
])
sn = (🍭, nb)

function coverage_data(nb)
function str(x)
s = strip(x, '"')
# Deal with Windows paths
out = replace(s, "\\\\" => "\\") |> String
end
(;
code_coverage = parse(Int, nb.cells[2].output.body),
tracked_path = nb.cells[3].output.body |> str,
output_code_coverage = nb.cells[4].output.body |> str,
)
end
# Tests inspired by https://github.com/JuliaLang/julia/blob/4e7294423f4462244617af6f9eea425bd06a78a6/test/cmdlineargs.jl#L459
Pluto.update_run!(🍭, nb, nb.cells)
nd = coverage_data(nb)
@test nd.code_coverage == 0 # Defaults to 0, `none`
@test nd.tracked_path == ""
@test nd.output_code_coverage == ""
Pluto.WorkspaceManager.unmake_workspace(sn)

🍭.options.compiler.code_coverage_track = "user"
Pluto.update_run!(🍭, nb, nb.cells)
nd = coverage_data(nb)
@test nd.code_coverage == 1 # 1 is `user`
@test nd.tracked_path == ""
@test nd.output_code_coverage == ""
Pluto.WorkspaceManager.unmake_workspace(sn)

🍭.options.compiler.code_coverage_track = "all"
Pluto.update_run!(🍭, nb, nb.cells)
nd = coverage_data(nb)
@test nd.code_coverage == 2 # 2 is `all`
@test nd.tracked_path == ""
@test nd.output_code_coverage == ""
Pluto.WorkspaceManager.unmake_workspace(sn)

if VERSION >= v"1.8"
# This option with path was only introduced in v1.8
filepath = @__FILE__
🍭.options.compiler.code_coverage_track = "@$filepath"
Pluto.update_run!(🍭, nb, nb.cells)
nd = coverage_data(nb)
@test nd.code_coverage == 3 # 3 is `@<path>`
@test nd.tracked_path == filepath
@test nd.output_code_coverage == ""
Pluto.WorkspaceManager.unmake_workspace(sn)
end

🍭.options.compiler.code_coverage_file = "coverage.info"
🍭.options.compiler.code_coverage_track = "user"
Pluto.update_run!(🍭, nb, nb.cells)
nd = coverage_data(nb)
@test nd.code_coverage == 1
@test nd.tracked_path == ""
@test endswith(nd.output_code_coverage , "coverage.info")
Pluto.WorkspaceManager.unmake_workspace(sn)
end

end