From 38a960491657bb57950c188b9545f35318338f84 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 2 Jan 2017 13:47:07 +0100 Subject: [PATCH] Implementation for saving changes in archive wrapper module --- fs_uae_wrapper/archive.py | 34 ++++++++++++++++-- tests/test_archive.py | 73 ++++++++++++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/fs_uae_wrapper/archive.py b/fs_uae_wrapper/archive.py index 6d37048..de54809 100644 --- a/fs_uae_wrapper/archive.py +++ b/fs_uae_wrapper/archive.py @@ -4,9 +4,12 @@ Run fs-uae with archived filesystem/adf files It will use compressed directories, and optionally replace source archive with the temporary one. """ +import os +import shutil import sys from fs_uae_wrapper import base +from fs_uae_wrapper import utils class Archive(base.Base): @@ -41,6 +44,8 @@ class Archive(base.Base): if not self._extract(): return False + self._load_save() + if not self._copy_conf(): return False @@ -48,10 +53,10 @@ class Archive(base.Base): if kick_opts: self.fsuae_options.update(kick_opts) - if self._run_emulator(self.fsuae_options.list()): - return self._make_archive() + if not self._run_emulator(self.fsuae_options.list()): + return False - return True + return self._make_archive() def _validate_options(self): @@ -71,6 +76,29 @@ class Archive(base.Base): """ Produce archive and save it back. Than remove old one. """ + if self.all_options.get('wrapper_persist_data', '0') != '1': + return True + + saves = self._get_saves_dir() + if saves: + if not self._save_save(): + return False + + curdir = os.path.abspath('.') + os.chdir(self.dir) + + if saves: + shutil.rmtree(saves) + os.unlink('Config.fs-uae') + + title = self._get_title() + + arch = os.path.basename(self.arch_filepath) + if not utils.create_archive(arch, title): + return False + + os.rename(arch, self.arch_filepath) + os.chdir(curdir) return True diff --git a/tests/test_archive.py b/tests/test_archive.py index 03f8bc6..2780ed6 100644 --- a/tests/test_archive.py +++ b/tests/test_archive.py @@ -1,3 +1,6 @@ +import os +import shutil +from tempfile import mkdtemp from unittest import TestCase try: @@ -11,6 +14,18 @@ from fs_uae_wrapper import utils class TestArchive(TestCase): + def setUp(self): + self.dirname = mkdtemp() + self.curdir = os.path.abspath(os.curdir) + os.chdir(self.dirname) + + def tearDown(self): + os.chdir(self.curdir) + try: + shutil.rmtree(self.dirname) + except OSError: + pass + def test_validate_options(self): arch = archive.Archive('Config.fs-uae', utils.CmdOption(), {}) @@ -23,16 +38,20 @@ class TestArchive(TestCase): self.assertTrue(arch._validate_options()) @mock.patch('tempfile.mkdtemp') + @mock.patch('fs_uae_wrapper.archive.Archive._make_archive') @mock.patch('fs_uae_wrapper.base.Base._run_emulator') @mock.patch('fs_uae_wrapper.base.Base._kickstart_option') @mock.patch('fs_uae_wrapper.base.Base._copy_conf') + @mock.patch('fs_uae_wrapper.base.Base._load_save') @mock.patch('fs_uae_wrapper.base.Base._extract') - def test_run(self, extr, cconf, kick, runemul, mkdtemp): + def test_run(self, extr, load, copy, kick, run, march, mkdtemp): extr.return_value = False - cconf.return_value = False - kick.return_value = {} - runemul.return_value = False + load.return_value = False + copy.return_value = False + kick.return_value = False + run.return_value = False + march.return_value = False arch = archive.Archive('Config.fs-uae', utils.CmdOption(), {}) self.assertFalse(arch.run()) @@ -45,12 +64,50 @@ class TestArchive(TestCase): extr.return_value = True self.assertFalse(arch.run()) - cconf.return_value = True - self.assertTrue(arch.run()) + load.return_value = True + self.assertFalse(arch.run()) + + copy.return_value = True + self.assertFalse(arch.run()) kick.return_value = {'foo': 'bar'} - self.assertTrue(arch.run()) + self.assertFalse(arch.run()) self.assertDictEqual(arch.fsuae_options, {'foo': 'bar'}) - runemul.return_value = True + run.return_value = True + self.assertFalse(arch.run()) + + march.return_value = True self.assertTrue(arch.run()) + + @mock.patch('os.rename') + @mock.patch('os.unlink') + @mock.patch('shutil.rmtree') + @mock.patch('fs_uae_wrapper.utils.create_archive') + @mock.patch('fs_uae_wrapper.base.Base._get_title') + @mock.patch('fs_uae_wrapper.base.Base._save_save') + @mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') + def test_make_archive(self, sdir, save, title, carch, rmt, unlink, rename): + + sdir.return_value = None + save.return_value = False + title.return_value = '' + carch.return_value = False + + arch = archive.Archive('Config.fs-uae', utils.CmdOption(), {}) + arch.dir = self.dirname + arch.arch_filepath = os.path.join(self.dirname, 'foo.tgz') + arch.all_options = {} + self.assertTrue(arch._make_archive()) + + arch.all_options['wrapper_persist_data'] = '1' + self.assertFalse(arch._make_archive()) + + carch.return_value = True + self.assertTrue(arch._make_archive()) + + sdir.return_value = '/some/path' + self.assertFalse(arch._make_archive()) + + save.return_value = True + self.assertTrue(arch._make_archive())