Mocking.jl icon indicating copy to clipboard operation
Mocking.jl copied to clipboard

Bug: Error while running in @safetestset

Open iskyd opened this issue 2 years ago • 5 comments

runtests.jl

cd(@__DIR__)

using Pkg

using Test, TestSetExtensions, SafeTestsets

@testset ExtendedTestSet "Example tests" begin
  @includetests ARGS
end

test.jl

@safetestset "test" begin
    using Mocking
    
    Mocking.activate()
    patch = @patch open(fn::Function, f::AbstractString) = fn(IOBuffer(data))
end

This throws LoadError: UndefVarError: @patch not defined

Julia version: 1.8.2 Operating System: Ubuntu 22.04 Mocking version:

iskyd avatar Mar 09 '23 14:03 iskyd

Hmm, does Mocking.@patch work since Mocking.activate() seems to be working? I wonder if something weird is happening with the export @patch inside the @safetestset macro?

rofinn avatar Mar 09 '23 15:03 rofinn

@rofinn I got LoadError: UndefVarError: Mocking not defined and that happens on the line where I use Mocking.@patch, Mocking.activate works.

iskyd avatar Mar 09 '23 16:03 iskyd

Weird. Okay, if nobody gets to it I'll take a closer look in a few hours.

rofinn avatar Mar 09 '23 17:03 rofinn

@rofinn I think that the problem is this: https://github.com/YingboMa/SafeTestsets.jl/issues/3

iskyd avatar Mar 09 '23 18:03 iskyd

Ah, that makes sense. I don't think there's much we can do on the Mocking.jl side then, but we might be able to work around it for your case. If you're willing to manually construct the Mocking.Patch object instead of using @patch to parse the expression then that should work.

https://github.com/JuliaTesting/Mocking.jl/blob/master/src/patch.jl#L2

rofinn avatar Mar 09 '23 21:03 rofinn

The workaround in SafeTestsets.jl is to use an include in your @safetestset and utilize the using Mocking from within the included file.

Overall, this is an issue with SafeTestsets.jl and I've made a PR to allow the original example to work in: https://github.com/YingboMa/SafeTestsets.jl/pull/16

omus avatar Jul 15 '24 15:07 omus