Skip to content

Commit ea5b40a

Browse files
committed
Add include methods for path types.
1 parent eb9895e commit ea5b40a

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/path.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,22 @@ systems as it should limit the number of remote calls where possible.
848848
"""
849849
diskusage(fp::AbstractPath) = isfile(fp) ? filesize(fp) : diskusage(walkpath(fp))
850850
diskusage(itr) = mapreduce(filesize, +, itr)
851+
852+
Base.include(m::Module, path::AbstractPath) = Base.include(identity, m, path)
853+
function Base.include(mapexpr::Function, m::Module, path::AbstractPath)
854+
tmp_file = cwd() / string(uuid4(), "-", basename(path))
855+
try
856+
cp(path, tmp_file; force = true)
857+
Base.include(mapexpr, m, string(tmp_file))
858+
finally
859+
rm(tmp_file; force = true)
860+
end
861+
end
862+
863+
macro __INCLUDE__()
864+
return quote
865+
m = @__MODULE__
866+
m.include(path::AbstractPath) = Base.include(identity, m, path)
867+
m.include(mapexpr::Function, path::AbstractPath) = Base.include(mapexpr, m, path)
868+
end
869+
end

src/system.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,7 @@ function canonicalize(fp::T) where T<:SystemPath
398398
end
399399

400400
Mmap.mmap(fp::SystemPath, args...; kwargs...) = Mmap.mmap(string(fp), args...; kwargs...)
401+
402+
function Base.include(mapexpr::Function, m::Module, path::SystemPath)
403+
Base.include(mapexpr, m, string(path))
404+
end

src/test.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ testsets = [
5353
test_mktmp,
5454
test_mktmpdir,
5555
test_download,
56+
test_include,
5657
]
5758
5859
# Run all the tests
@@ -120,7 +121,8 @@ module TestPaths
120121
test_mktmpdir,
121122
test_chown,
122123
test_chmod,
123-
test_download
124+
test_download,
125+
test_include
124126

125127
"""
126128
PathSet(root::AbstractPath=tmpdir(); symlink=false)
@@ -918,6 +920,14 @@ module TestPaths
918920
end
919921
end
920922

923+
function test_include(ps::PathSet)
924+
@testset "include" begin
925+
write(ps.quux, "2 + 2\n")
926+
res = include(ps.quux)
927+
@test res == 4
928+
end
929+
end
930+
921931
TESTALL = [
922932
test_registration,
923933
test_show,
@@ -970,6 +980,7 @@ module TestPaths
970980
test_chown,
971981
test_chmod,
972982
test_download,
983+
test_include,
973984
]
974985

975986
function test(ps::PathSet, test_sets=TESTALL)

test/runtests.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ using Test
88

99
using FilePathsBase.TestPaths
1010

11-
include("testpkg.jl")
11+
# Support including filepaths code
12+
FilePathsBase.@__INCLUDE__()
13+
14+
include(p"testpkg.jl")
1215

1316
@testset "FilePathsBase" begin
14-
include("mode.jl")
15-
include("buffer.jl")
16-
include("system.jl")
17+
include(p"mode.jl")
18+
include(p"buffer.jl")
19+
include(p"system.jl")
1720

1821
@static if Sys.isunix()
1922
# Test that our weird registered path works
@@ -23,7 +26,7 @@ include("testpkg.jl")
2326
@test propertynames(ps.root) == (:drive, :root, :anchor, :separator)
2427
@test propertynames(ps.root, true) == (:drive, :root, :anchor, :separator, :segments)
2528
end
26-
29+
2730
@testset "$(typeof(ps.root))" begin
2831
testsets = [
2932
test_registration,
@@ -80,6 +83,7 @@ include("testpkg.jl")
8083
test_iswritable,
8184
test_chown,
8285
test_chmod,
86+
test_include,
8387
]
8488

8589
# Run all of the automated tests

test/system.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ps = PathSet(; symlink=true)
4545
test_mktmp,
4646
test_mktmpdir,
4747
test_download,
48+
test_include,
4849
]
4950

5051
if isa(ps.root, PosixPath)
@@ -284,10 +285,10 @@ ps = PathSet(; symlink=true)
284285
@testset "User/Group constructors" begin
285286
my_user = FilePathsBase.User()
286287
my_group = FilePathsBase.Group()
287-
288+
288289
u_int = FilePathsBase.User(UInt(my_user.uid))
289290
g_int = FilePathsBase.Group(UInt(my_group.gid))
290-
291+
291292
@test u_int isa FilePathsBase.User
292293
@test g_int isa FilePathsBase.Group
293294
@test u_int.uid isa Unsigned

0 commit comments

Comments
 (0)