Pkg.test() fails if isdir(tempdir()) == false

Hi, Im have some tests which delete the tempdir() folder before exiting. They works perfectly. But, the problem is that if I call the tests again in the same process (e.g. using a REPL) the tests engine (Pkg) fails. Is this intended? Is deleting tempdir() a terrible practice? I must remark that Pkg fails regarded tempdir() is used in the tests or not.

Here a MWE:

using Pkg

# utils
printerr(err) = print(sprint(showerror, err, catch_backtrace()))

# creating package
pkg_name = "TestIssuePkg"
println("\n\n\n")
@info "Creating test pkg" pkg_name
println("\n\n")

pkg_path = joinpath(Pkg.devdir(), pkg_name)
rm(pkg_path; force = true, recursive = true)
Pkg.generate(pkg_path)

# adding Test extras
proj_file = joinpath(pkg_path, "Project.toml")
open(proj_file, "a") do io
println(io, 
"""
[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
"""
)
end

# adding runtests
# Note that the tests are empty
test_dir = joinpath(pkg_path, "test")
mkpath(test_dir)
write(joinpath(test_dir, "runtests.jl"), 
"""
using $(pkg_name)
using Test

@testset "$(pkg_name).jl" begin
    @test true
end
"""
)

# running tests
Pkg.activate(pkg_path)

println("\n\n\n")
@info "This should pass"
println("\n\n")
mkpath(tempdir())
Pkg.test()

println("\n\n\n")
@info "Pkg.test() fails if 'isdir(tempdir()) == false'"
println("\n\n")
rm(tempdir(); force = true, recursive = true)
try
    Pkg.test()
catch err
    printerr(err)
end

println("\n\n\n")
@info "If I run mkpath(tempdir()) autside 'test' it works again"
println("\n\n")

mkpath(tempdir())
Pkg.test()

Thanks

Should I file an issue?

I would think that this is expected behavior. If you delete the directory returned by tempdir(), bad things will happen.

Why does your code need to delete the tempdir() directory? If the goal is to create some temp files and then delete them when Julia exits, just do something like this:

my_temp_dir = mktempdir(; cleanup = true)

# save all your temporary files inside the `my_temp_dir` directory

Right before the Julia process exits, it will automatically delete the my_temp_dir directory and all of its contents.

1 Like

Yes, I know. I just think is a little rude that Pkg.test fails because tempdir() was deleted. In the docs there are no reference about tempdir() to be expected to actually exist.

help?> tempdir()
  tempdir()


  Gets the path of the temporary directory. On Windows, tempdir() uses the first environment variable
  found in the ordered list TMP, TEMP, USERPROFILE. On all other operating systems, tempdir() uses
  the first environment variable found in the ordered list TMPDIR, TMP, TEMP, and TEMPDIR. If none of
  these are found, the path "/tmp" is used.

This could affect the julia program if an external one delete the tempdir() as set in an environmental variables.

TMPDIR="~/not/a/real/dir" julia --project --startup-file=no -E '@show(tempdir()); import Pkg; Pkg.test()'
tempdir() = "~/not/a/real/dir"
     Testing GitWorkers
ERROR: IOError: mktempdir("~/not/a/real/dir"): no such file or directory (ENOENT)
Stacktrace:
  [...]