julia-vscode / TestItemRunner.jl

Run Julia test items
MIT License
67 stars 11 forks source link

The working directory inside of a `@testitem` block does not point to the package test directory or root directory. #40

Open 00krishna opened 1 year ago

00krishna commented 1 year ago

I am building a package and was starting to use @testitem for tests.

The @testitem block seems to set the working directory as my user home directory, instead of the local directory of the testing file, or the package root directory. I have some code such as:

The impact is that I have some test files that I download while testing in CI. So the issue with the working directory means that the path to my test files won't work, or has to be hard coded--which is usually not the best :).

@testitem "MyPkg environment setup" begin

    using MyPkg, Test

    @show pwd()
    @test isfile("testfile.csv")

end

I get an output that looks like below in the test output:

> Test run finished at 02/01/2023, 15:14:48 <

pwd() = "/home/krishnab"
Test Summary:                                                                                  | Pass  Total  Time
/drives/.../MyPkg/test/runtests.jl:MyPkg environment setup |    4      4  0.0s

Please let me know if you need any additional detail.

felixcremer commented 1 week ago

You can use the @__DIR__ macro inside of the testitem to get the path to the directory to then use the relative path from there. So you can replace the isfile("testfile.csv") by joinpath(@__DIR__, "testfile.csv")

pfitzseb commented 1 week ago

Yeah, that's the correct solution I think. Arguably we could set the working directory to the package dir though.

davidanthoff commented 1 week ago

So the main question is: package dir or folder of the file in which the test is defined :) I had a Slack question on that, and folks came down on both sides. So I'm not sure what to do...

pfitzseb commented 1 week ago

Option 3 is "document the current behaviour and discourage people from relying on pwd" :P

davidanthoff commented 1 week ago

Haha, you mean having a different choice in VS Code and the command line runner ;) Not really ideal, I would say :)

pfitzseb commented 1 week ago

Random working dir to really drive the point home maybe?

But yeah, agreed. Imho a consistent working dir across all tests would be the nicer choice.

00krishna commented 1 week ago

Hey folks. My sense is that in the absence of an optimal solution it is still best to have a consistent solution. So starting from the package directory or starting from the test file directory would be fine, it should just be consistent. Then the users can work around that.

davidanthoff commented 1 week ago

Let's make it the folder of the file where the test is defined. If at some point in the future we want to allow tests outside of packages that would still work.