Skip to content

Commit

Permalink
Fix #28, handle "bare" files correctly in relocation
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Oct 7, 2023
1 parent ecbb64f commit 337eace
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/RelocatableFolders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ to a valid folder.
`Vector{Regex}`, or a single argument `Function` that takes the path and returns
`true` if it should be ignored, `false` otherwise.
"""
macro path(expr, ignore = :nothing)
macro path(expr, ignore=:nothing)
file = string(__source__.file)
dir = safe_isfile(file) ? dirname(file) : pwd()
return :($(Path)($__module__, $dir, $(esc(expr)), $(esc(ignore))))
Expand All @@ -49,7 +49,7 @@ struct Path <: AbstractString
hash::String
files::Dict{String,Vector{UInt8}}

function Path(mod::Module, dir, path::AbstractString, ignore = nothing)
function Path(mod::Module, dir, path::AbstractString, ignore=nothing)
path = isabspath(path) ? path : joinpath(dir, path)
path = normpath(path)
safe_ispath(path) || throw(ArgumentError("not a path: `$path`"))
Expand Down Expand Up @@ -88,7 +88,14 @@ Base.iterate(f::Path, state::Integer) = iterate(getpath(f), state)
Base.String(f::Path) = String(getpath(f))

function getpath(f::Path)
safe_ispath(f.path) && return getroot(f)
if safe_ispath(f.path)
root = getroot(f)
# Confirm whether what's actually been returned as the root path is
# valid, when it isn't then provide the relocated path instead.
if safe_ispath(root)
return root
end
end
dir = Scratch.get_scratch!(f.mod, f.hash * "_" * string(hash(f.path), base=62))
if !isempty(f.files) && !safe_ispath(joinpath(dir, first(keys(f.files))))
cd(dir) do
Expand All @@ -113,6 +120,6 @@ If `p` corresponds to a file `filename`, return `joinpath(root, filename)`.
This function is mostly useful to get information about `p` (e.g., its extension)
without interacting with the filesystem.
"""
getroot(p::Path, root = p.path) = p.is_dir ? root : joinpath(root, first(keys(p.files)))
getroot(p::Path, root=p.path) = p.is_dir ? root : joinpath(root, first(keys(p.files)))

end # module
1 change: 1 addition & 0 deletions test/bare-file.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# bare file.jl
13 changes: 12 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module M

using RelocatableFolders

const BARE_FILE = @path "bare-file.jl"
const DIR = @path "path"
const FILE = @path joinpath("path", "file.jl")
const OTHER = @path joinpath(DIR, "subfolder/other.jl") # issue 8
Expand All @@ -17,6 +18,9 @@ end

@testset "RelocatableFolders" begin
tests = function ()
@test isfile(M.BARE_FILE)
@test read(M.BARE_FILE, String) == "# bare file.jl"

@test isfile(M.FILE)
@test read(M.FILE, String) == "# file.jl"

Expand Down Expand Up @@ -48,6 +52,7 @@ end
@test !haskey(M.IGNORE_FN_REL.files, joinpath("path", "file.jl"))
end
from, to = joinpath.(Ref(@__DIR__), ("path", "moved"))
file_from, file_to = joinpath.(Ref(@__DIR__), ("bare-file.jl", "moved-bare-file.jl"))
try
tests()
@test String(M.DIR) == joinpath(@__DIR__, "path")
Expand All @@ -56,12 +61,17 @@ end
# Remove the referenced folder `DIR`.
@test isdir(from)
@test !isdir(to)
@test isfile(file_from)
@test !isfile(file_to)
mv(from, to)
mv(file_from, file_to)
@test isdir(to)
@test !isdir(from)
@test isfile(file_to)
@test !isfile(file_from)

let path = String(M.DIR)
rm(path; recursive = true)
rm(path; recursive=true)
@test !isdir(path)
end
let file = String(M.FILE)
Expand Down Expand Up @@ -103,5 +113,6 @@ end
@test_throws(ErrorException, @path("path", 123))
finally
isdir(from) || mv(to, from)
isfile(file_from) || mv(file_to, file_from)
end
end

0 comments on commit 337eace

Please sign in to comment.