Skip to content

Commit

Permalink
Removing double prefixes bug in produce_or_load (#393)
Browse files Browse the repository at this point in the history
* Removing double prefixes bug in produce_or_load

* correcting test when default_prefix was modified

* Correcting produce_or_load: filename as function

* Update Project.toml

* Update CHANGELOG.md

---------

Co-authored-by: George Datseris <[email protected]>
  • Loading branch information
pjgorski and Datseris authored Jul 3, 2023
1 parent ab87902 commit 506109c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.12.6
- Crucial bugfix to `produce_or_load`. When used with a prefix, it attached double prefix to the file (one coming as a duplicate from `savename`). This is now fixed, but it means that some files produced with prefix and `produce_or_load` in v2.12 may be re-produced after this update.

# 2.12.0
- Arbitrary functions extracting strings from data can be used in `produce_or_load` instead of `savename`. `hash` is the most useful function here. An example in Real World Examples highlights this.
- Additional keywords in `produce_or_load` propagated to `savename` are deprecated.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DrWatson"
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
version = "2.12.5"
version = "2.12.6"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
14 changes: 7 additions & 7 deletions src/saving_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ function produce_or_load(f::Function, config, path::String = "";
for the keyword `filename` as `filename = config -> savename(config; kwargs...)`
"""
end
if filename === nothing
filename = config -> savename(config; kwargs...)
end
# Prepare absolute file name
if filename isa AbstractString
name = filename
else
if filename === nothing
filename = config -> savename(prefix, config, suffix; kwargs...)
name = filename(config)
elseif filename isa AbstractString
name = append_prefix_suffix(filename, prefix, suffix)
else #if filename isa Function
name = string(filename(config))
name = append_prefix_suffix(name, prefix, suffix)
end
name = append_prefix_suffix(name, prefix, suffix)
file = joinpath(path, name)
# Run the remaining logic on whether to produce or load
if !force && isfile(file)
Expand Down
17 changes: 17 additions & 0 deletions test/savefiles_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ end
rm(path; recursive = true, force = true)
end

# Testing proper filenames when default_prefix was modified. See https://github.com/JuliaDynamics/DrWatson.jl/issues/392
@testset "@produce_or_load with default_prefix modified" begin
path = mktempdir()

struct Dummy
x
y
end
simulation = Dummy(1,2)
DrWatson.default_prefix(d::Dummy) = "Prefix_"

sim, path = produce_or_load(f, simulation, "")
@test path == savename(simulation, "jld2")

rm(path)
DrWatson.default_prefix(ntuple::NamedTuple) = ""
end


################################################################################
Expand Down

0 comments on commit 506109c

Please sign in to comment.