mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-20 04:48:02 +01:00
Added new function for creating archives
This commit is contained in:
@@ -57,31 +57,51 @@ def get_config_options(conf):
|
|||||||
for key, val in parser.items(section)}
|
for key, val in parser.items(section)}
|
||||||
|
|
||||||
|
|
||||||
def extract_archive(arch_name, title=''):
|
def operate_archive(arch_name, operation, text):
|
||||||
"""
|
"""
|
||||||
Extract provided archive to current directory
|
Create archive from contents of current directory
|
||||||
"""
|
"""
|
||||||
|
|
||||||
archiver = file_archive.get_archiver(arch_name)
|
archiver = file_archive.get_archiver(arch_name)
|
||||||
|
|
||||||
if archiver is None:
|
if archiver is None:
|
||||||
sys.stderr.write("Unable find archive type for `%s' or executable "
|
|
||||||
"doesn't exists.\n" % arch_name)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
msg = message.Message("Extracting files for `%s'. Please be "
|
msg = message.Message(text)
|
||||||
"patient" % title)
|
if text:
|
||||||
if title:
|
|
||||||
msg.show()
|
msg.show()
|
||||||
|
|
||||||
res = archiver.extract(arch_name)
|
res = False
|
||||||
|
|
||||||
|
if operation == 'extract':
|
||||||
|
res = archiver.extract(arch_name)
|
||||||
|
|
||||||
|
if operation == 'create':
|
||||||
|
res = archiver.create(arch_name)
|
||||||
|
|
||||||
msg.close()
|
msg.close()
|
||||||
|
|
||||||
if not res:
|
return res
|
||||||
sys.stderr.write("Error extracting `%s'.\n" % arch_name)
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
def create_archive(arch_name, title=''):
|
||||||
|
"""
|
||||||
|
Create archive from contents of current directory
|
||||||
|
"""
|
||||||
|
msg = ''
|
||||||
|
if title:
|
||||||
|
msg = "Creating archive for `%s'. Please be patient" % title
|
||||||
|
return operate_archive(arch_name, 'create', msg)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_archive(arch_name, title=''):
|
||||||
|
"""
|
||||||
|
Extract provided archive to current directory
|
||||||
|
"""
|
||||||
|
msg = ''
|
||||||
|
if title:
|
||||||
|
msg = "Extracting files for `%s'. Please be patient" % title
|
||||||
|
return operate_archive(arch_name, 'extract', msg)
|
||||||
|
|
||||||
|
|
||||||
def run_command(cmd):
|
def run_command(cmd):
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class TestCD32(TestCase):
|
|||||||
@mock.patch('fs_uae_wrapper.base.Base._load_save')
|
@mock.patch('fs_uae_wrapper.base.Base._load_save')
|
||||||
@mock.patch('fs_uae_wrapper.base.Base._copy_conf')
|
@mock.patch('fs_uae_wrapper.base.Base._copy_conf')
|
||||||
@mock.patch('fs_uae_wrapper.base.Base._extract')
|
@mock.patch('fs_uae_wrapper.base.Base._extract')
|
||||||
def test_run(self, extr, cconf, lsave, kick, runemul, ssave, *args):
|
def test_run(self, extr, cconf, lsave, kick, runemul, ssave, mkdtemp):
|
||||||
|
|
||||||
extr.return_value = False
|
extr.return_value = False
|
||||||
cconf.return_value = False
|
cconf.return_value = False
|
||||||
|
|||||||
@@ -72,6 +72,44 @@ class TestUtils(TestCase):
|
|||||||
conf = utils.get_config_options(self.fname)
|
conf = utils.get_config_options(self.fname)
|
||||||
self.assertDictEqual(conf, {'wrapper': ''})
|
self.assertDictEqual(conf, {'wrapper': ''})
|
||||||
|
|
||||||
|
@mock.patch('fs_uae_wrapper.file_archive.Archive.extract')
|
||||||
|
@mock.patch('fs_uae_wrapper.file_archive.Archive.create')
|
||||||
|
@mock.patch('fs_uae_wrapper.message.Message.close')
|
||||||
|
@mock.patch('fs_uae_wrapper.message.Message.show')
|
||||||
|
def test_operate_archive(self, show, close, create, extract):
|
||||||
|
|
||||||
|
os.chdir(self.dirname)
|
||||||
|
|
||||||
|
# No config
|
||||||
|
self.assertFalse(utils.operate_archive('non-existend.7z', 'foo', ''))
|
||||||
|
|
||||||
|
# Archive type not known
|
||||||
|
with open('unsupported-archive.ace', 'w') as fobj:
|
||||||
|
fobj.write("\n")
|
||||||
|
self.assertFalse(utils.operate_archive('unsupported-archive.ace',
|
||||||
|
'foo', ''))
|
||||||
|
|
||||||
|
# archive is known, but extraction will fail - we have an empty
|
||||||
|
# archive and there is no guarantee, that 7z exists on system where
|
||||||
|
# test will run
|
||||||
|
extract.return_value = True
|
||||||
|
with open('supported-archive.7z', 'w') as fobj:
|
||||||
|
fobj.write("\n")
|
||||||
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
|
'extract', ''))
|
||||||
|
extract.assert_called_once()
|
||||||
|
|
||||||
|
extract.reset_mock()
|
||||||
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
|
'extract', ''))
|
||||||
|
extract.assert_called_once()
|
||||||
|
|
||||||
|
os.unlink('supported-archive.7z')
|
||||||
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
|
'create', 'test'))
|
||||||
|
create.assert_called_once()
|
||||||
|
show.assert_called_once()
|
||||||
|
|
||||||
def test_extract_archive(self):
|
def test_extract_archive(self):
|
||||||
|
|
||||||
os.chdir(self.dirname)
|
os.chdir(self.dirname)
|
||||||
@@ -91,6 +129,25 @@ class TestUtils(TestCase):
|
|||||||
fobj.write("\n")
|
fobj.write("\n")
|
||||||
self.assertFalse(utils.extract_archive('supported-archive.7z'))
|
self.assertFalse(utils.extract_archive('supported-archive.7z'))
|
||||||
|
|
||||||
|
def test_create_archive(self):
|
||||||
|
|
||||||
|
os.chdir(self.dirname)
|
||||||
|
|
||||||
|
# No config
|
||||||
|
self.assertFalse(utils.extract_archive('non-existend.7z'))
|
||||||
|
|
||||||
|
# Archive type not known
|
||||||
|
with open('unsupported-archive.ace', 'w') as fobj:
|
||||||
|
fobj.write("\n")
|
||||||
|
self.assertFalse(utils.extract_archive('unsupported-archive.ace'))
|
||||||
|
|
||||||
|
# archive is known, but extraction will fail - we have an empty
|
||||||
|
# archive and there is no guarantee, that 7z exists on system where
|
||||||
|
# test will run
|
||||||
|
with open('supported-archive.7z', 'w') as fobj:
|
||||||
|
fobj.write("\n")
|
||||||
|
self.assertFalse(utils.extract_archive('supported-archive.7z'))
|
||||||
|
|
||||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.extract')
|
@mock.patch('fs_uae_wrapper.file_archive.Archive.extract')
|
||||||
def test_extract_archive_positive(self, arch_extract):
|
def test_extract_archive_positive(self, arch_extract):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user