I recently reorganized my packages and moved local ones outside ~/.julia/v0.6
. This makes is easier for me to wipe everything when run into problems I can’t solve otherwise.
However, I found that Pkg.test
does not work any more. Is there a hack/workaround that would allow me to run tests for local packages?
(I know that I can always symlink things back into ~/.julia/v0.6
, I am interested in other solutions)
I looked at Pkg.test
and now have a partial solution, which just needs a directory. The question is: how can I find the directory for a module? Then I would be all set.
function local_test(dir; coverage::Bool=false)
test_path = joinpath(dir, "test", "runtests.jl")
pkg = splitdir(dir)[2]
@assert isfile(test_path) "Could not find $(test_path)"
Base.cd(dir) do
try
color = Base.have_color? "--color=yes" : "--color=no"
codecov = coverage? ["--code-coverage=user"] : ["--code-coverage=none"]
compilecache = "--compilecache=" * (Bool(Base.JLOptions().use_compilecache) ? "yes" : "no")
julia_exe = Base.julia_cmd()
run(`$julia_exe --check-bounds=yes $codecov $color $compilecache $test_path`)
info("$pkg tests passed")
catch err
Base.Pkg.Entry.warnbanner(err, label="[ ERROR: $pkg ]")
push!(errs,pkg)
end
end
end
This now works, but it makes assumptions about the directory structure which are hopefully standard:
"""
local_test(pkgname, [coverage])
Find and test a package in `LOAD_PATH`.
Useful when the package is outside `Pkg.dir()`.
"""
function local_test(pkgname; coverage::Bool=false)
module_path = Base.find_in_path(pkgname, nothing)
src_dir, module_file = splitdir(module_path)
dir = normpath(src_dir, "..")
test_path = joinpath(dir, "test", "runtests.jl")
@assert isfile(test_path) "Could not find $(test_path)"
Base.cd(dir) do
try
color = Base.have_color? "--color=yes" : "--color=no"
codecov = coverage? ["--code-coverage=user"] : ["--code-coverage=none"]
compilecache = "--compilecache=" * (Bool(Base.JLOptions().use_compilecache) ? "yes" : "no")
julia_exe = Base.julia_cmd()
run(`$julia_exe --check-bounds=yes $codecov $color $compilecache $test_path`)
info("$module_file tests passed")
catch err
Base.Pkg.Entry.warnbanner(err, label="[ ERROR: $module_file ]")
end
end
end
2 Likes
Can’t you just set JULIA_PKGDIR
to point to the new location?
Assuming that the module is accessible from LOAD_PATH
, I am just using something like this when I run the tests locally:
include("/path/to/MyModule/test/runtest.jl")
For code coverage, I rely only on travis-ci.
Pkg.test
has a few convenience like starting the script in a new process so there is no old state hanging around and stuff like segfaults doesn’t kill your main session.
Unfortunately not, Pkg.test
will complain about the lack of METADATA
. One could initialize it etc, but that is more hassle than just running the function above.