Skip to content

Commit 6f003d8

Browse files
authored
Merge pull request #151 from rofinn/rf/include
Add include methods for path types
2 parents eb9895e + 9b8c02e commit 6f003d8

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FilePathsBase"
22
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
33
authors = ["Rory Finnegan"]
4-
version = "0.9.14"
4+
version = "0.9.15"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,10 @@ julia> rm(tmpfp; recursive=true)
129129

130130
julia> exists(tmpfp)
131131
false
132+
133+
# Loading code from paths (e.g., S3Path)
134+
julia> FilePathsBase.@__INCLUDE__()
135+
136+
julia> include(p"test/testpkg.jl")
137+
Main.TestPkg
132138
```

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)