Reputation: 14501
I am trying to do a few tests on my package and the file I need to use is located in the data folder so when I try to use it in my code I do randomFuncName(joinpath(@__DIR__, "src", "data", "etc", "etc"))
but the issue is that since I am in the "test" folder (given that I am running the tests) it creates the relative path of ".../.../test/src/data/..." instead of just "src/data".
Any ideas on how I can specify this path without using the absolute path (such that it works on CI and other systems)?
Upvotes: 4
Views: 867
Reputation: 42214
I prefer using the location of the package rather than relying on relative paths.
For example when you project grows you might want to have many test files and move them around to subfolders. Relying on joinpath(@__DIR__, "..","something")
always means that you are making some assumptions on where your file running tests is located.
Hence what I would do is to use pathof(Module)
:
using PackageName
BASE_FOLDER = dirname(dirname(pathof(PackageName)))
test_file = joinpath(BASE_FOLDER, "data", "file.txt")
Of course this is very subjective :-)
Upvotes: 6
Reputation: 2554
@__DIR__
depends on the file you're in, not pwd
. So you could call f(joinpath(@__DIR__, "foo.jl"))
from inside of src
and f(joinpath(@__DIR__, "..", "src", "foo.jl"))
from inside test
.
Upvotes: 3