diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py index 174537d..746a47c 100644 --- a/fs_uae_wrapper/utils.py +++ b/fs_uae_wrapper/utils.py @@ -10,7 +10,7 @@ try: except ImportError: import ConfigParser as configparser -from fs_uae_wrapper import WRAPPER_KEY +from fs_uae_wrapper import message ARCHIVERS = {'.tar': ['tar', 'xf'], @@ -68,7 +68,7 @@ def get_config_options(conf): for key, val in parser.items(section)} -def extract_archive(arch_name): +def extract_archive(arch_name, show_gui_message, message_text): """ Extract provided archive to current directory """ @@ -85,41 +85,29 @@ def extract_archive(arch_name): sys.stderr.write("Unable find archive type for `%s'.\n" % arch_name) return False + msg = message.Message("Extracting files for `%s'. Please be " + "patient" % message_text) + if show_gui_message == '1': + msg.show() + try: subprocess.check_call(cmd + [arch_name]) except subprocess.CalledProcessError: sys.stderr.write("Error during extracting archive `%s'.\n" % arch_name) + msg.close() return False + msg.close() return True -def merge_wrapper_options(configuration, wrapper_options): - """ - Merge dictionaries with wrapper options into one. Commandline options - have precedence. - """ - options = {} - for key, val in configuration: - if WRAPPER_KEY in key: - options[key] = val - - options.update(wrapper_options) - - return options - - def merge_all_options(configuration, commandline): """ Merge dictionaries with wrapper options into one. Commandline options have precedence. """ - options = {} - for key, val in configuration.items(): - options[key] = val - + options = configuration.copy() options.update(commandline) - return options diff --git a/tests/test_utils.py b/tests/test_utils.py index 38059b7..4c1d672 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,11 @@ from tempfile import mkstemp, mkdtemp from unittest import TestCase import shutil +try: + from unittest import mock +except ImportError: + import mock + from fs_uae_wrapper import utils @@ -67,6 +72,50 @@ class TestUtils(TestCase): conf = utils.get_config_options(self.fname) self.assertDictEqual(conf, {'wrapper': ''}) + def test_extract_archive(self): + + os.chdir(self.dirname) + + # No config + self.assertFalse(utils.extract_archive('non-existend.7z', False, '')) + + # Archive type not known + with open('unsupported-archive.ace', 'w') as fobj: + fobj.write("\n") + self.assertFalse(utils.extract_archive('unsupported-archive.ace', + False, '')) + + # 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', + False, '')) + + @mock.patch('subprocess.check_call') + def test_extract_archive_positive(self, sp_check_call): + + os.chdir(self.dirname) + # archive is known, and extraction should succeed + arch_name = 'archive.7z' + with open(arch_name, 'w') as fobj: + fobj.write("\n") + self.assertTrue(utils.extract_archive(arch_name, False, '')) + sp_check_call.assert_called_once_with(utils.ARCHIVERS['.7z'] + + [arch_name]) + + def test_merge_all_options(self): + + conf = {'foo': '1', 'bar': 'zip'} + other = {'foo': '2', 'baz': '3'} + + merged = utils.merge_all_options(conf, other) + + self.assertDictEqual(merged, {'foo': '2', 'bar': 'zip', 'baz': '3'}) + self.assertDictEqual(conf, {'foo': '1', 'bar': 'zip'}) + self.assertDictEqual(other, {'foo': '2', 'baz': '3'}) + class TestCmdOptions(TestCase): @@ -99,3 +148,34 @@ class TestCmdOptions(TestCase): self.assertDictEqual(cmd, {'fullscreen': '1', 'fast_memory': '4096'}) self.assertListEqual(sorted(cmd.list()), ['--fast_memory=4096', '--fullscreen']) + + @mock.patch('os.getenv') + @mock.patch('os.path.expandvars') + @mock.patch('distutils.spawn.find_executable') + def test_interpolate_variables(self, find_exe, expandv, getenv): + + itrpl = utils.interpolate_variables + + string = 'foo = $CONFIG/../path/to/smth' + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae'), + 'foo = /home/user/../path/to/smth') + string = 'bar = $HOME' + expandv.return_value = '/home/user' + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae'), + 'bar = /home/user') + + string = 'foo = $APP/$EXE' + find_exe.return_value = '/usr/bin/fs-uae' + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae'), + 'foo = /usr/bin/fs-uae//usr/bin/fs-uae') + + string = 'docs = $DOCUMENTS' + getenv.return_value = '/home/user/Docs' + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae'), + 'docs = /home/user/Docs') + + string = 'baz = $BASE' + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae'), + 'baz = $BASE') + self.assertEqual(itrpl(string, '/home/user/Config.fs-uae', 'base'), + 'baz = base')