From cffa09db733b0f326c83bf6aa46f17044dddc596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Bryon?= Date: Fri, 24 May 2013 10:56:45 +0200 Subject: [PATCH] Refs #19 - Added tests for rst2rst.tests.test_fixtures.fixture_names() and refactored fixture_names(). --- rst2rst/tests/test_fixtures.py | 32 +++++++++++++++++++--- rst2rst/{utils.py => utils/__init__.py} | 7 ++--- rst2rst/utils/tempdir.py | 36 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) rename rst2rst/{utils.py => utils/__init__.py} (90%) create mode 100644 rst2rst/utils/tempdir.py diff --git a/rst2rst/tests/test_fixtures.py b/rst2rst/tests/test_fixtures.py index 6d1a7cd..9ea181a 100644 --- a/rst2rst/tests/test_fixtures.py +++ b/rst2rst/tests/test_fixtures.py @@ -7,6 +7,8 @@ from docutils.core import publish_string +from rst2rst.utils.tempdir import temporary_directory + #: Absolute path to ``rst2rst.tests`` module. TESTS_DIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__))) @@ -24,11 +26,35 @@ def fixture_names(fixture_dir=FIXTURE_DIR): Fixture names are computed from filenames in ``fixture_dir``. """ - return sorted([ - re.sub(r'^(.+?)-input\.txt$', r'\1', f) + return [ + re.match(r'^(.+?)-input\.txt$', f).group(1) for f in os.listdir(fixture_dir) if f.endswith('-input.txt') - ]) + ] + + +class FixturesNamesTestCase(unittest.TestCase): + """:py:func:`fixture_names` return list of fixture names in directory.""" + def test_capture(self): + """fixture_names() grabs all *-input.txt file in a directory.""" + filenames = ('one.txt', + 'two-input.txt', + 'threeinput.txt', + 'four-five-six-input.txt', + 'seven-output.txt', + 'eight-input', + 'nine-input.rst', + 'ten_input.txt') + expected_names = ['two', 'four-five-six'] # Order does not matter. + with temporary_directory() as directory: + for filename in filenames: # Create the files. + open(os.path.join(directory, filename), 'w').write('fake') + names = fixture_names(directory) + self.assertEqual(sorted(names), sorted(expected_names)) + + def test_not_empty(self): + """fixture_names() actually finds some fixtures.""" + self.assertTrue(fixture_names()) def fixture_method(name): diff --git a/rst2rst/utils.py b/rst2rst/utils/__init__.py similarity index 90% rename from rst2rst/utils.py rename to rst2rst/utils/__init__.py index ec268f6..d2c9e27 100644 --- a/rst2rst/utils.py +++ b/rst2rst/utils/__init__.py @@ -1,4 +1,5 @@ -"""Various utilities.""" +# -*- coding: utf-8 -*- +"""Tools not specific to rst2rst. They could be moved in external projects.""" import os @@ -52,9 +53,9 @@ def __cmp__(self, other): def read_relative_file(filename, relative_to=None): """Returns contents of the given file, which path is supposed relative - to this module.""" + to this package.""" if relative_to is None: - relative_to = __file__ + relative_to = os.path.dirname(__file__) with open(os.path.join(os.path.dirname(relative_to), filename)) as f: return f.read() diff --git a/rst2rst/utils/tempdir.py b/rst2rst/utils/tempdir.py new file mode 100644 index 0000000..1281673 --- /dev/null +++ b/rst2rst/utils/tempdir.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +"""Temporary directory management.""" +import shutil +import tempfile + + +class temporary_directory(object): + """Create, yield, and finally delete a temporary directory. + + >>> from rst2rst.utils.tempdir import temporary_directory + >>> import os + >>> with temporary_directory() as directory: + ... os.path.isdir(directory) + True + >>> os.path.exists(directory) + False + + Deletion of temporary directory is recursive. + + >>> with temporary_directory() as directory: + ... filename = os.path.join(directory, 'sample.txt') + ... __ = open(filename, 'w').close() + ... os.path.isfile(filename) + True + >>> os.path.isfile(filename) + False + + """ + def __enter__(self): + """Create temporary directory and return its path.""" + self.path = tempfile.mkdtemp() + return self.path + + def __exit__(self, exc_type=None, exc_val=None, exc_tb=None): + """Remove temporary directory recursively.""" + shutil.rmtree(self.path)