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

**URL:** <https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486>\
**Category:** General Usage\
**Tags:** question, testing, pkg\
**Created:** [October 9, 2021, 9:36pm UTC](https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486 "2021-10-09T21:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [October 9, 2021, 9:36pm UTC](https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486/1 "2021-10-09T21:36:38Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [October 9, 2021, 9:39pm UTC](https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486/2 "2021-10-09T21:39:44Z")

</div>

Should I file an issue?

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [October 9, 2021, 10:14pm UTC](https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486/3 "2021-10-09T22:14:20Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [October 10, 2021, 4:33am UTC](https://discourse.julialang.org/t/pkg-test-fails-if-isdir-tempdir-false/69486/4 "2021-10-10T04:33:15Z")

</div>

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.

```julia
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.

```bash
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:
  [...]

```
