Skip to content

Commit

Permalink
Merge branch 'main' into cv/codecov-token
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored Jul 16, 2024
2 parents 0dd807a + f07e2a9 commit cee5ec2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 52 deletions.
7 changes: 0 additions & 7 deletions src/Mocking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ module Mocking
using Compat: mergewith
using ExprTools: splitdef, combinedef

# Available in Julia 1.11+: https://github.com/JuliaLang/julia/pull/50958
# We cannot use ScopedValues.jl for backwards compatability as that implementation breaks
# `@test_logs`.
if VERSION >= v"1.11.0-DEV.482"
using Base: ScopedValue, with
end

export @patch, @mock, Patch, apply

include("expr.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/mock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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(get_active_env(), target, args...)

function _debug_msg(method::Union{Method,Nothing}, target, args)
call = "$target($(join(map(arg -> "::$(Core.Typeof(arg))", args), ", ")))"
Expand Down
29 changes: 10 additions & 19 deletions src/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,19 @@ julia> apply(p1) do
function apply end

function apply(body::Function, pe::PatchEnv)
merged_pe = merge(PATCH_ENV[], pe)
return with_active_env(body, merged_pe)
prev_pe = get_active_env()
set_active_env(merge(prev_pe, pe))
try
return body()
finally
set_active_env(prev_pe)
end
end

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

# https://github.com/JuliaLang/julia/pull/50958
if VERSION >= v"1.11.0-DEV.482"
const PATCH_ENV = ScopedValue(PatchEnv())
with_active_env(body::Function, pe::PatchEnv) = with(body, PATCH_ENV => pe)
else
const PATCH_ENV = Ref{PatchEnv}(PatchEnv())

function with_active_env(body::Function, pe::PatchEnv)
old_pe = PATCH_ENV[]
try
PATCH_ENV[] = pe
body()
finally
PATCH_ENV[] = old_pe
end
end
end
const PATCH_ENV = Ref{PatchEnv}(PatchEnv())
set_active_env(pe::PatchEnv) = (PATCH_ENV[] = pe)
get_active_env() = PATCH_ENV[]
17 changes: 3 additions & 14 deletions test/async-scope.jl → test/async.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Async tasks should consistently use the patch environment (if any) they started with
@testset "async scope" begin
@testset "tasks" begin
c = Condition()
ch = Channel{String}(1)
f() = "original"
Expand All @@ -21,12 +20,7 @@
@test (@mock f()) == "mocked"

notify(c)
# https://github.com/JuliaLang/julia/pull/50958
if VERSION >= v"1.11.0-DEV.482"
@test take!(ch) == "original"
else
@test_broken take!(ch) == "original"
end
@test_broken take!(ch) == "original"

# Task started inside patched context should call patched functions.
@async background()
Expand All @@ -41,11 +35,6 @@
end

notify(c)
# https://github.com/JuliaLang/julia/pull/50958
if VERSION >= v"1.11.0-DEV.482"
@test take!(ch) == "mocked"
else
@test_broken take!(ch) == "mocked"
end
@test_broken take!(ch) == "mocked"
end
end
20 changes: 10 additions & 10 deletions test/concept.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
for p in patches
Mocking.apply!(pe, p)
end
Mocking.set_active_env(pe)

Mocking.apply(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)`
@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)`

@test (@mock multiply(2)) != multiply(2)
@test (@mock multiply(0x2)) != multiply(0x2)
@test (@mock multiply(2//1)) == multiply(2//1)
end
@test (@mock multiply(2)) != multiply(2)
@test (@mock multiply(0x2)) != multiply(0x2)
@test (@mock multiply(2//1)) == multiply(2//1)

# Patch environment has been reset back to original clean state
@test Mocking.PATCH_ENV[] == Mocking.PatchEnv()
# Clean env
pe = Mocking.PatchEnv()
Mocking.set_active_env(pe)

# Ensure that original behaviour is restored
@test (@mock multiply(2)) == 3
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Mocking.activate()
include("args.jl")
include("merge.jl")
include("nested_apply.jl")
include("async-scope.jl")
include("async.jl")
include("issues.jl")
include("activate.jl")
include("async-world-ages.jl")
Expand Down

0 comments on commit cee5ec2

Please sign in to comment.