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

Profile: expand heap snapshot tests #50513

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 36 additions & 9 deletions stdlib/Profile/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")