Skip to content

Commit

Permalink
Add download tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Apr 6, 2017
1 parent de8bd9a commit 96dd5ab
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/tzdata/download.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ function tzdata_download(version::AbstractString="latest", dir::AbstractString=t
if version == "latest"
v = latest_version(now_utc)
if !isnull(v)
return joinpath(dir, "tzdata$(unsafe_get(v)).tar.gz")
archive = joinpath(dir, "tzdata$(unsafe_get(v)).tar.gz")
isfile(archive) && return archive
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/helpers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Utility functions for testing

function ignore_output(body::Function; stdout::Bool=true, stderr::Bool=true)
out_old = STDOUT
err_old = STDERR

if stdout
(out_rd, out_wr) = redirect_stdout()
end
if stderr
(err_rd, err_wr) = redirect_stderr()
end

result = body()

if stdout
redirect_stdout(out_old)
close(out_wr)
close(out_rd)
end
if stderr
redirect_stderr(err_old)
close(err_wr)
close(err_rd)
end

return result
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ for name in TEST_REGIONS
tzdata[name] = tzparse(joinpath(TZ_SOURCE_DIR, name))
end

include("helpers.jl")

include("utils.jl")
include(joinpath("tzdata", "timeoffset.jl"))
include(joinpath("tzdata", "archive.jl"))
include(joinpath("tzdata", "version.jl"))
include(joinpath("tzdata", "download.jl"))
include(joinpath("tzdata", "compile.jl"))
include("utcoffset.jl")
include("types.jl")
Expand Down
24 changes: 24 additions & 0 deletions test/tzdata/download.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import TimeZones.TZData: tzdata_url, tzdata_download, isarchive

@test tzdata_url("2016j") == "https://www.iana.org/time-zones/repository/releases/tzdata2016j.tar.gz"
@test tzdata_url("latest") == "https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz"

# Note: Try to keep the number of `tzdata_download` calls low to avoid unnecessary network traffic
mktempdir() do temp_dir
file_path = ignore_output() do
tzdata_download("latest", temp_dir)
end

@test isfile(file_path)
@test isarchive(file_path)
@test basename(file_path) != basename(tzdata_url("latest"))

last_modified = mtime(file_path)
last_file_path = file_path

# No need to ignore output as this should never trigger a download
file_path = tzdata_download("latest", temp_dir)

@test file_path == last_file_path
@test mtime(file_path) == last_modified
end

0 comments on commit 96dd5ab

Please sign in to comment.