[Zope-Checkins] SVN: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ Add hasPackage and installPackage functions for dealing with "products"

Stefan H. Holek stefan at epy.co.at
Sat Jun 23 04:40:50 EDT 2007


Log message for revision 76966:
  Add hasPackage and installPackage functions for dealing with "products"
  registered via five:registerPackage.
  

Changed:
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/__init__.py
  U   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/doc/CHANGES.txt
  A   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/
  A   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/__init__.py
  A   Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py

-=-
Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py	2007-06-23 08:40:00 UTC (rev 76965)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/ZopeLite.py	2007-06-23 08:40:48 UTC (rev 76966)
@@ -26,7 +26,6 @@
 """
 
 import os, sys, time
-import transaction
 
 # Allow code to tell it is run by the test framework
 os.environ['ZOPETESTCASE'] = '1'
@@ -133,62 +132,60 @@
 
 # Allow test authors to install Zope products into the test environment. Note
 # that installProduct() must be called at module level -- never from tests.
-from OFS.Application import get_folder_permissions, get_products, install_product
+from OFS.Application import get_folder_permissions, get_products
+from OFS.Application import install_product, install_package
 from OFS.Folder import Folder
 import Products
 
 _theApp = Zope2.app()
 _installedProducts = {}
+_installedPackages = {}
 
 def hasProduct(name):
     '''Checks if a product can be found along Products.__path__'''
     return name in [n[1] for n in get_products()]
 
-def installProduct(name, quiet=0, package=False):
+def installProduct(name, quiet=0):
     '''Installs a Zope product.'''
     start = time.time()
     meta_types = []
     if _patched and not _installedProducts.has_key(name):
-        if package:
-            # Processing of products-as-packages can be simpler; also check
-            # whether this has been registered with <five:registerPackage />
-            # and has not been loaded.
-            for module_, init_func in getattr(Products, '_packages_to_initialize', []):
-                if module_.__name__ == name:
-                    if not quiet: _print('Installing %s ... ' % name)
-                    try:
-                        product = App.Product.initializeProduct(module_, 
-                                                                module_.__name__, 
-                                                                module_.__path__[0],
-                                                                _theApp)
+        for priority, product_name, index, product_dir in get_products():
+            if product_name == name:
+                if not quiet: _print('Installing %s ... ' % product_name)
+                # We want to fail immediately if a product throws an exception
+                # during install, so we set the raise_exc flag.
+                install_product(_theApp, product_dir, product_name, meta_types,
+                                get_folder_permissions(), raise_exc=1)
+                _installedProducts[product_name] = 1
+                Products.meta_types = Products.meta_types + tuple(meta_types)
+                Globals.InitializeClass(Folder)
+                if not quiet: _print('done (%.3fs)\n' % (time.time() - start))
+                break
+        else:
+            if name != 'SomeProduct':   # Ignore the skeleton tests :-P
+                if not quiet: _print('Installing %s ... NOT FOUND\n' % name)
 
-                        product.package_name = module_.__name__
+def hasPackage(name):
+    '''Checks if a package has been registered with five:registerPackage.'''
+    return name in [m.__name__ for m, f in Products._packages_to_initialize]
 
-                        if init_func is not None:
-                            newContext = App.ProductContext.ProductContext(product, app, module_)
-                            init_func(newContext)
-                    finally:
-                        transaction.commit()
-                    
-                    Globals.InitializeClass(Folder)
-                    if not quiet: _print('done (%.3fs)\n' % (time.time() - start))
-                    break
+def installPackage(name, quiet=0):
+    '''Installs a registered Python package like a Zope product.'''
+    start = time.time()
+    if _patched and not _installedPackages.has_key(name):
+        for module, init_func in Products._packages_to_initialize:
+            if module.__name__ == name:
+                if not quiet: _print('Installing %s ... ' % module.__name__)
+                # We want to fail immediately if a package throws an exception
+                # during install, so we set the raise_exc flag.
+                install_package(_theApp, module, init_func, raise_exc=1)
+                _installedPackages[module.__name__] = 1
+                Products._packages_to_initialize.remove((module, init_func))
+                if not quiet: _print('done (%.3fs)\n' % (time.time() - start))
+                break
         else:
-            for priority, product_name, index, product_dir in get_products():
-                if product_name == name:
-                    if not quiet: _print('Installing %s ... ' % product_name)
-                    # We want to fail immediately if a product throws an exception
-                    # during install, so we set the raise_exc flag.
-                    install_product(_theApp, product_dir, product_name, meta_types,
-                                    get_folder_permissions(), raise_exc=1)
-                    _installedProducts[product_name] = 1
-                    Products.meta_types = Products.meta_types + tuple(meta_types)
-                    Globals.InitializeClass(Folder)
-                    if not quiet: _print('done (%.3fs)\n' % (time.time() - start))
-                    break
-            else:
-                if name != 'SomeProduct':   # Ignore the skeleton tests :-P
-                    if not quiet: _print('Installing %s ... NOT FOUND\n' % name)
+            if not quiet: _print('Installing %s ... NOT FOUND\n' % name)
 
 def _load_control_panel():
     # Loading the Control_Panel of an existing ZODB may take

Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/__init__.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/__init__.py	2007-06-23 08:40:00 UTC (rev 76965)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/__init__.py	2007-06-23 08:40:48 UTC (rev 76966)
@@ -20,6 +20,8 @@
 
 from ZopeLite import hasProduct
 from ZopeLite import installProduct
+from ZopeLite import hasPackage
+from ZopeLite import installPackage
 from ZopeLite import _print
 
 from ZopeTestCase import folder_name

Modified: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/doc/CHANGES.txt
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/doc/CHANGES.txt	2007-06-23 08:40:00 UTC (rev 76965)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/doc/CHANGES.txt	2007-06-23 08:40:48 UTC (rev 76966)
@@ -14,6 +14,8 @@
   publish_module(). Thanks to Andreas Zeidler.
 - Fixed doctestsuite factory to copy layers from test_class to the suite.
   Thanks to Whit Morris.
+- Added hasPackage and installPackage functions for dealing with "products"
+  registered via five:registerPackage.
 
 0.9.8 (Zope 2.8 edition)
 - Renamed 'doctest' package to 'zopedoctest' because of name-shadowing

Added: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/__init__.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/__init__.py	                        (rev 0)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/__init__.py	2007-06-23 08:40:48 UTC (rev 76966)
@@ -0,0 +1,2 @@
+def initialize(context):
+    print 'testpackage.initialize called'


Property changes on: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/testpackage/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py
===================================================================
--- Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py	                        (rev 0)
+++ Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py	2007-06-23 08:40:48 UTC (rev 76966)
@@ -0,0 +1,103 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Corporation and Contributors. 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.
+#
+##############################################################################
+"""Tests for installPackage
+
+$Id$
+"""
+
+import os, sys
+if __name__ == '__main__':
+    execfile(os.path.join(sys.path[0], 'framework.py'))
+
+from unittest import TestSuite
+from Testing.ZopeTestCase import FunctionalDocTestSuite
+from Products.Five import zcml
+from zope.testing import cleanup
+
+
+def testInstallPackage():
+    """
+    Test if installPackage works.
+
+      >>> from Testing import ZopeTestCase
+      >>> from Products.Five import zcml
+      >>> import sys, Products
+
+    Rig sys.path so testpackage can be imported
+
+      >>> saved = sys.path[:]
+      >>> sys.path.append(ZopeTestCase.__path__[0])
+
+    Register testpackage
+
+      >>> config = '''
+      ... <configure
+      ...     xmlns:five="http://namespaces.zope.org/five">
+      ...   <five:registerPackage
+      ...     package="testpackage"
+      ...     initialize="testpackage.initialize"
+      ...     />
+      ... </configure>'''
+
+      >>> ZopeTestCase.hasPackage('testpackage')
+      False
+      >>> zcml.load_string(config)
+      >>> ZopeTestCase.hasPackage('testpackage')
+      True
+
+    Not yet installed
+
+      >>> app = ZopeTestCase.app()
+      >>> 'testpackage' in app.Control_Panel.Products.objectIds()
+      False
+      >>> ZopeTestCase.close(app)
+
+    Install it
+
+      >>> ZopeTestCase.installPackage('testpackage', quiet=True)
+      testpackage.initialize called
+
+    Now it shows up in Control_Panel
+
+      >>> app = ZopeTestCase.app()
+      >>> 'testpackage' in app.Control_Panel.Products.objectIds()
+      True
+      >>> ZopeTestCase.close(app)
+
+    Clean up
+
+      >>> import testpackage
+      >>> Products._registered_packages.remove(testpackage)
+      >>> sys.path[:] = saved
+    """
+
+
+def setUp(self):
+    cleanup.cleanUp()
+    zcml._initialized = False
+    zcml.load_site()
+
+def tearDown(self):
+    cleanup.cleanUp()
+    zcml._initialized = False
+
+
+def test_suite():
+    return TestSuite((
+        # Must use functional because installPackage commits
+        FunctionalDocTestSuite(setUp=setUp, tearDown=tearDown),
+    ))
+
+if __name__ == '__main__':
+    framework()
+


Property changes on: Zope/branches/2.10/lib/python/Testing/ZopeTestCase/zopedoctest/testPackageAsProduct.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope-Checkins mailing list