Skip to content

Commit f46c631

Browse files
Fix ENV usage (#305)
* Trim trailing whitespace * Fix ENV usage Also fix wrong default of DRWATSON_STOREPATCH in docs. * Add test for tag!() based on ENV * Fix CI Apparently, GitHub CI doesn't expect it's git to perform a commit, so it doesn't have user.name configured. Let's hope this makes CI happy. * Add documentation for readenv()
1 parent 0bf95a4 commit f46c631

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

src/DrWatson.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ const PATH_SEPARATOR = joinpath("_", "_")[2]
77
convert_to_kw(ex::Expr) = Expr(:kw,ex.args...)
88
convert_to_kw(ex) = error("invalid keyword argument syntax \"$ex\"")
99

10+
# Misc helpers
11+
"""
12+
readenv(var, default::T) where {T}
13+
14+
Try to read the environment variable `var` and parse it as a `::T`.
15+
If that fails, return `default`.
16+
"""
17+
readenv(var, default::T) where {T} = something(tryparse(T, get(ENV, var, "")), Some(default))
18+
1019
# Pure Julia implementation
1120
include("project_setup.jl")
1221
include("naming.jl")
@@ -68,7 +77,7 @@ function __init__()
6877
It showcases how to eliminate code duplication and streamline your simulation setup
6978
and run phase using `savename` and `produce_or_load`.
7079
* By default now `gitpatch` is NOT saved when calling `tag!` and derivative functions.
71-
This is due to an unknown problem that causes collecting the git patch to
80+
This is due to an unknown problem that causes collecting the git patch to
7281
never halt, potentially not saving a user's output.
7382
\n
7483
"""; color = :light_magenta)

src/saving_files.jl

Lines changed: 6 additions & 6 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 = DrWatson.readenv("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,9 +43,9 @@ 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 = readenv("DRWATSON_TAG", istaggable(suffix)),
4747
gitpath = projectdir(), loadfile = true,
48-
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
48+
storepatch::Bool = readenv("DRWATSON_STOREPATCH", false),
4949
force = false, verbose = true, wsave_kwargs = Dict(),
5050
kwargs...
5151
)
@@ -137,13 +137,13 @@ Keywords `gitpath, storepatch, force,` are propagated to [`tag!`](@ref).
137137
Any additional keyword arguments are propagated to `wsave`, to e.g.
138138
enable compression.
139139
140-
The keyword `safe = get(ENV, "DRWATSON_SAFESAVE", false)` decides whether
140+
The keyword `safe = DrWatson.readenv("DRWATSON_SAFESAVE", false)` decides whether
141141
to save the file using [`safesave`](@ref).
142142
"""
143143
function tagsave(file, d;
144144
gitpath = projectdir(),
145-
safe::Bool = get(ENV, "DRWATSON_SAFESAVE", false),
146-
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
145+
safe::Bool = readenv("DRWATSON_SAFESAVE", false),
146+
storepatch::Bool = readenv("DRWATSON_STOREPATCH", false),
147147
force = false, source = nothing, kwargs...
148148
)
149149
d2 = tag!(d, gitpath=gitpath, storepatch=storepatch, force=force, source=source)

src/saving_tools.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ To restore a repository to the state of a particular model-run do:
181181
## Keywords
182182
* `gitpath = projectdir()`
183183
* `force = false`
184-
* `storepatch = get(ENV, "DRWATSON_STOREPATCH", true)`: Whether to collect and store the
184+
* `storepatch = DrWatson.readenv("DRWATSON_STOREPATCH", false)`: Whether to collect and store the
185185
output of [`gitpatch`](@ref) as well.
186186
187187
## Examples
@@ -198,9 +198,9 @@ Dict{Symbol,Any} with 3 entries:
198198
:x => 3
199199
```
200200
"""
201-
function tag!(d::AbstractDict{K,T};
201+
function tag!(d::AbstractDict{K,T};
202202
gitpath = projectdir(), force = false, source = nothing,
203-
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
203+
storepatch::Bool = readenv("DRWATSON_STOREPATCH", false),
204204
) where {K,T}
205205
@assert (K <: Union{Symbol,String}) "We only know how to tag dictionaries that have keys that are strings or symbols"
206206
c = gitdescribe(gitpath)

test/stools_tests.jl

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DrWatson, Test
22
using DataStructures
33
using JLD2
4+
using LibGit2
45

56
# Test commit function
67
com = gitdescribe(@__DIR__)
@@ -12,30 +13,70 @@ com = gitdescribe(dirname(@__DIR__))
1213
# TODO: why?
1314
# @test com[1] == 'v' # test that it has a version tag
1415

15-
# Test isdirty.
16-
if isdirty(@__DIR__)
17-
@test endswith(gitdescribe(@__DIR__), "_dirty")
18-
else
19-
@test !endswith(gitdescribe(@__DIR__), "_dirty")
16+
# Set up a clean and a dirty repo.
17+
function _setup_repo(dirty)
18+
path = mktempdir(cleanup=true) # delete path on process exit
19+
repo = LibGit2.init(path)
20+
john = LibGit2.Signature("Dr. John H. Watson", "snail mail only")
21+
write(joinpath(path, "foo.txt"), "bar\n")
22+
LibGit2.add!(repo, "foo.txt")
23+
LibGit2.commit(repo, "tmp repo commit", author=john, committer=john)
24+
dirty && write(joinpath(path, "foo.txt"), "baz\n")
25+
return path
2026
end
27+
dpath = _setup_repo(true) # dirty
28+
cpath = _setup_repo(false) # clean
29+
30+
@test isdirty(dpath)
31+
@test endswith(gitdescribe(dpath), "_dirty")
32+
@test !isdirty(cpath)
33+
@test !endswith(gitdescribe(cpath), "_dirty")
2134

2235
# tag!
36+
function _test_tag!(d, path, haspatch, DRWATSON_STOREPATCH)
37+
d = copy(d)
38+
withenv("DRWATSON_STOREPATCH" => DRWATSON_STOREPATCH) do
39+
d = tag!(d, gitpath=path)
40+
commitname = keytype(d)(:gitcommit)
41+
@test haskey(d, commitname)
42+
@test d[commitname] isa String
43+
if haspatch
44+
patchname = keytype(d)(:gitpatch)
45+
@test haskey(d, patchname)
46+
@test d[patchname] isa String
47+
@test d[patchname] != ""
48+
end
49+
end
50+
end
51+
2352
d1 = Dict(:x => 3, :y => 4)
2453
d2 = Dict("x" => 3, "y" => 4)
25-
for d in (d1, d2)
26-
d = tag!(d, gitpath=@__DIR__)
27-
28-
@test haskey(d, keytype(d)(:gitcommit))
29-
@test d[keytype(d)(:gitcommit)] |> typeof <: String
54+
@testset "tag! ($(keytype(d)))" for d in (d1, d2)
55+
@testset "no patch ($(dirty ? "dirty" : "clean"))" for dirty in (true, false)
56+
path = dirty ? dpath : cpath
57+
_test_tag!(d, path, false, nothing) # variable unset
58+
_test_tag!(d, path, false, "") # variable set but empty
59+
_test_tag!(d, path, false, "false") # variable parses as false
60+
_test_tag!(d, path, false, "0") # variable parses as false
61+
_test_tag!(d, path, false, "rubbish") # variable not a Bool
62+
end
63+
@testset "patch" begin
64+
_test_tag!(d, dpath, true, "true") # variable parses as true
65+
_test_tag!(d, dpath, true, "1") # variable parses as true
66+
end
3067
end
3168

69+
# Ensure that above tests operated out-of-place.
70+
@test d1 == Dict(:x => 3, :y => 4)
71+
@test d2 == Dict("x" => 3, "y" => 4)
72+
3273
# Test assertion error when the data has a incompatible key type
3374
@test_throws AssertionError("We only know how to tag dictionaries that have keys that are strings or symbols") tag!(Dict{Int64,Any}(1 => 2))
3475
@test_throws AssertionError("We only know how to tag dictionaries that have keys that are strings or symbols") DrWatson.scripttag!(Dict{Int64,Any}(1 => 2), "foo")
3576

3677

3778
# @tag!
38-
for d in (d1, d2)
79+
@testset "@tag! ($(keytype(d)))" for d in (d1, d2)
3980
d = @tag!(d, gitpath=@__DIR__)
4081
@test haskey(d, keytype(d)(:gitcommit))
4182
@test d[keytype(d)(:gitcommit)] |> typeof <: String
@@ -318,7 +359,7 @@ rm(tmpdir, force = true, recursive = true)
318359
n = @ntuple x y
319360
@test isa(ntuple2dict(n),Dict)
320361
@test isa(ntuple2dict(OrderedDict,n),OrderedDict)
321-
362+
322363
#test checktagtype!
323364
@test isa(DrWatson.checktagtype!(d3),Dict)
324365
@test isa(DrWatson.checktagtype!(d11),OrderedDict)

0 commit comments

Comments
 (0)