Skip to content

Commit

Permalink
Add tests about setting up the sources
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jun 11, 2020
1 parent bcaf7f0 commit 0710389
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/Prefix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,16 @@ function setup(source::SetupSource{GitSource}, targetdir, verbose)
# Chop off the `.git` at the end of the source.path
repo_dir = joinpath(targetdir, basename(source.path)[1:end-4])
if verbose
@info "Cloning $(basename(source.path)) to $(basename(targetdir))..."
# Need to strip the trailing separator
path = isdirpath(targetdir) ? dirname(targetdir) : targetdir
@info "Cloning $(basename(source.path)) to $(basename(repo_dir))..."
end
LibGit2.with(LibGit2.clone(source.path, repo_dir)) do repo
LibGit2.checkout!(repo, source.hash)
end
end

function setup(source::SetupSource{ArchiveSource}, targetdir, verbose)
function setup(source::SetupSource{ArchiveSource}, targetdir, verbose; tar_flags = verbose ? "xvof" : "xof")
mkpath(targetdir)
# Extract with host tools because it is _much_ faster on e.g. OSX.
# If this becomes a compatibility problem, we'll just have to install
Expand All @@ -277,7 +279,6 @@ function setup(source::SetupSource{ArchiveSource}, targetdir, verbose)
if verbose
@info "Extracting tarball $(basename(source.path))..."
end
tar_flags = verbose ? "xvof" : "xof"
run(`tar -$(tar_flags) $(source.path)`)
elseif endswith(source.path, ".zip")
if verbose
Expand Down
2 changes: 1 addition & 1 deletion test/prefix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ end
file = joinpath(dir, "file.txt")
file_gz = file * ".gz"
write(file, lorem)
compress_dir(dir)
@test_logs (:info, r"Compressing files in") compress_dir(dir; verbose=true)
# Check that there is only the compressed file
@test readdir(dir) == [basename(file_gz)]
# Decompress it
Expand Down
65 changes: 43 additions & 22 deletions test/sources.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Test
using BinaryBuilderBase
using BinaryBuilderBase: coerce_source, sourcify, SetupSource
using BinaryBuilderBase: coerce_source, sourcify, SetupSource, setup
using JSON

@testset "Sources" begin
Expand All @@ -16,29 +16,50 @@ using JSON
@test SetupSource("https://github.com/jedisct1/libsodium.git", "", "", "") isa SetupSource{GitSource}
@test SetupSource("https://curl.haxx.se/ca/cacert-2020-01-01.pem", "", "", "") isa SetupSource{FileSource}

@testset "Download" begin
@testset "Download and setup" begin
mktempdir() do dir
as = ArchiveSource("https://github.com/ralna/ARCHDefs/archive/v2.0.3x.tar.gz", "6583e27f84338447767bbdf4335514c8836ae4ad54f5e66280307e8b57189cff")
# Download the source
if VERSION >= v"1.4"
@test_logs (:info, r"^No hash cache found") (:info, r"Calculated hash") download_source(as; verbose = true, downloads_dir = dir)
else
@test_logs (:info, r"^Downloading") (:info, r"^No hash cache found") (:info, r"Calculated hash") download_source(as; verbose = true, downloads_dir = dir)
cd(dir) do
as = ArchiveSource("https://github.com/ralna/ARCHDefs/archive/v2.0.3x.tar.gz", "6583e27f84338447767bbdf4335514c8836ae4ad54f5e66280307e8b57189cff")
# Download the source
sas = if VERSION >= v"1.4"
@test_logs (:info, r"^No hash cache found") (:info, r"Calculated hash") download_source(as; verbose = true, downloads_dir = dir)
else
@test_logs (:info, r"^Downloading") (:info, r"^No hash cache found") (:info, r"Calculated hash") download_source(as; verbose = true, downloads_dir = dir)
end
# Check that the cache is found
@test @test_logs (:info, r"^Destination file .* already exists") (:info, r"Hash cache is consistent, returning true") download_source(as; verbose = true, downloads_dir = dir) == sas
fs = FileSource("https://github.com/ralna/ARCHDefs/archive/v2.0.3x.tar.gz", "6583e27f84338447767bbdf4335514c8836ae4ad54f5e66280307e8b57189cff"; filename = "file-source.tar.gz")
# Re-fetch the same tarball, as a `FileSource` this time
sfs = @test_logs (:info, r"^Destination file .* already exists") (:info, r"Hash cache is consistent, returning true") download_source(fs; verbose = true, downloads_dir = dir)
gs = GitSource("https://github.com/ralna/ARCHDefs.git", "fc8c5960c3a6d26970ab245241cfc067fe4ecfdd")
# Clone the repo once
sgs = @test_logs (:info, r"^Cloning") download_source(gs; verbose = true, downloads_dir = dir)
# Fetch again the repo to make sure cache works
@test @test_logs (:info, r"^Cached repository found in") download_source(gs; verbose = true, downloads_dir = dir) == sgs
# Fetch the temp directory as a `DirectorySource`
ds = DirectorySource("./bundled")
patchdir = abspath(joinpath(dir, ds.path, "patches"))
mkpath(patchdir)
write(abspath(joinpath(patchdir, "fix-windows-headers.patch")), "This is a patch file")
sds = @test_logs (:info, r"^Directory .* found") download_source(ds; verbose = true)
# Try to fetch a non-existing directory
@test_throws ErrorException download_source(DirectorySource(joinpath(dir, "does_not_exist")); verbose = true)

srcdir = joinpath(dir, "srcdir")
target = joinpath(srcdir, as.unpack_target)
@test_logs (:info, r"^Extracting tarball") setup(sas, target, true; tar_flags = "xof")
@test isdir(target)
target = joinpath(srcdir, fs.filename)
@test_logs (:info, r"^Copying") setup(sfs, target, true)
@test isfile(target)
target = joinpath(srcdir, gs.unpack_target)
@test_logs (:info, "Cloning ARCHDefs.git to ARCHDefs...") setup(sgs, target, true)
@test isdir(target)
target = abspath(joinpath(srcdir, "patches"))
@test_logs (:info, "Copying content of bundled in srcdir...") setup(sds, srcdir, true)
@test isdir(target)
@test Set(readdir(srcdir)) == Set(["ARCHDefs", "ARCHDefs-2.0.3x", fs.filename, "patches"])
end
# Check that the cache is found
@test_logs (:info, r"^Destination file .* already exists") (:info, r"Hash cache is consistent, returning true") download_source(as; verbose = true, downloads_dir = dir)
fs = FileSource("https://github.com/ralna/ARCHDefs/archive/v2.0.3x.tar.gz", "6583e27f84338447767bbdf4335514c8836ae4ad54f5e66280307e8b57189cff")
# Re-fetch the same tarball, as a `FileSource` this time
@test_logs (:info, r"^Destination file .* already exists") (:info, r"Hash cache is consistent, returning true") download_source(fs; verbose = true, downloads_dir = dir)
gs = GitSource("https://github.com/ralna/ARCHDefs", "fc8c5960c3a6d26970ab245241cfc067fe4ecfdd")
# Clone the repo once
@test_logs (:info, r"^Cloning") download_source(gs; verbose = true, downloads_dir = dir)
# Fetch again the repo to make sure cache works
@test_logs (:info, r"^Cached repository found in") download_source(gs; verbose = true, downloads_dir = dir)
# Fetch the temp directory as a `DirectorySource`
@test_logs (:info, r"^Directory .* found") download_source(DirectorySource(dir); verbose = true)
# Try to fetch a non-existing directory
@test_throws ErrorException download_source(DirectorySource(joinpath(dir, "does_not_exist")); verbose = true)
end
end

Expand Down

0 comments on commit 0710389

Please sign in to comment.