erlang - What is the best way to assert the contents of a zip archive in Elixir? -
currently doing:
- testing function letting zip file/directory. assert exists.
- using
:zip.t
,:zip.tt
let list down contents of zip folder see if it's expecting.
somehow think missing something. better test :zip.table
? function looks confusing. can provide example of how use ? below example of output got to, can't figure out how make test ? md5sum better test zip archives ?
iex(4)> :zip.table('testing.zip') {:ok, [{:zip_comment, []}, {:zip_file, 'mix.exs', {:file_info, 930, :regular, :read_write, {{2015, 7, 15}, {2, 11, 9}}, {{2015, 7, 15}, {2, 11, 9}}, {{2015, 7, 15}, {2, 11, 9}}, 54, 1, 0, 0, 0, 0, 0}, [], 0, 444}, {:zip_file, 'mix.lock', {:file_info, 332, :regular, :read_write, {{2015, 7, 15}, {2, 9, 6}}, {{2015, 7, 15}, {2, 9, 6}}, {{2015, 7, 15}, {2, 9, 6}}, 54, 1, 0, 0, 0, 0, 0}, [], 481, 152}]}
the :zip
module erlang not easy work with, i'll try break down you.
first, need appropriate representation of zip_file
record in order erlang able work it. otherwise, have matches on tuples lot of elements clutter our code unnecessarily. following module heavily based on the file.stat
implementation elixir , allow access values in unwieldy tuples simple dot notation.
require record defmodule zip.file record = record.extract(:zip_file, from_lib: "stdlib/include/zip.hrl") keys = :lists.map(&elem(&1, 0), record) vals = :lists.map(&{&1, [], nil}, keys) pairs = :lists.zip(keys, vals) defstruct keys def to_record(%zip.file{unquote_splicing(pairs)}) {:zip_file, unquote_splicing(vals)} end def from_record(zip_file) def from_record({:zip_file, unquote_splicing(vals)}) %zip.file{unquote_splicing(pairs)} |> map.update!(:info, fn(info) -> file.stat.from_record(info) end) end end
we can build small wrapper class around erlang zip
module. not wrap methods, ones we'll use here. added list_files/1
function returns files, excluding directories , comments listing.
defmodule zip def open(archive) {:ok, zip_handle} = :zip.zip_open(archive) zip_handle end def close(zip_handle) :zip.zip_close(zip_handle) end def list_dir(zip_handle) {:ok, result} = :zip.zip_list_dir(zip_handle) result end def list_files(zip_handle) list_dir(zip_handle) |> enum.drop(1) |> enum.map(&zip.file.from_record/1) |> enum.filter(&(&1.info.type == :regular)) end end
suppose have following zip archive testing:
cd /tmp touch foo bar baz zip archive.zip foo bar baz
now can assert file names inside zip archive:
test "files in zip" zip = zip.open('/tmp/archive.zip') files = zip.list_files(zip) |> enum.map(&(&1.name)) zip.close(zip) assert files == ['foo', 'bar', 'baz'] end
i'll leave further operations , assertions on zip archive implement, , hope gets started.
Comments
Post a Comment