Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Writer is compatible with other zip extractors. #102

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
matrix:
- julia_version: 1.3
- julia_version: 1.6
- julia_version: nightly

platform:
Expand Down
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ task:
name: FreeBSD
env:
matrix:
- JULIA_VERSION: 1.3
- JULIA_VERSION: 1.6
- JULIA_VERSION: nightly
allow_failures: $JULIA_VERSION == 'nightly'
install_script:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ doc/_build/
deps/deps.jl
build.log
deps/usr/*
Manifest.toml
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ os:
- osx

julia:
- 1.3
- 1.6
- nightly

matrix:
Expand Down
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ Zlib_jll = "83775a58-1f1d-513f-b197-d71354ab007a"

[compat]
Zlib_jll = "1.2.11"
julia = "1.3.0"
julia = "1.6.0"

[extras]
LibArchive_jll = "1e303b3e-d4db-56ce-88c4-91e52606a1a8"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
p7zip_jll = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"

[targets]
test = ["Test"]
test = ["LibArchive_jll", "PyCall", "Test", "p7zip_jll"]
84 changes: 84 additions & 0 deletions test/external_unzippers.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Used to test that zip files written by ZipFile.jl can be read by other programs.
# This defines a vector of functions in `unzippers`
# These functions take a zipfile path and a directory path and extract the zipfile into the directory
using Test
using ZipFile
import p7zip_jll
import LibArchive_jll
import PyCall



"""
Extract the zip file at zippath into the directory dirpath
Use p7zip
"""
function unzip_p7zip(zippath, dirpath)
# pipe output to devnull because p7zip is noisy
p7zip_jll.p7zip() do exe
run(pipeline(`$(exe) x -y -o$(dirpath) $(zippath)`, devnull))
end
nothing
end

"""
Extract the zip file at zippath into the directory dirpath
Use bsdtar from libarchive
"""
function unzip_bsdtar(zippath, dirpath)
LibArchive_jll.bsdtar() do exe
run(`$(exe) -x -f $(zippath) -C $(dirpath)`)
end
nothing
end

"""
Extract the zip file at zippath into the directory dirpath
Use zipfile.py from python standard library
"""
function unzip_python(zippath, dirpath)
zipfile = PyCall.pyimport("zipfile")
PyCall.@pywith zipfile.ZipFile(zippath) as f begin
isnothing(f.testzip()) || error(string(f.testzip()))
f.extractall(dirpath)
end
nothing
end


# This is modified to only check for `unzip` from
# https://github.com/samoconnor/InfoZIP.jl/blob/1247b24dd3183e00baa7890c1a2c7f6766c3d774/src/InfoZIP.jl#L6-L14
function have_infozip()
try
occursin(r"^UnZip.*by Info-ZIP", read(`unzip`, String))
catch
return false
end
end

"""
Extract the zip file at zippath into the directory dirpath
Use unzip from the infamous builtin Info-ZIP
"""
function unzip_infozip(zippath, dirpath)
try
run(`unzip -qq $(zippath) -d $(dirpath)`)
catch
# unzip errors if the zip file is empty for some reason
end
nothing
end


unzippers = [
unzip_p7zip,
unzip_bsdtar,
unzip_python,
]

if have_infozip()
push!(unzippers, unzip_infozip)
else
@info "system Info-ZIP unzip not found, skipping `unzip_infozip` tests"
end

31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ r = ZipFile.Reader(filename)
close(r)
close(dir)

# This defines a vector of functions in `unzippers`
# These functions take a zipfile path and a directory path
# They extract the zipfile into the directory
include("external_unzippers.jl")

@testset "Writer compat with $(unzipper)" for unzipper in unzippers
for filename in readdir(tmp)
endswith(filename, ".zip") || continue
zippath = joinpath(tmp, filename)
mktempdir() do tmpout
# Unzip into an output directory
unzipper(zippath, tmpout)
# Read zippath with ZipFile.Reader
# Check file names and data match
local dir = ZipFile.Reader(zippath)
for f in dir.files
local name = f.name
local extracted_path = joinpath(tmpout, name)
@test isfile(extracted_path)
@test read(f) == read(extracted_path)
end
# Check number of extracted files match
local total_files = sum(walkdir(tmpout)) do (root, dirs, files)
length(files)
end
@test length(dir.files) == total_files
close(dir)
end
end
end


if !Debug
rm(tmp, recursive=true)
Expand Down