Skip to content

Commit

Permalink
tests: try adjusting the project for testing
Browse files Browse the repository at this point in the history
to have documenter available for some test files

except when running from the main runtests.jl
  • Loading branch information
benlorenz committed Feb 12, 2024
1 parent 4b20afe commit d3a0de7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
54 changes: 43 additions & 11 deletions src/utils/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ function _gather_tests(path::AbstractString; ignore=[])
return tests
end

_setactiveproject(s::String) = @static if VERSION >= v"1.8"
Base.set_active_project(s)

Check warning on line 91 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L90-L91

Added lines #L90 - L91 were not covered by tests
else
Base.ACTIVE_PROJECT[] = s

Check warning on line 93 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L93

Added line #L93 was not covered by tests
end

@doc raw"""
test_module(path::AbstractString; new::Bool = true, timed::Bool=false, ignore=[])
Expand All @@ -113,7 +119,7 @@ This only works for `new=false`.
For experimental modules, use [`test_experimental_module`](@ref) instead.
"""
function test_module(path::AbstractString; new::Bool=true, timed::Bool=false, ignore=[])
function test_module(path::AbstractString; new::Bool=true, timed::Bool=false, tempproject::Bool=true, ignore=[])
with_unicode(false) do
julia_exe = Base.julia_cmd()
project_path = Base.active_project()
Expand All @@ -126,7 +132,12 @@ function test_module(path::AbstractString; new::Bool=true, timed::Bool=false, ig
end
if new
@req isempty(ignore) && !timed "The `timed` and `ignore` options only work for `new=false`."
cmd = "using Test; using Oscar; Hecke.assertions(true); Oscar.test_module(\"$path\"; new=false);"
cmd = """

Check warning on line 135 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L135

Added line #L135 was not covered by tests
using Test;
using Oscar;
Hecke.assertions(true);
Oscar.test_module("$path"; new=false);
"""
@info("spawning ", `$julia_exe --project=$project_path -e \"$cmd\"`)
run(`$julia_exe --project=$project_path -e $cmd`)
else
Expand All @@ -140,18 +151,39 @@ function test_module(path::AbstractString; new::Bool=true, timed::Bool=false, ig
Base.cumulative_compile_timing(true)
end
stats = Dict{String,NamedTuple}()
for entry in testlist
dir = dirname(entry)
if isfile(joinpath(dir, "setup_tests.jl"))
Base.include(identity, Main, joinpath(dir, "setup_tests.jl"))

if tempproject
# we preserve the old load path
oldloadpath = copy(LOAD_PATH)

Check warning on line 157 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L157

Added line #L157 was not covered by tests
# make a copy of the test environment to make sure any existing manifest doesn't interfere
tmpproj = joinpath(mktempdir(), "Project.toml")
cp(joinpath(Oscar.oscardir, "test", "Project.toml"), tmpproj)

Check warning on line 160 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L159-L160

Added lines #L159 - L160 were not covered by tests
# activate the temporary project
_setactiveproject(tmpproj)

Check warning on line 162 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L162

Added line #L162 was not covered by tests
# and make sure the current project is still available to allow e.g. `using Oscar`
pushfirst!(LOAD_PATH, dirname(project_path))
Pkg.resolve()

Check warning on line 165 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L164-L165

Added lines #L164 - L165 were not covered by tests
end

try
for entry in testlist
dir = dirname(entry)
if isfile(joinpath(dir, "setup_tests.jl"))
Base.include(identity, Main, joinpath(dir, "setup_tests.jl"))
end
if timed
push!(stats, _timed_include(entry; use_ctime=use_ctime))
else
Base.include(identity, Main, entry)

Check warning on line 177 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L177

Added line #L177 was not covered by tests
end
end
if timed
push!(stats, _timed_include(entry; use_ctime=use_ctime))
else
Base.include(identity, Main, entry)
finally
# restore load path and project
if tempproject
copy!(LOAD_PATH, oldloadpath)
_setactiveproject(project_path)

Check warning on line 184 in src/utils/tests.jl

View check run for this annotation

Codecov / codecov/patch

src/utils/tests.jl#L183-L184

Added lines #L183 - L184 were not covered by tests
end
end

if timed
use_ctime && Base.cumulative_compile_timing(false)
return stats
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ end
# otherwise, is essentially a serial loop
stats = reduce(merge, pmap(testlist) do x
println("Starting tests for $x")
Oscar.test_module(x; new=false, timed=true)
Oscar.test_module(x; new=false, timed=true, tempproject=false)
end)

# this needs to run here to make sure it runs on the main process
Expand Down

0 comments on commit d3a0de7

Please sign in to comment.