Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
00krishna opened this issue Jan 2, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@00krishna
Copy link

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.

@davidanthoff davidanthoff added the bug Something isn't working label Jan 2, 2023
@davidanthoff davidanthoff transferred this issue from julia-vscode/TestItems.jl Jan 17, 2023
@felixcremer
Copy link

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
Copy link
Member

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

@davidanthoff
Copy link
Member

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
Copy link
Member

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

@davidanthoff
Copy link
Member

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

@pfitzseb
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants