1
0
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:
2017-01-02 13:50:39 +01:00
parent 22d152bb64
commit 77dd4dfaf4
3 changed files with 90 additions and 13 deletions

View File

@@ -57,31 +57,51 @@ def get_config_options(conf):
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)
if archiver is None:
sys.stderr.write("Unable find archive type for `%s' or executable "
"doesn't exists.\n" % arch_name)
return False
msg = message.Message("Extracting files for `%s'. Please be "
"patient" % title)
if title:
msg = message.Message(text)
if text:
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()
if not res:
sys.stderr.write("Error extracting `%s'.\n" % arch_name)
return False
return res
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):

View File

@@ -29,7 +29,7 @@ class TestCD32(TestCase):
@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._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
cconf.return_value = False

View File

@@ -72,6 +72,44 @@ class TestUtils(TestCase):
conf = utils.get_config_options(self.fname)
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):
os.chdir(self.dirname)
@@ -91,6 +129,25 @@ class TestUtils(TestCase):
fobj.write("\n")
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')
def test_extract_archive_positive(self, arch_extract):