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

Doctests as testitem? #49

Open
gdalle opened this issue May 8, 2023 · 3 comments
Open

Doctests as testitem? #49

gdalle opened this issue May 8, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@gdalle
Copy link

gdalle commented May 8, 2023

I'm not sure how to run doctests as a testitem:

  • the output of doctest(MyPackage) is not a bool, so I can't do @test doctest(MyPackage) inside the @testitem
  • when I do doctest(MyPackage) directly inside the @testitem, it fails with an uninformative error message:
Test Failed at ~.julia/packages/Documenter/H5y27/src/Documenter.jl:963
  Expression: all_doctests()
@gdalle
Copy link
Author

gdalle commented May 8, 2023

By digging around in the output I found some more information. It probably has to do with the execution of the testitems in a new module:

[ Info: Doctest: running doctests.
┌ Warning: Failed to evaluate `CurrentModule = MyPackage` in `@meta` block.
│   exception = UndefVarError: `MyPackage` not defined
└ @ Documenter.DocTests ~/.julia/packages/Documenter/H5y27/src/Utilities/Utilities.jl:34
┌ Error: Doctesting failed
│   exception =
│    `makedocs` encountered a doctest error. Terminating build

@davidanthoff davidanthoff added the enhancement New feature or request label Jun 15, 2023
@davidanthoff
Copy link
Member

We should probably just try to detect doc tests statically and then treat them as a special kind of test item, or something like that...

@ORBAT
Copy link

ORBAT commented Jul 29, 2023

This is the workaround I've been using to run doctests in a @testitem. It temporarily adds the standard environment "@v#.#" to the load path so that my package doesn't have to have Documenter in its dependencies (ie. it assumes that Documenter can be found in that environment), loads Documenter and then runs the actual function that calls doctest. Fooling with the load path while "live" is probably not a great idea, but I haven't personally run into any surprises yet.

I feel like there's got to be better ways to do this, but I'm a Julia beginner so this is the best I could come up with 😅

using TestItems

@testitem "doctests" begin
  function with_documenter(fn)
    env = "@v#.#"

    if !(env in Base.LOAD_PATH)
      insert!(Base.LOAD_PATH, 2, env)
      @info "changed LOAD_PATH" Base.LOAD_PATH
    end

    __mod = @__MODULE__()
    @eval __mod using Documenter

    try
      @info "running test fn"
      @eval __mod $fn()
      @info "test fn done"
    catch e
      rethrow(e)
    finally
      let idx = findfirst(isequal(env), Base.LOAD_PATH)
        if idx == nothing
          return
        end
        deleteat!(Base.LOAD_PATH, idx)
        @info "reset LOAD_PATH" Base.LOAD_PATH
      end
    end
  end

  with_documenter() do
    DocMeta.setdocmeta!(Musica, :DocTestSetup, :(using Musica); recursive=true)
    doctest(Musica; manual=false)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants