Skip to content

Commit c106b1f

Browse files
Check for path errors eagerly during load/save (#263) (#265)
* add missing gzip test file (and parent csv file) * add eager checks for file and path existence * create dummy file for multierr test * add tests for path errors * mkpath eagerly during save if dir doesn't exist
1 parent 882dc75 commit c106b1f

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/loadsave.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ for fn in (:load, :loadstreaming, :metadata)
167167
isfile(filename(q)) || open(filename(q)) # force systemerror
168168
throw(UnknownFormat(q))
169169
end
170+
if q isa File
171+
!isfile(filename(q)) && throw(ArgumentError("No file exists at given path: $(filename(q))"))
172+
end
170173
libraries = applicable_loaders(q)
171174
failures = Any[]
172175
for library in libraries
@@ -191,6 +194,10 @@ for fn in (:save, :savestreaming)
191194
gen2_func_name = Symbol("fileio_", fn)
192195
@eval function $fn(@nospecialize(q::Formatted), @nospecialize(data...); @nospecialize(options...))
193196
unknown(q) && throw(UnknownFormat(q))
197+
if q isa File
198+
isdir(filename(q)) && throw(ArgumentError("Given file path is a directory: $(filename(q))"))
199+
!isdir(dirname(filename(q))) && mkpath(dirname(filename(q)))
200+
end
194201
libraries = applicable_savers(q)
195202
failures = Any[]
196203
for library in libraries

test/error_handling.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
println("these tests will print warnings: ")
22

3+
module PathError
4+
import FileIO: File, @format_str
5+
save(file::File{format"PATHERROR"}, data) = nothing
6+
load(file::File{format"PATHERROR"}) = nothing
7+
end
8+
add_format(format"PATHERROR", (), ".patherror", [:PathError])
9+
10+
@testset "Path errors" begin
11+
# handling a nonexistent parent directory, during save
12+
temp_dir = joinpath(mktempdir(), "dir_that_did_not_exist")
13+
@assert !isdir(temp_dir) "Testing error. This dir shouldn't exist"
14+
fn = joinpath(temp_dir, "file.patherror")
15+
save(fn, "test content")
16+
@test isdir(temp_dir)
17+
18+
# handling a filepath that's an existing directory, during save
19+
@test_throws ArgumentError save(format"PATHERROR", mktempdir(), "test content")
20+
21+
# handling a nonexistent filepath, during load
22+
@test_throws ArgumentError load(joinpath(mktempdir(), "dummy.patherror"))
23+
end
24+
325
@testset "Not installed" begin
426
add_format(format"NotInstalled", (), ".not_installed", [:NotInstalled])
527
@test_throws ArgumentError save("test.not_installed", nothing)
@@ -60,6 +82,10 @@ end
6082
[:MultiError1],
6183
[:MultiError2]
6284
)
63-
ret = @test_throws ErrorException load("test.multierr")
85+
tmpfile = joinpath(mktempdir(), "test.multierr")
86+
open(tmpfile, "w") do io
87+
println(io, "dummy content")
88+
end
89+
ret = @test_throws ErrorException load(tmpfile)
6490
#@test ret.value.msg == "1" # this is 0.5 only
6591
end

test/files/file.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dummy,data
2+
0,1

test/files/file.csv.gz

43 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)