Skip to content

Commit

Permalink
Append user exeflags to env exeflags (#209)
Browse files Browse the repository at this point in the history
* Merge user specified exeflags into env specified flags

* formatting

* need default exeflags after all
  • Loading branch information
jkrumbiegel authored Nov 7, 2024
1 parent cbb3269 commit 6c90394
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ function _extract_timeout(merged_options)
end

function _exeflags_and_env(options)
exeflags = map(String, options["format"]["metadata"]["julia"]["exeflags"])
env_exeflags =
JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String})
options_exeflags = map(String, options["format"]["metadata"]["julia"]["exeflags"])
# We want to be able to override exeflags that are defined via environment variable,
# but leave the remaining flags intact (for example override number of threads but leave sysimage).
# We can do this by adding the options exeflags after the env exeflags.
# Julia will ignore earlier uses of the same flag.
exeflags = [env_exeflags; options_exeflags]
env = map(String, options["format"]["metadata"]["julia"]["env"])
# Use `--project=@.` if neither `JULIA_PROJECT=...` nor `--project=...` are specified
if !any(startswith("JULIA_PROJECT="), env) && !any(startswith("--project="), exeflags)
Expand Down Expand Up @@ -537,11 +544,10 @@ end

function default_frontmatter()
D = Dict{String,Any}
exeflags = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_EXEFLAGS", "[]"), Vector{String})
env = JSON3.read(get(ENV, "QUARTONOTEBOOKRUNNER_ENV", "[]"), Vector{String})
return D(
"fig-format" => "png",
"julia" => D("exeflags" => exeflags, "env" => env),
"julia" => D("env" => env, "exeflags" => []),
"execute" => D("error" => true),
)
end
Expand Down
14 changes: 14 additions & 0 deletions test/examples/exeflags_merging.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Exeflags merging
engine: julia
julia:
exeflags: []
---

```{julia}
print(Base.active_project())
```

```{julia}
print(Threads.nthreads())
```
46 changes: 46 additions & 0 deletions test/testsets/exeflags_merging.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
include("../utilities/prelude.jl")

@testset "exeflags merging" begin

function with_replacement_file(f, file, replacements...)
s = read(file, String)
mktempdir() do dir
tempfile = joinpath(dir, basename(file))
open(tempfile, "w") do io
write(io, replace(s, replacements...))
end
f(tempfile)
end
end

file = joinpath(@__DIR__, "../examples/exeflags_merging.qmd")

withenv(
"QUARTONOTEBOOKRUNNER_EXEFLAGS" => "[\"--project=/set_via_env\", \"--threads=3\"]",
) do
server = QuartoNotebookRunner.Server()
json = QuartoNotebookRunner.run!(server, file; showprogress = false)
@test contains(json.cells[2].outputs[1].text, "set_via_env")
@test json.cells[4].outputs[1].text == "3"
close!(server)

with_replacement_file(
file,
"[]" => "[\"--project=/override_via_frontmatter\"]",
) do newfile
server = QuartoNotebookRunner.Server()
json = QuartoNotebookRunner.run!(server, newfile; showprogress = false)
@test contains(json.cells[2].outputs[1].text, "override_via_frontmatter")
@test json.cells[4].outputs[1].text == "3"
close!(server)
end

with_replacement_file(file, "[]" => "[\"--threads=5\"]") do newfile
server = QuartoNotebookRunner.Server()
json = QuartoNotebookRunner.run!(server, newfile; showprogress = false)
@test contains(json.cells[2].outputs[1].text, "set_via_env")
@test json.cells[4].outputs[1].text == "5"
close!(server)
end
end
end

0 comments on commit 6c90394

Please sign in to comment.