JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.46k stars 187 forks source link

using @pywith inside @safetestset #967

Closed AbhimanyuAryan closed 2 years ago

AbhimanyuAryan commented 2 years ago

Trying to call a macro inside @safetestset for testing purposes. Keep getting errors @pywith, @py_str, @uitest(coming from my package which uses pycall) not defined

Error Stack:

test_with:
=====================================================
StippleUI tests: Error During Test at /Users/arstar/Git/Stipp/STest/test/runtests.jl:10
  Got exception outside of a @test
  LoadError: UndefVarError: @py_str not defined
  Stacktrace:
    [1] top-level scope
      @ :0
    [2] eval(m::Module, e::Any)
      @ Core ./boot.jl:373
    [3] top-level scope
      @ ~/.julia/packages/SafeTestsets/A83XK/src/SafeTestsets.jl:23
    [4] include(mod::Module, _path::String)
      @ Base ./Base.jl:418
    [5] macro expansion
      @ ~/.julia/packages/TestSetExtensions/lB7xG/src/TestSetExtensions.jl:40 [inlined]
    [6] macro expansion
      @ ~/Git/Stipp/STest/test/runtests.jl:11 [inlined]
    [7] macro expansion
      @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Test/src/Test.jl:1283 [inlined]
    [8] top-level scope
      @ ~/Git/Stipp/STest/test/runtests.jl:11
    [9] include(fname::String)
      @ Base.MainInclude ./client.jl:451
   [10] top-level scope
      @ REPL[1]:1
   [11] eval
      @ ./boot.jl:373 [inlined]
   [12] eval_user_input(ast::Any, backend::REPL.REPLBackend)
      @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:150
   [13] repl_backend_loop(backend::REPL.REPLBackend)
      @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:246
   [14] start_repl_backend(backend::REPL.REPLBackend, consumer::Any)
      @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:231
   [15] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool)
      @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:364
   [16] run_repl(repl::REPL.AbstractREPL, consumer::Any)
      @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:351
   [17] (::Base.var"#930#932"{Bool, Bool, Bool})(REPL::Module)
      @ Base ./client.jl:394
   [18] #invokelatest#2
      @ ./essentials.jl:716 [inlined]
   [19] invokelatest
      @ ./essentials.jl:714 [inlined]
   [20] run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool)
      @ Base ./client.jl:379
   [21] exec_options(opts::Base.JLOptions)
      @ Base ./client.jl:309
   [22] _start()
      @ Base ./client.jl:495
  in expression starting at /Users/arstar/Git/Stipp/STest/test/test_with.jl:4
  in expression starting at /Users/arstar/Git/Stipp/STest/test/test_with.jl:1
=====================================================

Test Summary:   | Error  Total
StippleUI tests |     1      1
ERROR: LoadError: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.
in expression starting at /Users/arstar/Git/Stipp/STest/test/runtests.jl:10
macro uitest(VAR, BLOCK)
  EXPR = myfunc()
  as = :as
  if isexpr(VAR, :(::))
    VAR, TYPE = VAR.args
  else
    TYPE = PyAny
  end
  if !((as == :as) && isa(VAR, Symbol))
    throw(ArgumentError("usage: @uitest EXPR[::TYPE1] BLOCK."))
  end
  PyCall._pywith(EXPR, VAR, TYPE, BLOCK)
end

#=
usage:

@uitest p begin
  # code
end
=#

Doesn't work inside @safetestset infact no macro does. Tested @pywith as well says definition not found. Maybe I'm doing something wrong. Can anyone point me to right direction?

@safetestset "With Macro Test" begin
  using PyCall

  py"""  # using @py_str
  x = 2
  """
end
AbhimanyuAryan commented 2 years ago

Also Macros are expanded before the code is executed so tried

test_with.jl

using PyCall

@safetestset "With Macro Test" begin

  @pywith pybuiltin("open")("file.txt","w") as f begin
    f.write("hello")
  end
end

Error:

  Got exception outside of a @test
  LoadError: LoadError: UndefVarError: @pywith not defined

runtests.jl

cd(@__DIR__)

using Pkg

using Test, TestSetExtensions, SafeTestsets, Logging

Logging.global_logger(NullLogger())

@testset ExtendedTestSet "Pycall tests" begin
  @includetests ARGS #[(endswith(t, ".jl") && t[1:end-3]) for t in ARGS]
end

julia> include("runtests.jl")

AbhimanyuAryan commented 2 years ago

solved by using separate file for test and including the file in safetestset