Skip to content

Commit

Permalink
Refs #22, refs #19 - Simplified 'make test' ; moved PEP396TestCase in…
Browse files Browse the repository at this point in the history
… rst2rst.tests.packaging ; some PEP8-related changes.
  • Loading branch information
benoitbryon committed May 21, 2013
1 parent f2516cc commit 3c751ad
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test: test-app


test-app:
$(NOSE) --config=etc/nose.cfg --with-doctest --with-coverage --cover-erase --cover-package=rst2rst rst2rst
$(NOSE) --config=etc/nose.cfg $(PROJECT)


release:
Expand Down
2 changes: 1 addition & 1 deletion etc/nose.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ rednose = True
no-path-adjustment = True
with-coverage = True
cover-erase = True
cover-package = diecutter
cover-package = rst2rst
all-modules = True
2 changes: 2 additions & 0 deletions rst2rst/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
"""rst2rst tests."""
33 changes: 33 additions & 0 deletions rst2rst/tests/packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""Tests around project's distribution and packaging."""
import unittest


class PEP396TestCase(unittest.TestCase):
"""Check's PEP 396 compliance, i.e. package's __version__ attribute."""
def get_version(self):
"""Return rst2rst.__version__."""
from rst2rst import __version__
return __version__

def test_version_present(self):
"""Check that rst2rst.__version__ exists."""
try:
self.get_version()
except ImportError:
self.fail('rst2rst package has no attribute __version__.')

def test_version_match(self):
"""Check that rst2rst.__version__ matches pkg_resources information."""
try:
import pkg_resources
except ImportError:
self.fail('Cannot import pkg_resources module. It is part of '
'setuptools, which is a dependency of rst2rst.')
installed_version = pkg_resources.get_distribution('rst2rst').version
self.assertEqual(installed_version, self.get_version(),
'Version mismatch: version.txt tells "%s" whereas '
'pkg_resources tells "%s". '
'YOU MAY NEED TO RUN ``make update`` to update the '
'installed version in development environment.'
% (self.get_version(), installed_version))
53 changes: 14 additions & 39 deletions rst2rst/tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
"""rst2rst tests."""
# -*- coding: utf-8 -*-
"""Tests around fixtures: check behaviour obtained with sample files."""
from difflib import unified_diff
import os
import re
from unittest import TestCase
import unittest

from docutils.core import publish_string


class PEP396TestCase(TestCase):
"""Check's PEP 396 compliance, i.e. package's __version__ attribute."""
def get_version(self):
"""Return rst2rst.__version__."""
from rst2rst import __version__
return __version__

def test_version_present(self):
"""Check that rst2rst.__version__ exists."""
try:
version = self.get_version()
except ImportError:
self.fail('rst2rst package has no attribute __version__.')

def test_version_match(self):
"""Check that rst2rst.__version__ matches pkg_resources information."""
try:
import pkg_resources
except ImportError:
self.fail('Cannot import pkg_resources module. It is part of ' \
'setuptools, which is a dependency of rst2rst.')
installed_version = pkg_resources.get_distribution('rst2rst').version
self.assertEqual(installed_version, self.get_version(),
'Version mismatch: version.txt tells "%s" whereas ' \
'pkg_resources tells "%s". ' \
'YOU MAY NEED TO RUN ``make update`` to update the ' \
'installed version in development environment.' \
% (self.get_version(), installed_version))

FILE_DIR = os.path.normpath(
os.path.abspath(
os.path.dirname(__file__)))
FIXTURES_DIR = os.path.join(FILE_DIR, 'fixtures')


class TestMeta(type):
""" Unittest is a pain in the ass: it calls dir() on the *class*,
and then goes and gets each found method (on the class again) to
Expand All @@ -65,9 +39,11 @@ def __getattr__(cls, attrname):

return lambda: None

class WriterTestCase(TestCase):
__metaclass__ = TestMeta

class WriterTestCase(unittest.TestCase):
"""Test suite for the rst2rst.writer.Writer class."""
__metaclass__ = TestMeta

@classmethod
def _fixture_methods(cls):
""" Lists the names of all fixtures in the ./fixtures
Expand All @@ -80,6 +56,7 @@ def _fixture_methods(cls):
for f in os.listdir(FIXTURES_DIR)
if f.endswith('-input.txt')
]

@classmethod
def _fixture_paths(cls, attrname):
""" For a fixture named $fixture, gets the corresponding
Expand All @@ -91,7 +68,7 @@ def _fixture_paths(cls, attrname):
cls.__name__, attrname))

fixture = attrname.replace('test_', '', 1)

fixture_input = '%s-input.txt' % fixture
input_path = os.path.join(FIXTURES_DIR, fixture_input)
if not os.path.exists(input_path):
Expand Down Expand Up @@ -128,11 +105,9 @@ def run_fixture(self, fixture_input, fixture_expectation):

diff = ''.join(unified_diff(expected_lines, output_lines))
msg = "Content generated from %s differs from content at %s" \
"\nDiff:\n%s" % (
os.path.basename(fixture_input),
os.path.basename(fixture_expectation),
diff
)
"\nDiff:\n%s" % (os.path.basename(fixture_input),
os.path.basename(fixture_expectation),
diff)
self.fail(msg)

def test_repeatability(self):
Expand Down

0 comments on commit 3c751ad

Please sign in to comment.