From 2e978d1ac14a9a60ae901ce3403b31ad385a40f9 Mon Sep 17 00:00:00 2001 From: Ivan Timokhin Date: Wed, 1 Apr 2020 08:45:09 +0300 Subject: [PATCH] Unset TMPDIR before calling tempnam in tempname tempnam consults the TMPDIR environment variable before its argument, so if it is set the function will suddenly and quietly ignore the `parent` argument. --- base/file.jl | 4 +++- test/file.jl | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index 9e91f433f2942..9ed305d5ff9ea 100644 --- a/base/file.jl +++ b/base/file.jl @@ -547,7 +547,9 @@ else # !windows # Obtain a temporary filename. function tempname(parent::AbstractString=tempdir(); cleanup::Bool=true) isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory")) - p = ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix) + p = withenv("TMPDIR" => nothing) do + ccall(:tempnam, Cstring, (Cstring, Cstring), parent, temp_prefix) + end systemerror(:tempnam, p == C_NULL) s = unsafe_string(p) Libc.free(p) diff --git a/test/file.jl b/test/file.jl index 74a42cf7275a5..66335fb2f6340 100644 --- a/test/file.jl +++ b/test/file.jl @@ -72,6 +72,15 @@ end @test_throws ArgumentError tempname(randstring()) end +@testset "tempname with parent and TMPDIR" begin + withenv("TMPDIR" => tempdir()) do + mktempdir() do d + t = tempname(d) + @test dirname(t) == d + end + end +end + child_eval(code::String) = eval(Meta.parse(readchomp(`$(Base.julia_cmd()) -E $code`))) @testset "mktemp/dir basic cleanup" begin