Commits

Olemis Lang committed 1847185

BH Multiproduct #355 : Test discovery reying upon pkg_resources

Comments (0)

Files changed (3)

-t288/t288_r1438538_sql_translate_global_env.diff
+#t288/t288_r1438538_sql_translate_global_env.diff
 t288/t288_r1438538_sql_translate_global_env_pending.diff
 #t115/t115_r1437383_inherit_product_config.diff
 #t333/t333_r1427886_schema_autoinc.diff
 #t115/t115_r1431447_product_envs_bep3_p1.diff
 #t115/t115_r1433322_unittest2_assertRaises_msg.diff
 #t115/t115_r1436300_option_accessor.diff
+#t355/t355_r1434677_trac_test_attachments.diff
+t355/t355_r1437383_pkgresources_discovery.diff
 t355/t355_r1437383_trac_test_perm.diff
-#t355/t355_r1434677_trac_test_attachments.diff
 t355/t355_r1437383_trac_test_ticket_api.diff

t288/t288_r1438538_sql_translate_global_env_pending.diff

 # HG changeset patch
-# Parent a103a7667c1345b7392b55ee894d699a67c4a7a5
+# Parent 01f625dcd92ab5f3d4814adf774d6026d153add0
 BH Multiproduct #288 : SQL translate global environment (pending)
 
-diff -r a103a7667c13 bloodhound_multiproduct/multiproduct/api.py
---- a/bloodhound_multiproduct/multiproduct/api.py	Tue Jan 29 19:47:18 2013 -0500
-+++ b/bloodhound_multiproduct/multiproduct/api.py	Tue Jan 29 22:46:55 2013 -0500
-@@ -97,7 +97,7 @@
-         self.log.debug("upgrading existing environment for %s plugin." % 
-                        PLUGIN_NAME)
-         db_installed_version = self.get_version()
--        with self.env.db_transaction as db:
-+        with self.env.db_direct_transaction as db:
-             if db_installed_version < 1:
-                 # Initial installation
-                 db("ALTER TABLE ticket ADD COLUMN product TEXT")
-diff -r a103a7667c13 bloodhound_multiproduct/multiproduct/dbcursor.py
---- a/bloodhound_multiproduct/multiproduct/dbcursor.py	Tue Jan 29 19:47:18 2013 -0500
-+++ b/bloodhound_multiproduct/multiproduct/dbcursor.py	Tue Jan 29 22:46:55 2013 -0500
-@@ -37,18 +37,25 @@
- PRODUCT_COLUMN = 'product'
- DEFAULT_PRODUCT = ''
+diff -r 01f625dcd92a bloodhound_multiproduct/multiproduct/env.py
+--- a/bloodhound_multiproduct/multiproduct/env.py	Thu Jan 31 14:41:22 2013 +0000
++++ b/bloodhound_multiproduct/multiproduct/env.py	Thu Jan 31 12:24:15 2013 -0500
+@@ -90,14 +90,6 @@
+         super(EnvironmentStub, self).__init__(default_data=default_data,
+                                               enable=enable, disable=disable,
+                                               path=path, destroying=destroying)
+-        # Apply multi product upgrades. This is required as the database proxy (translator)
+-        # is installed in any case, we want it to see multi-product enabled database
+-        # schema...
+-        mpsystem = MultiProductSystem(self)
+-        try:
+-            mpsystem.upgrade_environment()
+-        except OperationalError:
+-            pass
  
-+# Singleton used to mark products as unset
-+class empty_product(object):
-+    pass
-+
-+empty_product = empty_product()
-+
- class BloodhoundIterableCursor(trac.db.util.IterableCursor):
--    __slots__ = trac.db.util.IterableCursor.__slots__ + ['_translator']
-+    __slots__ = trac.db.util.IterableCursor.__slots__ + ['_translator', '_product_prefix']
-     _tls = concurrency.ThreadLocal(env=None)
- 
-     def __init__(self, cursor, log=None):
-         super(BloodhoundIterableCursor, self).__init__(cursor, log=log)
-         self._translator = None
-+        self._product_prefix = empty_product
- 
-     @property
-     def translator(self):
-         if not self._translator:
--            product_prefix = self.env.product.prefix if (self.env and self.env.product) else DEFAULT_PRODUCT
-+            product_prefix = self._product_prefix
-             self._translator = BloodhoundProductSQLTranslate(SKIP_TABLES,
-                                                              TRANSLATE_TABLES,
-                                                              PRODUCT_COLUMN,
-@@ -56,7 +63,14 @@
-         return self._translator
- 
-     def _translate_sql(self, sql):
--        return self.translator.translate(sql) if (self.env and self.env.product) else sql
-+        if self._product_prefix is empty_product:
-+            if self.env is None:
-+                self._product_prefix = None
-+            else:
-+                self._product_prefix = self.env.product.prefix \
-+                        if self.env.product else DEFAULT_PRODUCT
-+        return self.translator.translate(sql) \
-+                if self._product_prefix is not None else sql
- 
-     def execute(self, sql, args=None):
-         return super(BloodhoundIterableCursor, self).execute(self._translate_sql(sql), args=args)
+ # replace trac.test.EnvironmentStub
+ trac.test.EnvironmentStub = EnvironmentStub

t355/t355_r1437383_pkgresources_discovery.diff

+# HG changeset patch
+# Parent e15893688d86565e4e416aaa1972767a26033bb0
+BH Multiproduct #355 : Test discovery across package hierarchy
+
+diff -r e15893688d86 bloodhound_multiproduct/tests/__init__.py
+--- a/bloodhound_multiproduct/tests/__init__.py	Thu Jan 31 19:07:05 2013 -0500
++++ b/bloodhound_multiproduct/tests/__init__.py	Thu Jan 31 19:12:18 2013 -0500
+@@ -15,3 +15,62 @@
+ #  KIND, either express or implied.  See the License for the
+ #  specific language governing permissions and limitations
+ #  under the License.
++
++from collections import deque
++from fnmatch import fnmatch
++import sys
++try:
++    import unittest2 as unittest
++except ImportError:
++    import unittest
++
++from pkg_resources import resource_listdir, resource_isdir, resource_exists
++
++
++class TestLoader(unittest.TestLoader):
++    testLoaderAttribute = '__testloader__'
++    testMethodPrefix = 'test'
++    sortTestMethodsUsing = cmp
++    suiteClass = unittest.TestSuite
++
++    def discover_package(self, package_or_requirement, pattern='test*.py'):
++        """Find and return all test modules from the specified package
++        directory, recursing into subdirectories to find them. Only test files
++        that match the pattern will be loaded. (Using shell style pattern
++        matching.)
++
++        All test modules must be importable from the top level of the project
++        and registered with `pkg_resources` (e.g. via `setup.py develop`).
++
++        If a target test module contains a '__testloader__' attribute then
++        related object will override current loader for every individual 
++        module across the hierarchy.
++        """
++        pending = deque([(package_or_requirement, self, True)])
++        tests = []
++        while pending:
++            mdlnm, loader, isdir = pending.popleft()
++            mdl = self._get_module_from_name(mdlnm)
++            loader = getattr(mdl, self.testLoaderAttribute, None) or loader
++            if mdlnm != package_or_requirement and hasattr(mdl, 'test_suite'):
++                tests.append(mdl.test_suite())
++            else:
++                tests.append(loader.loadTestsFromModule(mdl))
++            if isdir and resource_exists(mdlnm, '__init__.py'):
++                for fnm in resource_listdir(mdlnm, ''):
++                    if resource_isdir(mdlnm, fnm):
++                        pending.append( (mdlnm + '.' + fnm, loader, True) )
++                    elif any(fnm.endswith(ext) for ext in ['.py', '.pyc']) \
++                            and fnmatch(fnm, pattern) and fnm != '__init__.py':
++                        submdlnm = mdlnm + '.' + fnm.rsplit('.', 1)[0]
++                        pending.append( (submdlnm, loader, False) )
++        return self.suiteClass(tests)
++
++    def _get_module_from_name(self, name):
++        __import__(name)
++        return sys.modules[name]
++
++def test_suite():
++    return TestLoader().discover_package('tests', pattern='*.py')
++
++