From 7da0506f2d8668a2c8013597482213f4d3422a07 Mon Sep 17 00:00:00 2001 From: Rogdham Date: Sat, 7 Dec 2013 17:35:54 +0100 Subject: [PATCH] Fix `utils.copy` for copying files, add unit tests `copy('', 'a/b.ext0', 'c/d.ext1')` is copying `a/b.ext0` into `c/d.ext1/b.ext0` (creating folder `c/d.ext1` in the process) instead of `c/d.ext1`. Bug introduced by e03cf3f51735d5387fee7064cae89aaa1d34a0a6. --- pelican/tests/test_utils.py | 93 +++++++++++++++++++++++++++++++++++++ pelican/utils.py | 2 +- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/pelican/tests/test_utils.py b/pelican/tests/test_utils.py index 0642926e..d35bbb98 100644 --- a/pelican/tests/test_utils.py +++ b/pelican/tests/test_utils.py @@ -323,6 +323,99 @@ class TestUtils(LoggedTestCase): locale.setlocale(locale.LC_TIME, old_locale) +class TestCopy(unittest.TestCase): + '''Tests the copy utility''' + + def setUp(self): + self.root_dir = mkdtemp(prefix='pelicantests.') + + def tearDown(self): + shutil.rmtree(self.root_dir) + + def _create_file(self, *path): + with open(os.path.join(self.root_dir, *path), 'w') as f: + f.write('42\n') + + def _create_dir(self, *path): + os.makedirs(os.path.join(self.root_dir, *path)) + + def _exist_file(self, *path): + path = os.path.join(self.root_dir, *path) + self.assertTrue(os.path.isfile(path), 'File does not exist: %s' % path) + + def _exist_dir(self, *path): + path = os.path.join(self.root_dir, *path) + self.assertTrue(os.path.exists(path), + 'Directory does not exist: %s' % path) + + def test_copy_file_same_path(self): + self._create_file('a.txt') + utils.copy('', + os.path.join(self.root_dir, 'a.txt'), + os.path.join(self.root_dir, 'b.txt') + ) + self._exist_file('b.txt') + + def test_copy_file_different_path(self): + self._create_dir('a') + self._create_dir('b') + self._create_file('a', 'a.txt') + utils.copy('', + os.path.join(self.root_dir, 'a', 'a.txt'), + os.path.join(self.root_dir, 'b', 'b.txt') + ) + self._exist_dir('b') + self._exist_file('b', 'b.txt') + + def test_copy_file_create_dirs(self): + self._create_file('a.txt') + utils.copy('', + os.path.join(self.root_dir, 'a.txt'), + os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b.txt') + ) + self._exist_dir('b0') + self._exist_dir('b0', 'b1') + self._exist_dir('b0', 'b1', 'b2') + self._exist_dir('b0', 'b1', 'b2', 'b3') + self._exist_file('b0', 'b1', 'b2', 'b3', 'b.txt') + + def test_copy_dir_same_path(self): + self._create_dir('a') + self._create_file('a', 'a.txt') + utils.copy('', + os.path.join(self.root_dir, 'a'), + os.path.join(self.root_dir, 'b') + ) + self._exist_dir('b') + self._exist_file('b', 'a.txt') + + def test_copy_dir_different_path(self): + self._create_dir('a0') + self._create_dir('a0', 'a1') + self._create_file('a0', 'a1', 'a.txt') + self._create_dir('b0') + utils.copy('', + os.path.join(self.root_dir, 'a0', 'a1'), + os.path.join(self.root_dir, 'b0', 'b1') + ) + self._exist_dir('b0', 'b1') + self._exist_file('b0', 'b1', 'a.txt') + + def test_copy_dir_create_dirs(self): + self._create_dir('a') + self._create_file('a', 'a.txt') + utils.copy('', + os.path.join(self.root_dir, 'a'), + os.path.join(self.root_dir, 'b0', 'b1', 'b2', 'b3', 'b') + ) + self._exist_dir('b0') + self._exist_dir('b0', 'b1') + self._exist_dir('b0', 'b1', 'b2') + self._exist_dir('b0', 'b1', 'b2', 'b3') + self._exist_dir('b0', 'b1', 'b2', 'b3', 'b') + self._exist_file('b0', 'b1', 'b2', 'b3', 'b', 'a.txt') + + class TestDateFormatter(unittest.TestCase): '''Tests that the output of DateFormatter jinja filter is same as utils.strftime''' diff --git a/pelican/utils.py b/pelican/utils.py index 4b25ec7f..7de1457d 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -273,7 +273,7 @@ def copy(path, source, destination, destination_path=None): destination_ = os.path.abspath( os.path.expanduser(os.path.join(destination, destination_path))) - if not os.path.exists(destination_): + if not os.path.exists(destination_) and not os.path.isfile(source_): os.makedirs(destination_) def recurse(source, destination):