Commits

Olemis Lang committed 6880e44

BH Multiproduct #115 : Type checking in product env initializer

Comments (0)

Files changed (1)

t115/t115_r1431447_product_envs_bep3_p1.diff

 
 diff -r 87807cda82ab bloodhound_multiproduct/multiproduct/env.py
 --- a/bloodhound_multiproduct/multiproduct/env.py	Sun Jan 13 18:57:57 2013 -0500
-+++ b/bloodhound_multiproduct/multiproduct/env.py	Sun Jan 13 23:22:00 2013 -0500
-@@ -52,11 +52,21 @@
++++ b/bloodhound_multiproduct/multiproduct/env.py	Mon Jan 14 00:11:49 2013 -0500
+@@ -52,11 +52,23 @@
  
      implements(ISystemInfoProvider)
  
          """
 -        return self.env.system_info_providers
 +        try:
++            if attrnm == 'parent':
++                raise AttributeError
 +            return getattr(self.parent, attrnm)
 +        except AttributeError:
 +            raise AttributeError("'%s' object has no attribute '%s'" %
  
      @property
      def setup_participants(self):
-@@ -86,12 +96,6 @@
+@@ -86,12 +98,6 @@
      base_url_for_redirect = ''
  
      @property
      def project_name(self):
          """Name of the product.
          """
-@@ -109,24 +113,18 @@
+@@ -109,24 +115,18 @@
          which the `base_url` resides. This is used in notification
          e-mails.
          """
  
      project_icon = Option('project', 'icon', 'common/trac.ico',
          """URL of the icon of the product.""")
-@@ -191,7 +189,7 @@
+@@ -177,6 +177,12 @@
+         :param product: product prefix or an instance of
+                         multiproduct.model.Product
+         """
++        if not isinstance(env, Environment):
++            raise TypeError("Initializer must be called with " \
++                "trac.env.Environment instance as first argument " \
++                "(got %s instance instead)" % 
++                        (self._component_name(env.__class__),) )
++
+         ComponentManager.__init__(self)
+ 
+         if isinstance(product, Product):
+@@ -191,7 +197,7 @@
                          product, products)
                  raise LookupError("Missing product %s" % (product,))
  
          self.product = product
          self.systeminfo = []
          self._href = self._abs_href = None
-@@ -200,16 +198,11 @@
+@@ -200,16 +206,12 @@
  
      # ISystemInfoProvider methods
  
 -
      # Same as parent environment's . Avoid duplicated code
      component_activated = Environment.component_activated.im_func
--    _component_name = Environment._component_name.im_func
+     _component_name = Environment._component_name.im_func
      _component_rules = Environment._component_rules
      enable_component = Environment.enable_component.im_func
      get_known_users = Environment.get_known_users.im_func
      get_repository = Environment.get_repository.im_func
      is_component_enabled = Environment.is_component_enabled.im_func
  
-@@ -359,58 +352,30 @@
+@@ -359,58 +361,30 @@
          """
          # TODO: Handle options args
  
          if format:
              format = format.replace('$(', '%(') \
                       .replace('%(path)s', self.path) \
-@@ -423,25 +388,8 @@
+@@ -423,25 +397,8 @@
          self.log.info('-' * 32 + ' environment startup [Trac %s] ' + '-' * 32,
                        get_pkginfo(core).get('version', VERSION))
  
      def upgrade(self, backup=False, backup_dest=None):
 diff -r 87807cda82ab bloodhound_multiproduct/tests/env.py
 --- a/bloodhound_multiproduct/tests/env.py	Sun Jan 13 18:57:57 2013 -0500
-+++ b/bloodhound_multiproduct/tests/env.py	Sun Jan 13 23:22:00 2013 -0500
++++ b/bloodhound_multiproduct/tests/env.py	Mon Jan 14 00:11:49 2013 -0500
 @@ -20,9 +20,17 @@
  
  import os.path
  class ProductEnvTestCase(EnvironmentTestCase, MultiproductTestCase):
      r"""Test cases for Trac environments rewritten for product environments
      """
-@@ -131,9 +147,86 @@
+@@ -131,9 +147,98 @@
  
          EnvironmentTestCase.tearDown(self)
  
 +            for attrnm in 'system_info_providers secure_cookies ' \
 +                    'project_admin_trac_url get_system_info get_version ' \
 +                    'get_templates_dir get_templates_dir get_log_dir ' \
-+                    'backup _component_name'.split(): 
++                    'backup'.split(): 
 +                original = getattr(Environment, attrnm)
 +                if isinstance(original, MethodType):
 +                    translation = getattr(self.product_env, attrnm)
 +                    self.fail("Environment member %s has unexpected type" % 
 +                            (repr(original),))
 +
-+            for attrnm in 'component_activated _component_rules ' \
-+                    'enable_component get_known_users get_repository ' \
-+                    'is_component_enabled'.split():
-+                original = getattr(Environment, attrnm)
-+                if isinstance(original, MethodType):
-+                    translation = getattr(self.product_env, attrnm)
-+                    self.assertIs(translation.im_self, self.product_env,
-+                            "'%s' not bound to product env" % (attrnm,))
-+                    self.assertIs(translation.im_func, original.im_func,
-+                            "'%s' function differs in product env" % (attrnm,))
-+                elif isinstance(original, property):
-+                    translation = getattr(ProductEnvironment, attrnm)
-+                    self.assertIs(original, translation,
-+                            "'%s' property differs in product env" % (attrnm,))
 +        finally:
 +            self.env.__class__ = EnvironmentStub
 +
++        for attrnm in 'component_activated _component_rules ' \
++                'enable_component get_known_users get_repository ' \
++                'is_component_enabled _component_name'.split():
++            original = getattr(Environment, attrnm)
++            if isinstance(original, MethodType):
++                translation = getattr(self.product_env, attrnm)
++                self.assertIs(translation.im_self, self.product_env,
++                        "'%s' not bound to product env" % (attrnm,))
++                self.assertIs(translation.im_func, original.im_func,
++                        "'%s' function differs in product env" % (attrnm,))
++            elif isinstance(original, property):
++                translation = getattr(ProductEnvironment, attrnm)
++                self.assertIs(original, translation,
++                        "'%s' property differs in product env" % (attrnm,))
++
++    def test_typecheck(self):
++        self._load_product_from_data(self.env, 'tp2')
++        with self.assertRaises(TypeError) as cm_test:
++            new_env = ProductEnvironment(self.product_env, 'tp2')
++
++        #msg = str(cm_test.exception)
++        #expected_msg = "Initializer must be called with " \
++        #        "trac.env.Environment instance as first argument " \
++        #        "(got multiproduct.env.ProductEnvironment instance instead)"
++        #self.assertEqual(msg, expected_msg)
++
 +    def tearDown(self):
 +        # Release reference to transient environment mock object
 +        self.env = None
  
 diff -r 87807cda82ab bloodhound_theme/setup.py
 --- a/bloodhound_theme/setup.py	Sun Jan 13 18:57:57 2013 -0500
-+++ b/bloodhound_theme/setup.py	Sun Jan 13 23:22:00 2013 -0500
++++ b/bloodhound_theme/setup.py	Mon Jan 14 00:11:49 2013 -0500
 @@ -19,6 +19,7 @@
  #  under the License.