Code cleanup
authorThierry Florac <tflorac@ulthar.net>
Tue, 26 Nov 2019 10:09:26 +0100
changeset 17 e94c6b8d5b50
parent 16 ac179d1bfc0b
child 18 d58d443e3683
Code cleanup
src/pyams_template/tests/__init__.py
src/pyams_template/tests/test_utilsdocs.py
src/pyams_template/tests/test_utilsdocstrings.py
--- a/src/pyams_template/tests/__init__.py	Tue Nov 26 10:08:37 2019 +0100
+++ b/src/pyams_template/tests/__init__.py	Tue Nov 26 10:09:26 2019 +0100
@@ -1,1 +1,29 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
 
+"""
+Generic Test case for ztfy.utils doctest
+"""
+
+import os
+import sys
+
+__docformat__ = 'restructuredtext'
+
+
+def get_package_dir(value):
+    """Get package directory"""
+
+    package_dir = os.path.split(value)[0]
+    if package_dir not in sys.path:
+        sys.path.append(package_dir)
+    return package_dir
--- a/src/pyams_template/tests/test_utilsdocs.py	Tue Nov 26 10:08:37 2019 +0100
+++ b/src/pyams_template/tests/test_utilsdocs.py	Tue Nov 26 10:09:26 2019 +0100
@@ -1,5 +1,3 @@
-### -*- coding: utf-8 -*- ####################################################
-##############################################################################
 #
 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
 # All Rights Reserved.
@@ -11,22 +9,24 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-##############################################################################
 
 """
 Generic Test case for pyams_template doctest
 """
+
+import doctest
+import os
+import unittest
+
+from pyams_template.tests import get_package_dir
+
+
 __docformat__ = 'restructuredtext'
 
-import unittest
-import doctest
-import sys
-import os
+CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
 
 
-current_dir = os.path.dirname(__file__)
-
-def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):
+def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):  # pylint: disable=invalid-name
     """Returns a test suite, based on doctests found in /doctest."""
     suite = []
     if globs is None:
@@ -35,15 +35,12 @@
     flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
              doctest.REPORT_ONLY_FIRST_FAILURE)
 
-    package_dir = os.path.split(test_dir)[0]
-    if package_dir not in sys.path:
-        sys.path.append(package_dir)
-
+    package_dir = get_package_dir(test_dir)
     doctest_dir = os.path.join(package_dir, 'doctests')
 
     # filtering files on extension
     docs = [os.path.join(doctest_dir, doc) for doc in
-            os.listdir(doctest_dir) if doc.endswith('.txt')]
+            os.listdir(doctest_dir) if doc.endswith('.txt') or doc.endswith('.rst')]
 
     for test in docs:
         suite.append(doctest.DocFileSuite(test, optionflags=flags,
@@ -53,10 +50,11 @@
 
     return unittest.TestSuite(suite)
 
+
 def test_suite():
     """returns the test suite"""
-    return doc_suite(current_dir)
+    return doc_suite(CURRENT_DIR)
+
 
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')
-
--- a/src/pyams_template/tests/test_utilsdocstrings.py	Tue Nov 26 10:08:37 2019 +0100
+++ b/src/pyams_template/tests/test_utilsdocstrings.py	Tue Nov 26 10:09:26 2019 +0100
@@ -1,7 +1,5 @@
-### -*- coding: utf-8 -*- ####################################################
-##############################################################################
 #
-# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -11,21 +9,22 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-##############################################################################
 
 """
 Generic Test case for pyams_template doc strings
 """
+
+import doctest
+import os
+import sys
+import unittest
+
+
 __docformat__ = 'restructuredtext'
 
-import unittest
-import doctest
-import sys
-import os
+CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
 
 
-current_dir = os.path.abspath(os.path.dirname(__file__))
-
 def doc_suite(test_dir, globs=None):
     """Returns a test suite, based on doc tests strings found in /*.py"""
     suite = []
@@ -45,7 +44,7 @@
     docs = [doc for doc in docs if not doc.startswith('__')]
 
     for test in docs:
-        fd = open(os.path.join(package_dir, test))
+        fd = open(os.path.join(package_dir, test))  # pylint: disable=invalid-name
         content = fd.read()
         fd.close()
         if '>>> ' not in content:
@@ -57,9 +56,11 @@
 
     return unittest.TestSuite(suite)
 
+
 def test_suite():
     """returns the test suite"""
-    return doc_suite(current_dir)
+    return doc_suite(CURRENT_DIR)
+
 
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')