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

Use ScopedValue to fix async issue #112

Merged
merged 10 commits into from
Jul 13, 2024
Prev Previous commit
Next Next commit
Use ScopedValues rather than ContextVariables
  • Loading branch information
oxinabox committed Feb 26, 2024
commit 230e310be62565300a02c6fdf1f145b0cbe37771
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ version = "0.7.7"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
ContextVariablesX = "6add18c4-b38d-439d-96f6-d6bc489c04c5"
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"

[compat]
Expand Down
1 change: 0 additions & 1 deletion src/Mocking.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Mocking

using Compat: mergewith
using ContextVariablesX: @contextvar, with_context
using ExprTools: splitdef, combinedef

export @patch, @mock, Patch, apply
Expand Down
2 changes: 2 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ function enable(; force=false)
depwarn("`$m.enable(; force=$force)` is deprecated, use `$m.activate()` instead.", :enable)
activate()
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end

@deprecate set_active_env(f, pe) with_active_env(f, pe) false
omus marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a proper deprecation as set_active_env would have it's changes persist beyond the scope of f. I think we should probably just make this a breaking change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, though this is what the previous change did, modulo also renaming set_active_env.
That function isn't actually exported.
Since the normal way to do it is to use apply(pe) do.
More an internal implementation detail of apply ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm okay with stating that set_active_env and get_active_env are internal functions and don't require deprecations. Making this a breaking change would be the safest but I could be convinced to have this be non-breaking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it breaks something can always revert, tag patch, and rerelease as breaking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will just delete them both and not deprecate

2 changes: 1 addition & 1 deletion src/mock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function get_alternate(pe::PatchEnv, target, args...)
end
end

get_alternate(target, args...) = get_alternate(patch_env[], target, args...)
get_alternate(target, args...) = get_alternate(PATCH_ENV[], target, args...)

function _debug_msg(method::Union{Method,Nothing}, target, args)
call = "$target($(join(map(arg -> "::$(Core.Typeof(arg))", args), ", ")))"
Expand Down
22 changes: 17 additions & 5 deletions src/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,26 @@ end
```
"""
function apply(body::Function, pe::PatchEnv)
merged_pe = merge(patch_env[], pe)
return with_context(body, patch_env => merged_pe)
merged_pe = merge(PATCH_ENV[], pe)
return with_active_env(body, merged_pe)
end

function apply(body::Function, patches; debug::Bool=false)
return apply(body, PatchEnv(patches, debug))
end

@contextvar patch_env = PatchEnv()
set_active_env(body::Function, pe::PatchEnv) = with_context(body, patch_env => pe)
get_active_env() = patch_env[]
if VERSION > v"1.11-"
omus marked this conversation as resolved.
Show resolved Hide resolved
const PATCH_ENV = ScopedValue(PatchEnv())
with_active_env(body::Function, pe::PatchEnv) = with(body, PATCH_ENV => pe)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
else
function with_active_env(body::Function, pe::PatchEnv)
old_pe = get_active_env
try
PATCH_ENV[] = pe
body()
finally
PATCH_ENV[] = old_pe
end
end
omus marked this conversation as resolved.
Show resolved Hide resolved
end
get_active_env() = PATCH_ENV[]
omus marked this conversation as resolved.
Show resolved Hide resolved
omus marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion test/concept.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Mocking.apply!(pe, p)
end

Mocking.set_active_env(pe) do
Mocking.with_active_env(pe) do
@test (@mock multiply(2)) == 8 # calls mocked `multiply(::Int)`
@test (@mock multiply(0x2)) == 0x6 # calls mocked `multiply(::Integer)`
@test (@mock multiply(2//1)) == 4//1 # calls original `multiply(::Number)`
Expand Down
Loading