summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-04-21 13:51:28 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-04-21 13:51:28 -0700
commite226768a7b69e580701e528f683ae4aabbfb85f7 (patch)
tree5e68e4a81664216ae6e75457ca8d3ac47da20dba
parente4f488c6ed0e65a20f07f0d6d4ce10875398557b (diff)
downloadpystache-e226768a7b69e580701e528f683ae4aabbfb85f7.tar.gz
The test script now converts text doctest files to Python 3 when needed.
-rw-r--r--.gitignore1
-rw-r--r--pystache/tests/doctesting.py45
2 files changed, 43 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index 746fa86..14ff9eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
*.pyc
.tox
+*.temp2to3.rst
.DS_Store
# TODO: are comments allowed in .gitignore files?
# TextMate project file
diff --git a/pystache/tests/doctesting.py b/pystache/tests/doctesting.py
index 3cc06c1..1d82fde 100644
--- a/pystache/tests/doctesting.py
+++ b/pystache/tests/doctesting.py
@@ -5,13 +5,17 @@ Exposes a get_doctests() function for the project's test harness.
"""
+from lib2to3.main import main as lib2to3main
+import doctest
import os
import pkgutil
-import doctest
+from shutil import copyfile
+import sys
import traceback
from pystache.tests.common import PACKAGE_DIR, TEXT_DOCTEST_PATHS
+
# This module follows the guidance documented here:
#
# http://docs.python.org/library/doctest.html#unittest-api
@@ -27,14 +31,19 @@ def get_doctests(text_file_dir):
(i.e. non-module files) containing doctests.
"""
- suites = []
-
# Since module_relative is False in our calls to DocFileSuite below,
# paths should be OS-specific. See the following for more info--
#
# http://docs.python.org/library/doctest.html#doctest.DocFileSuite
#
paths = [os.path.normpath(os.path.join(text_file_dir, path)) for path in TEXT_DOCTEST_PATHS]
+
+ py_version = sys.version_info
+ if py_version >= (3,):
+ paths = _convert_paths(paths)
+
+ suites = []
+
for path in paths:
suite = doctest.DocFileSuite(path, module_relative=False)
suites.append(suite)
@@ -47,6 +56,36 @@ def get_doctests(text_file_dir):
return suites
+def _convert_2to3(path):
+ """
+ Convert the given file, and return the path to the converted files.
+
+ """
+ base, ext = os.path.splitext(path)
+ # For example, "README.temp2to3.rst".
+ new_path = "%s.temp2to3%s" % (base, ext)
+
+ copyfile(path, new_path)
+
+ args = ['--doctests_only', '--no-diffs', '--write', '--nobackups', new_path]
+ lib2to3main("lib2to3.fixes", args=args)
+
+ return new_path
+
+
+def _convert_paths(paths):
+ """
+ Convert the given files, and return the paths to the converted files.
+
+ """
+ new_paths = []
+ for path in paths:
+ new_path = _convert_2to3(path)
+ new_paths.append(new_path)
+
+ return new_paths
+
+
def _get_module_doctests(package_dir):
modules = []