Skip to content

Commit

Permalink
dev(narugo): add unittest for archive writer
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Nov 27, 2024
1 parent e4a7cf6 commit 5a9703d
Show file tree
Hide file tree
Showing 204 changed files with 85 additions and 10 deletions.
8 changes: 7 additions & 1 deletion test/archive/test_rar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from hbutils.testing import isolated_directory, disable_output

from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack
from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack, archive_writer
from hfutils.utils import walk_files
from test.testings import get_testfile

Expand Down Expand Up @@ -56,6 +56,12 @@ def test_archive_pack(self, raw_dir, check_unpack_dir):
archive_pack('rar', raw_dir, 'pack.rar')
assert len(list(walk_files(raw_dir))) == origin_files

@skipUnless(rarfile, 'rarfile module required.')
def test_archive_writer(self, raw_dir, check_unpack_dir):
with isolated_directory():
with disable_output(), pytest.raises(RuntimeError):
archive_writer('rar', 'pack.rar')

@skipUnless(rarfile, 'rarfile module required.')
def test_archive_unpack(self, raw_rar, check_unpack_dir):
with isolated_directory():
Expand Down
26 changes: 23 additions & 3 deletions test/archive/test_sevenz.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import glob
import os.path
from unittest import skipUnless

import pytest
from hbutils.testing import isolated_directory, disable_output
from hbutils.testing import isolated_directory, disable_output, tmatrix

from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack
from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack, archive_writer
from hfutils.utils import walk_files
from test.testings import get_testfile
from test.testings import get_testfile, dir_compare

try:
import py7zr
Expand Down Expand Up @@ -93,3 +94,22 @@ def test_archive_unpack_with_password(self, raw_password_7z, check_unpack_dir):
with disable_output():
archive_unpack(raw_password_7z, '.', password='password')
check_unpack_dir('.')

@skipUnless(py7zr, 'py7zr module required.')
@pytest.mark.parametrize(*tmatrix({
'ext': ['bin', 'binary', 'bst'],
'type_': ['7z'],
}))
def test_archive_writer(self, ext, type_):
src_dir = get_testfile('complex_directory')
dst_dir = get_testfile(f'complex_directory_{ext}')

with isolated_directory():
archive_file = f'archive.{type_}'
with archive_writer(type_, archive_file) as af:
for file in glob.glob(os.path.join(src_dir, '**', f'*.{ext}'), recursive=True):
af.add(file, os.path.basename(file))

archive_unpack(archive_file, '.', silent=True)
os.remove(archive_file)
dir_compare('.', dst_dir)
30 changes: 27 additions & 3 deletions test/archive/test_tar.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import glob
import os.path
import tarfile

import pytest
from hbutils.testing import isolated_directory, disable_output
from hbutils.testing import isolated_directory, disable_output, tmatrix

from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack
from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack, archive_writer
from hfutils.utils import walk_files
from test.testings import get_testfile
from test.testings import get_testfile, dir_compare


@pytest.fixture()
Expand Down Expand Up @@ -83,3 +84,26 @@ def test_archive_unpack_password_ignore(self, check_unpack_dir, type_, ext):
with disable_output(), pytest.warns(UserWarning):
archive_unpack(get_testfile(f'raw{ext}'), '.', password='password')
check_unpack_dir('.')

@pytest.mark.parametrize(*tmatrix({
'ext': ['bin', 'binary', 'bst'],
('type_', 'type_ext'): [
('tar', '.tar'),
('gztar', '.tar.gz'),
('bztar', '.tar.bz2'),
('xztar', '.tar.xz'),
],
}))
def test_archive_writer(self, ext, type_, type_ext):
src_dir = get_testfile('complex_directory')
dst_dir = get_testfile(f'complex_directory_{ext}')

with isolated_directory():
archive_file = f'archive.{type_ext}'
with archive_writer(type_, archive_file) as af:
for file in glob.glob(os.path.join(src_dir, '**', f'*.{ext}'), recursive=True):
af.add(file, os.path.basename(file))

archive_unpack(archive_file, '.', silent=True)
os.remove(archive_file)
dir_compare('.', dst_dir)
25 changes: 22 additions & 3 deletions test/archive/test_zip.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import glob
import os.path
import zipfile

import pytest
from hbutils.testing import isolated_directory, disable_output
from hbutils.testing import isolated_directory, disable_output, tmatrix

from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack
from hfutils.archive import get_archive_type, get_archive_extname, archive_pack, archive_unpack, archive_writer
from hfutils.utils import walk_files
from test.testings import get_testfile
from test.testings import get_testfile, dir_compare


@pytest.fixture()
Expand Down Expand Up @@ -69,3 +70,21 @@ def test_archive_unpack_with_password(self, raw_password_zip, check_unpack_dir):
with disable_output():
archive_unpack(raw_password_zip, '.', password='password')
check_unpack_dir('.')

@pytest.mark.parametrize(*tmatrix({
'ext': ['bin', 'binary', 'bst'],
'type_': ['zip'],
}))
def test_archive_writer(self, ext, type_):
src_dir = get_testfile('complex_directory')
dst_dir = get_testfile(f'complex_directory_{ext}')

with isolated_directory():
archive_file = f'archive.{type_}'
with archive_writer(type_, archive_file) as af:
for file in glob.glob(os.path.join(src_dir, '**', f'*.{ext}'), recursive=True):
af.add(file, os.path.basename(file))

archive_unpack(archive_file, '.', silent=True)
os.remove(archive_file)
dir_compare('.', dst_dir)
Binary file added test/testfile/complex_directory/0/00009.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00014.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00025.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00052.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00069.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00072.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00088.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00092.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/0/00093.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/testfile/complex_directory/00003.bst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�z�C��]�<LJs���a�;�cg� Vtx���V� Yn�P&��dgA�n��KA�2�x=L_���<�?�cJјä��G.Cw�bg>���#��=��W:'u^�Ա*j7W<���.���A[P�LV���n��
Expand Down
Binary file added test/testfile/complex_directory/00005.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/00029.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/00035.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/00042.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/00048.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/00084.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/00094.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00002.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00011.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00051.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00059.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00073.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00078.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/1/00082.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/2/00015.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/2/00016.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/2/00021.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00031.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00033.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00050.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00053.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00065.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00096.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00097.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/0/00098.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00006.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00010.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00012.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00027.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00045.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00046.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00060.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00062.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00076.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00079.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00086.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/00089.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00008.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00019.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00022.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00024.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00040.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00043.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00044.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00049.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00067.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00068.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/1/00087.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00007.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00023.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00032.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00038.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00039.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00041.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00054.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00057.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00061.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00064.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00074.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00080.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/x/2/00085.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00000.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00004.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00017.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00028.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00030.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00034.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00036.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00037.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00058.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00063.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00081.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/0/00099.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00001.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00020.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00026.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00071.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00075.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00090.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/00091.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/1/00056.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/1/00095.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/2/00013.bst
Binary file not shown.
Binary file added test/testfile/complex_directory/y/2/00018.bin
Binary file not shown.
Binary file added test/testfile/complex_directory/y/2/00047.binary
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions test/testfile/complex_directory/y/2/00070.bst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
G�O�;��5Z����`�;����b����~�S�I��QOj�����,�7�XȀ!�PA�x�9TQ�aF-�y�#_���`k�p�:r,�H2D' ^M��nûp����C��G���%�8!o�2P��I�QX�
[���ϧ����.�} u��#��>c�~�3��+V���U y�YӍ����8+��%��p��E\�A���i��f��Lk)�o���4�/e�m��W�r�,��9����{�Q�QY�"��:,��}�H���U}�#<�
Expand Down
Binary file added test/testfile/complex_directory/y/2/00077.binary
Binary file not shown.
Binary file added test/testfile/complex_directory/y/2/00083.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00005.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00011.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00012.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00014.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00018.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00025.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00026.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00027.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00028.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00034.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00036.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00039.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00045.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00050.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00051.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00054.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00056.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00058.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00067.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00071.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00072.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00075.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00082.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00085.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00091.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00093.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00095.bin
Binary file not shown.
Binary file added test/testfile/complex_directory_bin/00098.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/testfile/complex_directory_binary/00092.binary
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00002.bst
Binary file not shown.
1 change: 1 addition & 0 deletions test/testfile/complex_directory_bst/00003.bst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�z�C��]�<LJs���a�;�cg� Vtx���V� Yn�P&��dgA�n��KA�2�x=L_���<�?�cJјä��G.Cw�bg>���#��=��W:'u^�Ա*j7W<���.���A[P�LV���n��
Expand Down
Binary file added test/testfile/complex_directory_bst/00006.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00009.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00010.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00013.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00015.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00019.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00020.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00022.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00023.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00029.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00030.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00031.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00032.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00035.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00037.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00038.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00040.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00042.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00044.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00046.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00048.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00057.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00059.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00060.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00061.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00063.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00068.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00069.bst
Binary file not shown.
2 changes: 2 additions & 0 deletions test/testfile/complex_directory_bst/00070.bst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
G�O�;��5Z����`�;����b����~�S�I��QOj�����,�7�XȀ!�PA�x�9TQ�aF-�y�#_���`k�p�:r,�H2D' ^M��nûp����C��G���%�8!o�2P��I�QX�
[���ϧ����.�} u��#��>c�~�3��+V���U y�YӍ����8+��%��p��E\�A���i��f��Lk)�o���4�/e�m��W�r�,��9����{�Q�QY�"��:,��}�H���U}�#<�
Expand Down
Binary file added test/testfile/complex_directory_bst/00074.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00078.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00079.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00080.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00081.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00083.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00084.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00087.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00096.bst
Binary file not shown.
Binary file added test/testfile/complex_directory_bst/00097.bst
Binary file not shown.

0 comments on commit 5a9703d

Please sign in to comment.