Commit a5efa2a3 authored by Tres Seaver's avatar Tres Seaver

Start on tests for 'getPath'.

parent 36d73122
...@@ -49,7 +49,7 @@ def _getPath(home, prefix, name, suffixes): ...@@ -49,7 +49,7 @@ def _getPath(home, prefix, name, suffixes):
else: s=d else: s=d
if exists(s): return s 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 """Find a file in one of several relative locations
Arguments: Arguments:
...@@ -70,6 +70,8 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',)): ...@@ -70,6 +70,8 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',)):
suffixes -- a sequences of file suffixes to check. suffixes -- a sequences of file suffixes to check.
By default, the name is used without a suffix. 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 search takes on multiple homes which are the instance home,
the directory containing the directory containing the software the directory containing the directory containing the software
home, and possibly product areas. home, and possibly product areas.
...@@ -89,8 +91,9 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',)): ...@@ -89,8 +91,9 @@ def getPath(prefix, name, checkProduct=1, suffixes=('',)):
if r is not None: result = r if r is not None: result = r
if result is None: if result is None:
import App.config if cfg is None:
cfg = App.config.getConfiguration() import App.config
cfg = App.config.getConfiguration()
locations = [] locations = []
locations.append(cfg.instancehome) locations.append(cfg.instancehome)
sw = getattr(cfg, 'softwarehome', None) sw = getattr(cfg, 'softwarehome', None)
......
...@@ -80,9 +80,108 @@ class FuncCodeTests(unittest.TestCase): ...@@ -80,9 +80,108 @@ class FuncCodeTests(unittest.TestCase):
fc = self._makeOne(f, im=1) fc = self._makeOne(f, im=1)
fc2 = self._makeOne(g, im=1) fc2 = self._makeOne(g, im=1)
self.failUnless(cmp(fc, fc2) < 0) 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(): def test_suite():
return unittest.TestSuite(( return unittest.TestSuite((
unittest.makeSuite(FuncCodeTests), unittest.makeSuite(FuncCodeTests),
unittest.makeSuite(Test_getPath),
)) ))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment