Skip to content

Commit a2a9d65

Browse files
authored
Merge pull request #425 from svilupp/js/fix-tofile-path
Update path splitting rule in Dagger.File / Dagger.tofile
2 parents 04e2466 + 876407b commit a2a9d65

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/file-io.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ function File(path::AbstractString;
3030
# FIXME: Once MemPool better propagates errors, this check won't be necessary
3131
throw(ArgumentError("`Dagger.File` expected file to exist at \"$path\""))
3232
end
33-
if isabspath(path)
34-
dir, file = dirname(path), basename(path)
35-
else
36-
dir, file = pwd(), basename(path)
37-
end
33+
dir, file = dirname(path), basename(path)
34+
# if dir is empty, use the current working directory
35+
dir = isempty(dir) ? pwd() : dir
3836
Tdevice = GenericFileDevice{serialize, deserialize, use_io, mmap}
3937
device = Tdevice(dir)
4038
leaf_tag = MemPool.Tag(Tdevice=>file)
@@ -61,11 +59,9 @@ function tofile(@nospecialize(data), path::AbstractString;
6159
deserialize::Base.Callable=deserialize,
6260
use_io::Bool=true,
6361
mmap::Bool=false)
64-
if isabspath(path)
65-
dir, file = dirname(path), basename(path)
66-
else
67-
dir, file = pwd(), basename(path)
68-
end
62+
dir, file = dirname(path), basename(path)
63+
# if dir is empty, use the current working directory
64+
dir = isempty(dir) ? pwd() : dir
6965
Tdevice = GenericFileDevice{serialize, deserialize, use_io, mmap}
7066
device = Tdevice(dir)
7167
chunk = Dagger.tochunk(data, OSProc(), ProcessScope();

test/file-io.jl

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,41 @@ using Serialization
33
@testset "File IO" begin
44
@testset "File" begin
55
data = [1,2,3]
6-
data_path = joinpath(tempdir(), "jl_" * join(rand('a':'z', 8)) * ".jls")
7-
atexit() do
8-
@assert isfile(data_path)
9-
rm(data_path)
10-
end
11-
serialize(data_path, data)
6+
# test both absolute and relative paths
7+
dir_abs = tempdir()
8+
dir_rel = splitpath(mktempdir(pwd()))[end]
9+
for dir in [dir_abs, dir_rel]
10+
data_path = joinpath(dir, "jl_" * join(rand('a':'z', 8)) * ".jls")
11+
atexit() do
12+
@assert isfile(data_path)
13+
rm(data_path)
14+
end
15+
serialize(data_path, data)
1216

13-
data_c = Dagger.File(data_path)::Dagger.File
14-
@test fetch(data_c) == data
15-
@test fetch(Dagger.@spawn identity(data_c)) == data
17+
data_c = Dagger.File(data_path)::Dagger.File
18+
@test fetch(data_c) == data
19+
@test fetch(Dagger.@spawn identity(data_c)) == data
1620

17-
@test isfile(data_path)
21+
@test isfile(data_path)
22+
end
1823
end
1924
@testset "tofile" begin
2025
data = [4,5,6]
21-
data_path = joinpath(tempdir(), "jl_" * join(rand('a':'z', 8)) * ".jls")
22-
atexit() do
23-
@assert isfile(data_path)
24-
rm(data_path)
25-
end
26+
# test both absolute and relative paths
27+
dir_abs = tempdir()
28+
dir_rel = splitpath(mktempdir(pwd()))[end]
29+
for dir in [dir_abs, dir_rel]
30+
data_path = joinpath(dir, "jl_" * join(rand('a':'z', 8)) * ".jls")
31+
atexit() do
32+
@assert isfile(data_path)
33+
rm(data_path)
34+
end
2635

27-
data_c = Dagger.tofile(data, data_path)::Dagger.File
28-
@test fetch(data_c) == data
29-
@test fetch(Dagger.@spawn identity(data_c)) == data
36+
data_c = Dagger.tofile(data, data_path)::Dagger.File
37+
@test fetch(data_c) == data
38+
@test fetch(Dagger.@spawn identity(data_c)) == data
3039

31-
@test isfile(data_path)
40+
@test isfile(data_path)
41+
end
3242
end
3343
end

0 commit comments

Comments
 (0)