[py-svn] r10498 - in py/branch/py-collect: path/local test test/testing

hpk at codespeak.net hpk at codespeak.net
Sat Apr 9 21:42:39 CEST 2005


Author: hpk
Date: Sat Apr  9 21:42:39 2005
New Revision: 10498

Modified:
   py/branch/py-collect/path/local/local.py
   py/branch/py-collect/test/collect.py
   py/branch/py-collect/test/run.py
   py/branch/py-collect/test/session.py
   py/branch/py-collect/test/terminal.py
   py/branch/py-collect/test/testing/test_session.py
Log:
- factored the "package finding" algo out into 
  py.path.local().pypkgpath() 

- made the test collection more uniform by 
  effectively unifying direct invocations 
  of test modules and invocations from 
  directories above. 

- changed the default output of verbose==0 
  for "module summaries"

- small related fixes 


Modified: py/branch/py-collect/path/local/local.py
==============================================================================
--- py/branch/py-collect/path/local/local.py	(original)
+++ py/branch/py-collect/path/local/local.py	Sat Apr  9 21:42:39 2005
@@ -321,8 +321,34 @@
         """ return string representation of the Path. """
         return self.strpath
 
+    def pypkgpath(self, pkgname=None): 
+        """ return the path's package path by looking for the given 
+            pkgname.  If pkgname is None then look for the last 
+            directory upwards which still contains an __init__.py. 
+            Return None if a pkgpath can not be determined. 
+        """ 
+        pkgpath = None 
+        for parent in self.parts(reverse=True): 
+            if pkgname is None: 
+                if parent.check(file=1): 
+                    continue
+                if parent.join('__init__.py').check(): 
+                    pkgpath = parent 
+                    continue 
+                return pkgpath 
+            else: 
+                if parent.basename == pkgname: 
+                    return parent 
+        return pkgpath 
+
+    def _prependsyspath(self, path): 
+        s = str(path) 
+        if s != sys.path[0]: 
+            #print "prepending to sys.path", s
+            sys.path.insert(0, s) 
+
     def pyimport(self, modname=None, ensuresyspath=True): 
-        """ return the path as an imported python module. 
+        """ return path as an imported python module. 
             if modname is None, look for the containing package 
             and construct an according module name. 
             The module will be put/looked up in sys.modules. 
@@ -332,33 +358,31 @@
         #print "trying to import", self
         pkgpath = None
         if modname is None: 
-            for parent in self.parts(reverse=True)[1:]: 
-                if parent.join('__init__.py').check(): 
-                    pkgpath = parent 
-                    continue 
+            pkgpath = self.pypkgpath()
+            if pkgpath is not None: 
                 if ensuresyspath: 
-                    #print "inserting into sys.path", parent
-                    py.std.sys.path.insert(0, str(parent)) 
-                if pkgpath is not None: 
-                    pkg = __import__(pkgpath.basename, None, None, [])
-                    assert py.path.local(pkg.__file__).relto(pkgpath) 
-                    if hasattr(pkg, '__package__'): 
-                        modname = pkg.__package__.getimportname(self) 
-                        assert modname is not None, "package %s doesn't know %s" % (
-                                                    pkg.__name__, self)
-                    else: 
-                        names = self.new(ext='').relto(pkgpath.dirpath())
-                        names = names.split(self.sep) 
-                        modname = ".".join(names) 
+                    self._prependsyspath(pkgpath.dirpath())
+                pkg = __import__(pkgpath.basename, None, None, [])
+                assert py.path.local(pkg.__file__).relto(pkgpath) 
+                if hasattr(pkg, '__package__'): 
+                    modname = pkg.__package__.getimportname(self) 
+                    assert modname is not None, "package %s doesn't know %s" % (
+                                                pkg.__name__, self)
                 else: 
-                    #print "sys.path[0] is", sys.path[0]
-                    modname = self.purebasename 
-                try: 
-                    return __import__(modname, None, None, ['something']) 
-                except ImportError: 
-                    if modname in sys.modules: 
-                        del sys.modules[modname]
-                    raise 
+                    names = self.new(ext='').relto(pkgpath.dirpath())
+                    names = names.split(self.sep) 
+                    modname = ".".join(names) 
+            else: 
+                # no package scope, still make it possible 
+                if ensuresyspath: 
+                    self._prependsyspath(self.dirpath())
+                modname = self.purebasename 
+            try: 
+                return __import__(modname, None, None, ['something']) 
+            except ImportError: 
+                if modname in sys.modules: 
+                    del sys.modules[modname]
+                raise 
             raise ImportError("cannot import fspath %s" %(self,))
         else: 
             try: 

Modified: py/branch/py-collect/test/collect.py
==============================================================================
--- py/branch/py-collect/test/collect.py	(original)
+++ py/branch/py-collect/test/collect.py	Sat Apr  9 21:42:39 2005
@@ -41,9 +41,26 @@
 def getfscollector(fspath):
     if isinstance(fspath, str): 
         fspath = py.path.local(fspath)
-    col = Directory(fspath.dirpath()).join(fspath.basename) 
-    col.parent = None 
-    return col 
+    if not fspath.check(): 
+       raise py.error.ENOENT(fspath) 
+    pkgpath = fspath.pypkgpath() 
+    if pkgpath is None: 
+        x = Directory(fspath.dirpath()).join(fspath.basename)
+        x.parent = None
+        return x
+        # XXX alternatively: 
+        #if fspath.check(file=1): 
+        #    colname = 'Module' 
+        #else: 
+        #    colname = 'Directory' 
+        #col = py.test.config.getconfigvalue(fspath, colname) 
+        #return col(fspath) 
+    current = Directory(pkgpath) 
+    #print "pkgpath", pkgpath
+    for name in filter(None, fspath.relto(pkgpath).split(fspath.sep)): 
+        #print "joining", name
+        current = current.join(name) 
+    return current 
  
 class Collector(object): 
     def __init__(self, name, parent=None): 

Modified: py/branch/py-collect/test/run.py
==============================================================================
--- py/branch/py-collect/test/run.py	(original)
+++ py/branch/py-collect/test/run.py	Sat Apr  9 21:42:39 2005
@@ -124,12 +124,6 @@
             l.append(current) 
     return l
 
-def getcollectors(filenames):
-    current = py.path.local()
-    for fn in filenames:
-        fullfn = current.join(str(fn), abs=1)
-        yield getfscollector(fullfn)
-
 class FrontendSession: 
     def __init__(self, option, args): 
         self.option = option 

Modified: py/branch/py-collect/test/session.py
==============================================================================
--- py/branch/py-collect/test/session.py	(original)
+++ py/branch/py-collect/test/session.py	Sat Apr  9 21:42:39 2005
@@ -57,13 +57,14 @@
         if self.shouldclose(): 
             raise SystemExit, "received external close signal" 
 
-        if not self.option.nocapture and isinstance(colitem, py.test.Item):
+        if not self.option.nocapture: #  and isinstance(colitem, py.test.Item):
             capture = SimpleOutErrCapture() 
         else: 
             capture = None
+        
+        res = None 
         try: 
             self.start(colitem)
-            res = None 
             try: 
                 try:
                     res = self.run(colitem) 

Modified: py/branch/py-collect/test/terminal.py
==============================================================================
--- py/branch/py-collect/test/terminal.py	(original)
+++ py/branch/py-collect/test/terminal.py	Sat Apr  9 21:42:39 2005
@@ -38,19 +38,22 @@
         try: 
             numunits = len(list(colitem.iteritems()))
         except TypeError: 
-            numunits = 99999
-            
+            numunits = -1
+           
+        colitem.numunits = numunits 
         if numunits > 0:
             fspath = colitem.fspath 
             if self.option.verbose == 0: 
-                parts = fspath.parts() 
-                basename = parts.pop().basename
-                while parts and parts[-1].basename in ('testing', 'test'): 
-                    parts.pop() 
-                base = parts[-1].basename
-                if len(base) < 13: 
-                    base = base + "_" * (13-len(base))
-                abbrev_fn = base + "_" + basename 
+                # old representation 
+                #parts = fspath.parts() 
+                #basename = parts.pop().basename
+                #while parts and parts[-1].basename in ('testing', 'test'): 
+                #    parts.pop() 
+                #base = parts[-1].basename
+                #if len(base) < 13: 
+                #    base = base + "_" * (13-len(base))
+                #abbrev_fn = base + "_" + basename 
+                abbrev_fn = "/".join(colitem.listnames())
                 self.out.write('%s[%d] ' % (abbrev_fn, numunits))
             elif self.option.verbose > 0: 
                 #curdir = py.path.local()
@@ -90,7 +93,9 @@
             if self.option.exitfirstproblem:
                 py.test.exit("exit on first problem configured.", item=colitem)
         if result is None or not isinstance(colitem, py.test.Item): 
-            if isinstance(colitem, py.test.collect.Module) and self.option.verbose == 0: 
+            if isinstance(colitem, py.test.collect.Module) \
+                and self.option.verbose == 0 \
+                and colitem.numunits > 0: 
                 self.out.line() 
             return
         else: 
@@ -215,6 +220,9 @@
         traceback = excinfo.traceback
         if item: 
             self.cut_traceback(traceback, item) 
+        if not traceback: 
+            self.out.line("empty traceback from item %r" % (item,)) 
+            return
         last = traceback[-1]
         recursioncache = {}
         for entry in traceback: 

Modified: py/branch/py-collect/test/testing/test_session.py
==============================================================================
--- py/branch/py-collect/test/testing/test_session.py	(original)
+++ py/branch/py-collect/test/testing/test_session.py	Sat Apr  9 21:42:39 2005
@@ -133,4 +133,4 @@
         item, result = l[-1]
         assert item.name == 'test_4' 
         names = item.listnames()
-        assert names == ['ordertest', 'test_orderofexecution.py', 'Testmygroup', '()', 'test_4']
+        assert names == ['test_drive', 'ordertest', 'test_orderofexecution.py', 'Testmygroup', '()', 'test_4']



More information about the pytest-commit mailing list