using Downloads, Sockets, SHA
include(joinpath(@__DIR__, "observations.jl"))
function observe_mode(opener,existing,missing)
write(existing,"ABCD")
reading,writing,initial,before,read_after = opener(existing) do io
reading,writing=isreadable(io),iswritable(io)
initial=position(io)
seekstart(io)
before=reading ? read(io,String) : nothing
if writing
seekstart(io)
write(io,"Z")
flush(io)
end
seekstart(io)
(reading,writing,initial,before,reading ? read(io,String) : nothing)
end
failure=outcome() do
opener(missing) do io
iswritable(io) && write(io,"N")
end
end
(reading,writing,initial,before,read_after,read(existing,String),failure,
isfile(missing) ? read(missing,String) : nothing)
end
function exercise_mode(root,label,mode,keywords,expected)
first=observe_mode((f,p)->open(f,p,mode),joinpath(root,label*"mode"),joinpath(root,label*"mode-new"))
second=observe_mode((f,p)->open(f,p;keywords...),joinpath(root,label*"kw"),joinpath(root,label*"kw-new"))
probe("file_mode_"*label,(mode,keywords,"ABCD and missing file; seekstart before write"),(first,second),(expected,expected))
end
function local_fixture(root)
fixture="Station 海\n"
payload=codeunits(fixture)
server=listen(ip"127.0.0.1",0)
endpoint="http://127.0.0.1:$(getsockname(server)[2])/fixture"
requests=String[]
handler=@async for _ in 1:2
conn=accept(server)
try
push!(requests,readline(conn))
while !isempty(readline(conn))
end
write(conn,"HTTP/1.1 200 OK\r\nContent-Length: $(length(payload))\r\nConnection: close\r\n\r\n")
write(conn,payload)
finally
close(conn)
end
end
try
direct=Downloads.download(endpoint,joinpath(root,"modern.txt");timeout=8)
historical=Base.download(endpoint,joinpath(root,"historical.txt"))
wait(handler)
probe("file_download","two loopback HTTP GET requests",
(requests,read(direct,String),read(historical,String),sha256(read(direct))==sha256(payload)),
(["GET /fixture HTTP/1.1","GET /fixture HTTP/1.1"],fixture,fixture,true))
finally
close(server)
end
end
mktempdir() do root
exercise_mode(root,"r","r",(;),(true,false,0,"ABCD","ABCD","ABCD","SystemError",nothing))
exercise_mode(root,"w","w",(write=true,),(false,true,0,nothing,nothing,"Z","no error","N"))
exercise_mode(root,"a","a",(append=true,),(false,true,4,nothing,nothing,"ZBCD","no error","N"))
exercise_mode(root,"rplus","r+",(read=true,write=true),(true,true,0,"ABCD","ZBCD","ZBCD","SystemError",nothing))
exercise_mode(root,"wplus","w+",(truncate=true,read=true),(true,true,0,"","Z","Z","no error","N"))
exercise_mode(root,"aplus","a+",(append=true,read=true),(true,true,4,"ABCD","ZBCD","ZBCD","no error","N"))
append_file=joinpath(root,"append.txt")
write(append_file,"ABCD")
open(append_file,"a") do io
write(io,"Z")
end
probe("file_append_at_open","no repositioning before write",read(append_file,String),"ABCDZ")
file=joinpath(root,"stations.txt")
io=open(file,"w")
bytes=write(io,"North\r\n海\nLast")
close(io)
probe("file_manual_close","write UTF-8 then close",(bytes,!isopen(io),read(file,String)),
(15,true,"North\r\n海\nLast"))
handle=Ref{IO}()
status=outcome() do
open(file,"a") do stream
handle[]=stream
write(stream,"\n")
error("test cleanup")
end
end
probe("file_do_close","append LF and throw", (status,!isopen(handle[]),read(file,String)),
("ErrorException",true,"North\r\n海\nLast\n"))
simple=joinpath(root,"copy-source.txt")
size=write(simple,"Harbor 海")
probe("file_write","Harbor 海",(size,read(simple,String),read(simple)==collect(codeunits("Harbor 海"))),
(10,"Harbor 海",true))
manual=open(file)
from_stream=read(manual,String)
close(manual)
from_do=open(f->read(f,String),file)
probe("file_read","read whole file three ways",(from_stream,from_do,read(file,String)),
("North\r\n海\nLast\n","North\r\n海\nLast\n","North\r\n海\nLast\n"))
with_lines=open(f->readlines(f;keep=true),file)
probe("file_readlines","CRLF, LF, LF",(with_lines,readlines(file)),
(["North\r\n","海\n","Last\n"],["North","海","Last"]))
streamed=open(file) do f
[(length(line),ncodeunits(line),line) for line in eachline(f)]
end
probe("file_eachline","three UTF-8 lines",streamed,[(5,5,"North"),(1,3,"海"),(4,4,"Last")])
numbered=open(file) do f
result=Tuple{Int,String}[]
line=1
while !eof(f)
push!(result,(line,readline(f)))
line+=1
end
result
end
probe("file_readline","number each line until EOF",numbered,[(1,"North"),(2,"海"),(3,"Last")])
metadata=stat(file)
probe("file_stat","station file",
(metadata.size,filesize(file),isfile(file),isdir(root),:mtime in fieldnames(typeof(metadata)),
isfinite(metadata.mtime)),(16,16,true,true,true,true))
before=pwd()
inside=cd(root) do
pwd()
end
probe("file_cd","temporarily enter exercise directory",(realpath(inside)==realpath(root),pwd()==before),(true,true))
probe("file_pwd","current project path",(isabspath(pwd()),isdir(pwd())),(true,true))
sub=joinpath(root,"sub")
mkdir(sub)
leaf=joinpath(sub,"deep","inside")
mkpath(leaf)
probe("file_readdir","sub directory and two seed files",all(name->name in readdir(root),["sub","stations.txt","copy-source.txt"]),true)
abs_names=cd(root) do
map(abspath,readdir())
end
probe("file_abspath","convert names from exercise directory",
(all(isabspath,abs_names),file in abs_names),(true,true))
probe("file_joinpath",("sub","deep","inside"),joinpath(sub,"deep","inside")==leaf,true)
probe("file_isdir","directory and regular file",(isdir(leaf),isdir(file)),(true,false))
probe("file_splitdir","stations.txt",(dirname(file)==root,splitdir(file)==(root,"stations.txt")),(true,true))
probe("file_splitdrive","Windows drive and path",(splitdrive(raw"C:\demo\sample.txt"),splitdrive("local.txt")),
((raw"C:",raw"\demo\sample.txt"),("","local.txt")))
probe("file_splitext","last extension",(splitext("archive.tar.gz"),splitext("station")), (("archive.tar",".gz"),("station","")))
probe("file_expanduser","tilde behavior",(expanduser("ordinary.txt"), Sys.iswindows() ? expanduser("~")=="~" : expanduser("~")==homedir()),
("ordinary.txt",true))
probe("file_normpath","dot and parent segments",normpath(joinpath(sub,".","..","stations.txt"))==file,true)
probe("file_realpath","existing path, including parent segments",realpath(joinpath(sub,"..","stations.txt"))==realpath(file),true)
probe("file_homedir","home directory path form without reading its contents",(isabspath(homedir()),isdir(homedir())),(true,true))
probe("file_dirname","station file",dirname(file)==root,true)
probe("file_basename","station file",basename(file),"stations.txt")
copy_path=joinpath(leaf,"copy.txt")
cp(simple,copy_path)
probe("file_cp","copy locally",(read(copy_path,String),isfile(simple)),("Harbor 海",true))
moved=joinpath(leaf,"moved.txt")
mv(copy_path,moved)
probe("file_mv","move within fixture",(isfile(copy_path),isfile(moved),read(moved,String)),(false,true,"Harbor 海"))
@assert dirname(realpath(moved))==realpath(leaf)
rm(moved)
probe("file_rm","remove one known fixture file",isfile(moved),false)
probe("file_mkdir","single and nested directories",(isdir(sub),isdir(leaf)),(true,true))
local_fixture(root)
end
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-dates-files-meta/files.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
files.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a208.zip