Skip to content

Commit 0b1a098

Browse files
Fix recursively_clear_path for directories (#303)
* Fix recursively_clear_path for directories * Trim trailing whitespace * Bump version
1 parent 9986472 commit 0b1a098

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "2.7.3"
4+
version = "2.7.4"
55

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

src/saving_files.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424
2525
## Keywords
2626
* `suffix = "jld2", prefix = default_prefix(config)` : Used in [`savename`](@ref).
27-
* `tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix))` : Save the file
27+
* `tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix))` : Save the file
2828
using [`tagsave`](@ref) if `true` (which is the default).
2929
* `gitpath, storepatch` : Given to [`tagsave`](@ref) if `tag` is `true`.
3030
* `force = false` : If `true` then don't check if file `s` exists and produce
@@ -43,10 +43,10 @@ produce_or_load(f::Function, c; kwargs...) = produce_or_load(c, f; kwargs...)
4343
produce_or_load(f::Function, path, c; kwargs...) = produce_or_load(path, c, f; kwargs...)
4444
function produce_or_load(path, c, f::Function;
4545
suffix = "jld2", prefix = default_prefix(c),
46-
tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix)),
46+
tag::Bool = get(ENV, "DRWATSON_TAG", istaggable(suffix)),
4747
gitpath = projectdir(), loadfile = true,
48-
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
49-
force = false, verbose = true, wsave_kwargs = Dict(),
48+
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
49+
force = false, verbose = true, wsave_kwargs = Dict(),
5050
kwargs...
5151
)
5252

@@ -140,10 +140,10 @@ enable compression.
140140
The keyword `safe = get(ENV, "DRWATSON_SAFESAVE", false)` decides whether
141141
to save the file using [`safesave`](@ref).
142142
"""
143-
function tagsave(file, d;
144-
gitpath = projectdir(),
145-
safe::Bool = get(ENV, "DRWATSON_SAFESAVE", false),
146-
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
143+
function tagsave(file, d;
144+
gitpath = projectdir(),
145+
safe::Bool = get(ENV, "DRWATSON_SAFESAVE", false),
146+
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
147147
force = false, source = nothing, kwargs...
148148
)
149149
d2 = tag!(d, gitpath=gitpath, storepatch=storepatch, force=force, source=source)
@@ -222,9 +222,9 @@ end
222222

223223
#recursively move files to increased backup number
224224
function recursively_clear_path(cur_path)
225-
isfile(cur_path) || return
225+
ispath(cur_path) || return
226226
new_path=increment_backup_num(cur_path)
227-
if isfile(new_path)
227+
if ispath(new_path)
228228
recursively_clear_path(new_path)
229229
end
230230
mv(cur_path, new_path)

test/savefiles_tests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,24 @@ end
252252
rm("test.#backup_#1."*ending)
253253
rm("test.#backup_#2."*ending)
254254
end
255+
256+
@testset "Backup (dir)" begin
257+
# Save contents as individual file(s) within a parent directory:
258+
struct Composite x end
259+
DrWatson._wsave(dir, data::Composite) = wsave(joinpath(dir, "x.jld2"), data.x)
260+
load_composite(dir) = load(joinpath(dir, "x.jld2"))
261+
262+
filepath = "test.#backup.dir"
263+
data = [Composite(Dict( "a" => i, "b" => rand(rand(1:10)))) for i = 1:3]
264+
for i = 1:3
265+
safesave(filepath, data[i])
266+
@test isdir(filepath)
267+
@test data[i].x == load_composite(filepath)
268+
end
269+
@test data[2].x == load_composite("test.#backup_#1.dir")
270+
@test data[1].x == load_composite("test.#backup_#2.dir")
271+
@test data[3].x == load_composite("test.#backup.dir")
272+
rm("test.#backup.dir"; recursive=true)
273+
rm("test.#backup_#1.dir"; recursive=true)
274+
rm("test.#backup_#2.dir"; recursive=true)
275+
end

0 commit comments

Comments
 (0)