diff --git a/Project.toml b/Project.toml index 58ac5c7..7f5de25 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TrixiBase" uuid = "9a0f1c46-06d5-4909-a5a3-ce25d3fa3284" authors = ["Michael Schlottke-Lakemper "] -version = "0.1.11-DEV" +version = "0.1.11" [deps] ChangePrecision = "3cb15238-376d-56a3-8042-d33272777c9a" diff --git a/src/trixi_include.jl b/src/trixi_include.jl index 00a7131..37f6f13 100644 --- a/src/trixi_include.jl +++ b/src/trixi_include.jl @@ -200,7 +200,9 @@ function replace_assignments(expr, recursive = false; kwargs...) for (key, val) in kwargs if (x.head === Symbol("=") || x.head === :kw) && x.args[1] === Symbol(key) - x.args[2] = :($val) + # Symbols need to be quoted so that they are evaluated as values instead + # of variable names in the included file. + x.args[2] = val isa Symbol ? QuoteNode(val) : val # dump(x) end end @@ -242,7 +244,8 @@ function replace_assignments(expr, recursive = false; kwargs...) # `x = ...` in the file, which will also be replaced in the loop above. for (key, val) in kwargs if !(Symbol(key) in existing_kwargs) - push!(x.args, Expr(:kw, Symbol(key), val)) + value = val isa Symbol ? QuoteNode(val) : val + push!(x.args, Expr(:kw, Symbol(key), value)) end end end diff --git a/test/trixi_include.jl b/test/trixi_include.jl index b41f389..67bc969 100644 --- a/test/trixi_include.jl +++ b/test/trixi_include.jl @@ -29,6 +29,15 @@ @test x == 7 end + @trixi_test_nowarn trixi_include(@__MODULE__, path, x = :symbol) + + if VERSION >= v"1.12" + mod = @__MODULE__ + @test (@invokelatest mod.x) === :symbol + else + @test x === :symbol + end + # Verify default version (that includes in `Main`) @trixi_test_nowarn trixi_include(path, x = 11) if VERSION >= v"1.12" @@ -196,6 +205,20 @@ @test y == 20 # Overridden from nested file @test z == 30 # Overridden from top file end + + # Test a Symbol override that is only assigned in the nested file. + # This covers the branch where missing kwargs are injected + # into the nested `trixi_include` call. + @test_warn "assignments" trixi_include(@__MODULE__, path2; + x = :symbol, + replace_assignments_recursive = true) + if VERSION >= v"1.12" + mod = @__MODULE__ + @test (@invokelatest mod.x) === :symbol + else + @test x === :symbol + end + # Test that kwargs are NOT passed recursively @trixi_test_nowarn trixi_include(@__MODULE__, path2; x = 10, y = 20, z = 30, @@ -255,6 +278,16 @@ @test a == 500 # Top-level override wins over nested explicit kwarg @test b == 600 # Passed through to nested file end + + # Test overwriting an existing nested kwarg with a Symbol. + @trixi_test_nowarn trixi_include(@__MODULE__, path4; a = :symbol, + replace_assignments_recursive = true) + if VERSION >= v"1.12" + mod = @__MODULE__ + @test (@invokelatest mod.a) === :symbol + else + @test a === :symbol + end end end