Skip to content

Commit 9d8a2c6

Browse files
committed
fix _max_appbundle_dir_size
1 parent 522c53e commit 9d8a2c6

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

src/utils.jl

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,55 @@ function Base.show(io::IO, filehash::FileHash)
304304
end
305305

306306
# Estimates the size of the bundle directory
307-
function _max_appbundle_dir_size(dir; maxsize=100 * 1024 * 1024)
307+
function _max_appbundle_dir_size(dir::AbstractString; maxsize=100 * 1024 * 1024)
308308
sz = 0
309+
_walk_appbundle_files(dir) do filepath
310+
if sz > maxsize
311+
return _WalkFilesReturnEarly()
312+
end
313+
sz += filesize(filepath)
314+
end
315+
return sz, sz <= maxsize
316+
end
317+
318+
function _walk_appbundle_files(f::Base.Callable, dir::AbstractString)
319+
# Note: even if if `path_filterer` says that directory `foo/bar`
320+
# should not be included, it will still likely return `true` for
321+
# any files in there, like `foo/bar/baz`. So we need to make sure
322+
# we stop recursing into subdirectories.
309323
pred = _PackageBundler.path_filterer(dir)
310-
for (root, _, files) in walkdir(dir)
311-
for file in files
312-
file = joinpath(root, file)
313-
if !pred(file)
314-
@debug "ignoring $file in dir size measurement"
315-
continue
316-
end
324+
_walkfiles(dir; descend=pred) do filepath
325+
if !pred(filepath)
326+
@debug "ignoring file in _walk_appbundle_files: $(file)"
327+
return nothing
328+
end
329+
return f(filepath)
330+
end
331+
end
332+
333+
struct _WalkFilesReturnEarly end
317334

318-
sz > maxsize && return sz, false
319-
sz += filesize(file)
335+
# Calls `f` on any non-directory in `root`.
336+
# `descend` gets called on any directory, and if it returns false,
337+
function _walkfiles(f::Base.Callable, root::AbstractString; descend::Base.Callable)
338+
if !isdir(root)
339+
error("Not a directory: $(root)")
340+
end
341+
directories = String[root]
342+
while !isempty(directories)
343+
dir = popfirst!(directories)
344+
for subpath in readdir(dir; join=true)
345+
if isdir(subpath)
346+
if descend(subpath)::Bool
347+
push!(directories, subpath)
348+
end
349+
else
350+
if f(subpath) === _WalkFilesReturnEarly()
351+
break
352+
end
353+
end
320354
end
321355
end
322-
return sz, sz < maxsize
323356
end
324357

325358
function _json_get(d::Dict, key, ::Type{T}; var::AbstractString, parse=false) where {T}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
000000000000000000000000000000000000000000000000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
000000000000000000000000000000000000000000000000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
000000000000000000000000000000000000000000000000

test/utils.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,23 @@ end
101101
Dict("_id_missing" => "123e4567-e89b-12d3-a456-426614174000"), "id", UUIDs.UUID
102102
)
103103
end
104+
105+
@testset "_max_appbundle_dir_size" begin
106+
# We check here that the `.juliabundleignore` is honored by making
107+
# sure that the calculated total file size of the Pkg3/ directory is
108+
dir = joinpath(@__DIR__, "fixtures", "ignorefiles", "Pkg3")
109+
110+
appbundle_files = String[]
111+
JuliaHub._walk_appbundle_files(dir) do filepath
112+
push!(appbundle_files, relpath(filepath, dir))
113+
end
114+
@test sort(appbundle_files) == [
115+
".gitignore", ".juliabundleignore", "Project.toml", "README.md",
116+
joinpath("src", "Pkg3.jl"), joinpath("src", "bar"), joinpath("src", "fooo"),
117+
joinpath("test", "fooo", "test"), joinpath("test", "runtests.jl"),
118+
]
119+
120+
# The files that are not meant to be included in the /Pkg3/ bundle here are
121+
# all 50 byte files. Should they should show up in the total size here.
122+
@test JuliaHub._max_appbundle_dir_size(dir) == (405, true)
123+
end

0 commit comments

Comments
 (0)