diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py index fbe74a8..08be081 100644 --- a/fs_uae_wrapper/utils.py +++ b/fs_uae_wrapper/utils.py @@ -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): diff --git a/tests/test_cd32.py b/tests/test_cd32.py index ca7e1c8..295319d 100644 --- a/tests/test_cd32.py +++ b/tests/test_cd32.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index 1c933f5..5124d06 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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):