From 3c751ad387b3e783fb69a121b529b7967d61307c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Bryon?= Date: Tue, 21 May 2013 23:36:38 +0200 Subject: [PATCH] Refs #22, refs #19 - Simplified 'make test' ; moved PEP396TestCase in rst2rst.tests.packaging ; some PEP8-related changes. --- Makefile | 2 +- etc/nose.cfg | 2 +- rst2rst/tests/__init__.py | 2 ++ rst2rst/tests/packaging.py | 33 +++++++++++++++++++++ rst2rst/tests/test_fixtures.py | 53 +++++++++------------------------- 5 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 rst2rst/tests/packaging.py diff --git a/Makefile b/Makefile index b2e7b0f..fa2ae66 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/etc/nose.cfg b/etc/nose.cfg index 891cdd3..45e9af4 100644 --- a/etc/nose.cfg +++ b/etc/nose.cfg @@ -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 diff --git a/rst2rst/tests/__init__.py b/rst2rst/tests/__init__.py index e69de29..737a4ab 100644 --- a/rst2rst/tests/__init__.py +++ b/rst2rst/tests/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +"""rst2rst tests.""" diff --git a/rst2rst/tests/packaging.py b/rst2rst/tests/packaging.py new file mode 100644 index 0000000..9daf2f1 --- /dev/null +++ b/rst2rst/tests/packaging.py @@ -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)) diff --git a/rst2rst/tests/test_fixtures.py b/rst2rst/tests/test_fixtures.py index f44c4a3..6e51946 100644 --- a/rst2rst/tests/test_fixtures.py +++ b/rst2rst/tests/test_fixtures.py @@ -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 @@ -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 @@ -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 @@ -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): @@ -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):