Skip to content

Commit

Permalink
Refs #19 - Added tests for rst2rst.tests.test_fixtures.fixture_names(…
Browse files Browse the repository at this point in the history
…) and refactored fixture_names().
  • Loading branch information
benoitbryon committed May 24, 2013
1 parent 5548e89 commit cffa09d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
32 changes: 29 additions & 3 deletions rst2rst/tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))
Expand All @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions rst2rst/utils.py → rst2rst/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Various utilities."""
# -*- coding: utf-8 -*-
"""Tools not specific to rst2rst. They could be moved in external projects."""
import os


Expand Down Expand Up @@ -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()

Expand Down
36 changes: 36 additions & 0 deletions rst2rst/utils/tempdir.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit cffa09d

Please sign in to comment.