diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 206d2957a91e5..847f41fe43a89 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -1254,9 +1254,10 @@ function take_heap_snapshot(all_one::Bool=false; dir::Union{Nothing,S}=nothing) if isnothing(dir) wd = pwd() fpath = joinpath(wd, fname) + fpath_test = fpath * ".test" # use a different test file as windows can fail to save immediately after rm-ing a file try - touch(fpath) - rm(fpath; force=true) + touch(fpath_test) + rm(fpath_test; force=true) catch @warn "Cannot write to current directory `$(pwd())` so saving heap snapshot to `$(tempdir())`" maxlog=1 _id=Symbol(wd) fpath = joinpath(tempdir(), fname) diff --git a/stdlib/Profile/test/runtests.jl b/stdlib/Profile/test/runtests.jl index eccfeea846a23..63f9a262f7905 100644 --- a/stdlib/Profile/test/runtests.jl +++ b/stdlib/Profile/test/runtests.jl @@ -269,19 +269,46 @@ end end @testset "HeapSnapshot" begin - tmpdir = mktempdir() - fname = cd(tmpdir) do - read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + @testset "default save" begin + tmpdir = mktempdir() + fname = cd(tmpdir) do + read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + end + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + rm(fname) + rm(tmpdir, force = true, recursive = true) end - @test isfile(fname) - - open(fname) do fs - @test readline(fs) != "" + @testset "default save when current directory is readonly" begin + tmpdir = mktempdir() + fname = cd(tmpdir) do + chmod(tmpdir, 0o555) + read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + end + @test !occursin(tmpdir, fname) # should not be in the given dir + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + chmod(tmpdir, 0o777) + rm(fname) + rm(tmpdir, force = true, recursive = true) end - rm(fname) - rm(tmpdir, force = true, recursive = true) + @testset "save with custom dir" begin + tmpdir = mktempdir() + fname = read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot(dir=$(repr(tmpdir))))"`, String) + @test occursin(tmpdir, fname) + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + rm(fname) + rm(tmpdir, force = true, recursive = true) + end end include("allocs.jl")