Is there a difference under the hood between running code via a .jl file and running code via `test name_of_pkg`?

Hey all,

I am running into a weird issue. I have some code which I will arbitrarily say is:

function z()
   z = 10
   z += 100
end
z()
# This code will work, it's just an arbitrary example. 

When I have the above code in a random “.jl” file and I run it, there are no issues. However, when I move the code into my “runtests.jl” file and then do test PkgName, I get an error.

Is there some difference under the hood in the way the code is run usually vs the way it is run when testing a package through the Repl?

There are a couple differences: Pkg.test spawns a new process to run the tests in, and starts the process with --check-bounds=yes and --startup-file=no (see 12. API Reference · Pkg.jl). I believe on master also Julia will not show deprecation warnings by default (https://github.com/JuliaLang/julia/pull/35362), but Pkg.test will pass --depwarn=yes to enable them.

5 Likes

An other difference has to do with dependencies: Pkg.test will run the tests in an “environment stack” that does not include the “default environment” (I’m actually not sure how to call it; it’s the environment that’s named after the Julia version and which is active by default).

In contrast, the REPL runs by default in a composite environment where the environment of the currently active project is stacked with the default environment, as determined by LOAD_PATH:

julia> LOAD_PATH
3-element Array{String,1}:
 "@"       # currently active project
 "@v#.#"   # default environment
 "@stdlib" # standard library

This means that if you code makes use of some package that has been Pkg.added to the default environment, but not declared as a dependency of the current project, Pkg.test will catch that.

5 Likes

Ah, that reminds me as well that the standard library InteractiveUtils is loaded automatically in the REPL, but isn’t loaded with Pkg.test.

3 Likes

Thanks all! It ended up being that I was using a relative file path and being one folder deeper (in the test folder) messed things up :slight_smile:

1 Like