Skip to content

Commit

Permalink
Fix show of StatStruct (JuliaLang#41148)
Browse files Browse the repository at this point in the history
- Remove trailing newline
 - Make sure everything is printed to the correct IO
  • Loading branch information
fredrikekre committed Jun 9, 2021
1 parent 9ee414d commit 7ca1a90
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
27 changes: 14 additions & 13 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,37 @@ end
function show_statstruct(io::IO, st::StatStruct, oneline::Bool)
print(io, oneline ? "StatStruct(" : "StatStruct for ")
show(io, st.desc)
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " size: ", st.size, " bytes")
oneline || print("\n")
oneline || print(io, "\n")
print(io, " device: ", st.device)
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " inode: ", st.inode)
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " mode: 0o", string(filemode(st), base = 8, pad = 6), " (", filemode_string(st), ")")
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " nlink: ", st.nlink)
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " uid: $(st.uid)")
username = getusername(st.uid)
username === nothing || print(io, " (", username, ")")
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " gid: ", st.gid)
groupname = getgroupname(st.gid)
groupname === nothing || print(io, " (", groupname, ")")
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " rdev: ", st.rdev)
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " blksz: ", st.blksize)
oneline || print("\n")
oneline || print(io, "\n")
print(io, " blocks: ", st.blocks)
tnow = round(UInt, time())
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " mtime: ", iso_datetime_with_relative(st.mtime, tnow))
oneline || print("\n ")
oneline || print(io, "\n ")
print(io, " ctime: ", iso_datetime_with_relative(st.ctime, tnow))
print(io, oneline ? ")" : "\n")
oneline && print(io, ")")
return nothing
end

show(io::IO, st::StatStruct) = show_statstruct(io, st, true)
Expand Down
16 changes: 16 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1610,11 +1610,19 @@ end
f, io = mktemp()
s = stat(f)
stat_show_str = sprint(show, s)
stat_show_str_multi = sprint(show, MIME("text/plain"), s)
@test startswith(stat_show_str, "StatStruct(")
@test endswith(stat_show_str, ")")
@test startswith(stat_show_str_multi, "StatStruct for ")
@test rstrip(stat_show_str_multi) == stat_show_str_multi # no trailing \n
@test occursin(repr(f), stat_show_str)
@test occursin(repr(f), stat_show_str_multi)
if Sys.iswindows()
@test occursin("mode: 0o100666 (-rw-rw-rw-)", stat_show_str)
@test occursin("mode: 0o100666 (-rw-rw-rw-)\n", stat_show_str_multi)
else
@test occursin("mode: 0o100600 (-rw-------)", stat_show_str)
@test occursin("mode: 0o100600 (-rw-------)\n", stat_show_str_multi)
end
if Sys.iswindows() == false
@test !isnothing(Base.Filesystem.getusername(s.uid))
Expand All @@ -1623,11 +1631,19 @@ end
d = mktempdir()
s = stat(d)
stat_show_str = sprint(show, s)
stat_show_str_multi = sprint(show, MIME("text/plain"), s)
@test startswith(stat_show_str, "StatStruct(")
@test endswith(stat_show_str, ")")
@test startswith(stat_show_str_multi, "StatStruct for ")
@test rstrip(stat_show_str_multi) == stat_show_str_multi # no trailing \n
@test occursin(repr(d), stat_show_str)
@test occursin(repr(d), stat_show_str_multi)
if Sys.iswindows()
@test occursin("mode: 0o040666 (drw-rw-rw-)", stat_show_str)
@test occursin("mode: 0o040666 (drw-rw-rw-)\n", stat_show_str_multi)
else
@test occursin("mode: 0o040700 (drwx------)", stat_show_str)
@test occursin("mode: 0o040700 (drwx------)\n", stat_show_str_multi)
end
if Sys.iswindows() == false
@test !isnothing(Base.Filesystem.getusername(s.uid))
Expand Down

0 comments on commit 7ca1a90

Please sign in to comment.