Commits

Olemis Lang committed 355b48c

BH Multiproduct #355 : Enhanced algorithm to determine whether component is disabled. Test case for environment configuration inheritance

Comments (0)

Files changed (2)

 #t355/t355_r1437383_pkgresources_discovery.diff
 #t355/t355_r1437383_trac_test_perm.diff
 #t355/t355_r1437383_trac_test_ticket_api.diff
+t355/t355_r1442601_trac_test_env_cmp_enabled.diff
 t355/t355_r1442601_trac_test_ticket_conversion.diff
 t355/t355_r1442601_trac_test_ticket_model.diff
 #t355/t355_r1442601_trac_test_ticket_batch.diff

t355/t355_r1442601_trac_test_env_cmp_enabled.diff

+# HG changeset patch
+# Parent 25f5af82c0ce83f1318165a3a2ffce8bdc4f705d
+BH Multiproduct #355 : ProductEnvironment.is_conponent_enabled enhanced to cope with trac.* built-in components
+
+diff -r 25f5af82c0ce bloodhound_multiproduct/multiproduct/env.py
+--- a/bloodhound_multiproduct/multiproduct/env.py	Thu Feb 07 14:50:28 2013 +0000
++++ b/bloodhound_multiproduct/multiproduct/env.py	Fri Feb 08 00:51:50 2013 -0500
+@@ -117,6 +117,40 @@
+         except OperationalError:
+             pass
+ 
++    def enable_component_in_config(self, cls):
++        """Keep track of enabled state in configuration as well 
++        during test runs. This is closer to reality than 
++        inherited `enable_component` method.
++        """
++        self.config['components'].set(self._component_name(cls), 'enabled')
++        self.enabled.clear()
++        self.components.pop(cls, None)
++        try:
++            del self._rules
++        except AttributeError:
++            pass
++        # FIXME: Shall we ?
++        #self.config.save()
++
++    def disable_component_in_config(self, component):
++        """Keep track of disabled state in configuration as well 
++        during test runs. This is closer to reality than 
++        inherited `disable_component` method.
++        """
++        if isinstance(component, type):
++            cls = component
++        else:
++            cls = component.__class__
++        self.config['components'].set(self._component_name(cls), 'disabled')
++        self.enabled.clear()
++        self.components.pop(cls, None)
++        try:
++            del self._rules
++        except AttributeError:
++            pass
++        # FIXME: Shall we ?
++        #self.config.save()
++
+     def reset_db(self, default_data=None):
+         from multiproduct.api import DB_VERSION
+         schema_version = -1
+@@ -127,6 +161,7 @@
+             with self.db_direct_transaction as db:
+                 self.mpsystem._update_db_version(db, DB_VERSION)
+ 
++
+ # replace trac.test.EnvironmentStub
+ trac.test.EnvironmentStub = EnvironmentStub
+ 
+@@ -337,7 +372,7 @@
+         `None`, the component only gets activated if it is located in
+         the `plugins` directory of the environment.
+         """
+-        if not self.parent.is_component_enabled(cls):
++        if self.parent[cls] is None:
+             return False
+         return self.is_component_enabled_local(cls)
+ 
+diff -r 25f5af82c0ce bloodhound_multiproduct/tests/env.py
+--- a/bloodhound_multiproduct/tests/env.py	Thu Feb 07 14:50:28 2013 +0000
++++ b/bloodhound_multiproduct/tests/env.py	Fri Feb 08 00:51:50 2013 -0500
+@@ -38,6 +38,8 @@
+ from trac.env import Environment
+ from trac.test import EnvironmentStub
+ from trac.tests.env import EnvironmentTestCase
++from trac.ticket.report import ReportModule
++from trac.ticket.web_ui import TicketModule
+ 
+ from multiproduct.api import MultiProductSystem
+ from multiproduct.env import ProductEnvironment
+@@ -159,6 +161,8 @@
+     def _upgrade_mp(self, env):
+         r"""Apply multi product upgrades
+         """
++        env.disable_component(TicketModule)
++        env.disable_component(ReportModule)
+         self.mpsystem = MultiProductSystem(env)
+         try:
+             self.mpsystem.upgrade_environment(env.db_transaction)
+@@ -215,6 +219,13 @@
+ 
+     def tearDown(self):
+         # Release reference to transient environment mock object
++        if self.env is not None:
++            try:
++                self.env.reset_db()
++            except OperationalError:
++                # "Database not found ...",
++                # "OperationalError: no such table: system" or the like
++                pass
+         self.env = None
+         self.product_env = None
+ 
+@@ -303,53 +314,84 @@
+ 
+         global_env = self.env
+         product_env = self.product_env
+-        
+-        def clear_component_rules(env):
+-            del env._rules
+-            env.enabled.clear()
+ 
+-        # C initially disabled in both envs
+-        self.assertFalse(global_env.is_component_enabled(C))
+-        self.assertFalse(product_env.is_component_enabled_local(C))
+-        self.assertIs(global_env[C], None)
+-        self.assertIs(product_env[C], None)
++        def _test_component_enabled(cls):
++            cname = global_env._component_name(cls)
+ 
+-        clear_component_rules(global_env)
+-        clear_component_rules(product_env)
++            # cls initially disabled in both envs
++            global_env.disable_component_in_config(cls)
++            product_env.disable_component_in_config(cls)
+ 
+-        # C enabled in product env but not in global env
+-        product_env.enable_component(C)
+-        self.assertFalse(global_env.is_component_enabled(C))
+-        self.assertTrue(product_env.is_component_enabled_local(C))
+-        self.assertIs(global_env[C], None)
+-        self.assertIs(product_env[C], None)
++            expected_rules = {
++                    'multiproduct' : True,
++                    'trac' : True,
++                    'trac.db' : True,
++                    cname : False,
++                }
++            self.assertEquals(expected_rules, global_env._component_rules)
++            self.assertEquals(expected_rules, product_env._component_rules)
++            self.assertFalse(global_env.is_component_enabled(cls))
++            self.assertFalse(product_env.is_component_enabled_local(cls))
++            self.assertIs(global_env[cls], None)
++            self.assertIs(product_env[cls], None)
+ 
+-        clear_component_rules(global_env)
+-        clear_component_rules(product_env)
++            # cls enabled in product env but not in global env
++            global_env.disable_component_in_config(cls)
++            expected_rules[cname] = False
++            self.assertEquals(expected_rules, global_env._component_rules)
++            product_env.enable_component_in_config(cls)
+ 
+-        # C enabled in both envs
+-        product_env.enable_component(C)
+-        global_env.enable_component(C)
+-        self.assertTrue(global_env.is_component_enabled(C))
+-        self.assertTrue(product_env.is_component_enabled_local(C))
+-        self.assertIsNot(global_env[C], None)
+-        self.assertIsNot(product_env[C], None)
++            expected_rules[cname] = False
++            self.assertEquals(expected_rules, global_env._component_rules)
++            expected_rules[cname] = True
++            self.assertEquals(expected_rules, product_env._component_rules)
++            self.assertFalse(global_env.is_component_enabled(cls))
++            self.assertTrue(product_env.is_component_enabled_local(cls))
++            self.assertIs(global_env[cls], None)
++            self.assertIs(product_env[cls], None)
+ 
+-        clear_component_rules(global_env)
+-        clear_component_rules(product_env)
++            # cls enabled in both envs
++            global_env.enable_component_in_config(cls)
++            product_env.enable_component_in_config(cls)
++            self.assertTrue(global_env.is_component_enabled(cls))
++            self.assertTrue(product_env.is_component_enabled_local(cls))
++            self.assertIsNot(global_env[cls], None)
++            self.assertIsNot(product_env[cls], None)
+ 
+-        # C enabled in global env but not in product env
+-        global_env.enable_component(C)
+-        self.assertTrue(global_env.is_component_enabled(C))
+-        self.assertFalse(product_env.is_component_enabled_local(C))
+-        self.assertIsNot(global_env[C], None)
+-        self.assertIs(product_env[C], None)
++            # cls enabled in global env but not in product env
++            global_env.enable_component_in_config(cls)
++            product_env.disable_component_in_config(cls)
++            self.assertTrue(global_env.is_component_enabled(cls))
++            self.assertFalse(product_env.is_component_enabled_local(cls))
++            self.assertIsNot(global_env[cls], None)
++            self.assertIs(product_env[cls], None)
++
++        # Test the rules against custom , external component
++        _test_component_enabled(C)
++        # Test the rules against Trac component class
++        _test_component_enabled(TicketModule)
+ 
+     def test_path(self):
+         """Testing env.path"""
+         self.assertEqual(self.product_env.path, 
+                 os.path.join(self.env.path, 'products', self.default_product))
+ 
++    def test_env_config_inheritance(self):
++        """Testing env.config"""
++        global_config = self.env.config
++        product_config = self.product_env.config
++
++        # By default inherit global settings ...
++        global_config['section'].set('key', 'value1')
++        self.assertEquals('value1', global_config['section'].get('key'))
++        self.assertEquals('value1', product_config['section'].get('key'))
++
++        # ... but allow for overrides in product scope
++        product_config['section'].set('key', 'value2')
++        self.assertEquals('value1', global_config['section'].get('key'))
++        self.assertEquals('value2', product_config['section'].get('key'))
++
++
+ def test_suite():
+     return unittest.TestSuite([
+             unittest.makeSuite(ProductEnvTestCase,'test'),