Log message for revision 110486:
  Start on tests for 'getPath'.

Changed:
  U   Zope/trunk/src/App/Extensions.py
  U   Zope/trunk/src/App/tests/test_Extensions.py

-=-
Modified: Zope/trunk/src/App/Extensions.py
===================================================================
--- Zope/trunk/src/App/Extensions.py    2010-04-04 00:06:50 UTC (rev 110485)
+++ Zope/trunk/src/App/Extensions.py    2010-04-04 11:48:48 UTC (rev 110486)
@@ -49,7 +49,7 @@
         else: s=d
         if exists(s): return s
 
-def getPath(prefix, name, checkProduct=1, suffixes=('',)):
+def getPath(prefix, name, checkProduct=1, suffixes=('',), cfg=None):
     """Find a file in one of several relative locations
 
     Arguments:
@@ -70,6 +70,8 @@
       suffixes -- a sequences of file suffixes to check.
         By default, the name is used without a suffix.
 
+      cfg -- ease testing (not part of the API)
+
     The search takes on multiple homes which are the instance home,
     the directory containing the directory containing the software
     home, and possibly product areas.
@@ -89,8 +91,9 @@
                 if r is not None: result = r
 
     if result is None:
-        import App.config
-        cfg = App.config.getConfiguration()
+        if cfg is None:
+            import App.config
+            cfg = App.config.getConfiguration()
         locations = []
         locations.append(cfg.instancehome)
         sw = getattr(cfg, 'softwarehome', None)

Modified: Zope/trunk/src/App/tests/test_Extensions.py
===================================================================
--- Zope/trunk/src/App/tests/test_Extensions.py 2010-04-04 00:06:50 UTC (rev 
110485)
+++ Zope/trunk/src/App/tests/test_Extensions.py 2010-04-04 11:48:48 UTC (rev 
110486)
@@ -80,9 +80,108 @@
         fc = self._makeOne(f, im=1)
         fc2 = self._makeOne(g, im=1)
         self.failUnless(cmp(fc, fc2) < 0)
-        
+ 
+class Test_getPath(unittest.TestCase):
 
+    _old_Products___path__ = None
+    _tmpdirs = ()
+
+    def tearDown(self):
+        import shutil
+        if self._old_Products___path__ is not None:
+            import Products
+            Products.__path__ = self._old_Products___path__
+        for tmpdir in self._tmpdirs:
+            shutil.rmtree(tmpdir)
+
+    def _callFUT(self, prefix, name, checkProduct=1, suffixes=('',), cfg=None):
+        from App.Extensions import getPath
+        return getPath(prefix, name, checkProduct, suffixes, cfg)
+
+    def _makeTempdir(self):
+        import tempfile
+        tmp = tempfile.mkdtemp()
+        self._tmpdirs += (tmp,)
+        return tmp
+
+    def _makeTempExtension(self, name='foo', extname='Extensions', dir=None):
+        import os
+        if dir is None:
+            dir = self._makeTempdir()
+        if name is None:
+            extdir = os.path.join(dir, extname)
+        else:
+            extdir = os.path.join(dir, name, extname)
+        os.makedirs(extdir)
+        return extdir
+
+    def _makeTempProduct(self, name='foo', extname='Extensions'):
+        import Products
+        root = self._makeTempdir()
+        pdir = self._makeTempExtension(name=name, extname=extname, dir=root)
+        Products.__path__ = (root,)
+        return pdir
+
+    def _makeFile(self, dir, name, text='#extension'):
+        import os
+        fqn = os.path.join(dir, name)
+        f = open(fqn, 'w')
+        f.write(text)
+        f.flush()
+        f.close()
+        return fqn
+
+    def _makeConfig(self, **kw):
+        class DummyConfig:
+            def __init__(self, **kw):
+                self.__dict__.update(kw)
+        return DummyConfig(**kw)
+
+    def test_name_as_path_raises(self):
+        self.assertRaises(ValueError, self._callFUT, 'Extensions', 'foo/bar')
+
+    def test_found_in_product(self):
+        instdir = self._makeTempdir()
+        swdir = self._makeTempdir()
+        cfg = self._makeConfig(instancehome=instdir,
+                               softwarehome=swdir,
+                              )
+        extdir = self._makeTempProduct()
+        ext = self._makeFile(extdir, 'extension.py')
+        path = self._callFUT('Extensions', 'foo.extension',
+                             suffixes=('py',), cfg=cfg)
+        self.assertEqual(path, ext)
+
+    def test_not_found_in_product(self):
+        instdir = self._makeTempdir()
+        swdir = self._makeTempdir()
+        cfg = self._makeConfig(instancehome=instdir,
+                               softwarehome=swdir,
+                              )
+        extdir = self._makeTempProduct()
+        ext = self._makeFile(extdir, 'extension.py')
+        path = self._callFUT('Extensions', 'foo.other',
+                             suffixes=('py',), cfg=cfg)
+        self.assertEqual(path, None)
+
+    def test_wo_checkProduct_skips_product(self):
+        import Products
+        del Products.__path__ # so any iteration will raise
+        instdir = self._makeTempdir()
+        instext = self._makeTempExtension(name=None, dir=instdir)
+        instfqn = self._makeFile(instext, 'extension.py')
+        swdir = self._makeTempdir()
+        swext = self._makeTempExtension(name=None, dir=swdir)
+        swfqn = self._makeFile(swext, 'extension.py')
+        cfg = self._makeConfig(instancehome=instdir,
+                                  softwarehome=swdir,
+                                 )
+        path = self._callFUT('Extensions', 'extension', checkProduct=0,
+                             suffixes=('py',), cfg=cfg)
+        self.assertEqual(path, instfqn)
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(FuncCodeTests),
+        unittest.makeSuite(Test_getPath),
     ))

_______________________________________________
Zope-Checkins maillist  -  Zope-Checkins@zope.org
https://mail.zope.org/mailman/listinfo/zope-checkins

Reply via email to