[py-svn] r61431 - in py/branch/pytestplugin/py: doc path path/testing test test/plugin

hpk at codespeak.net hpk at codespeak.net
Wed Jan 28 19:24:17 CET 2009


Author: hpk
Date: Wed Jan 28 19:24:13 2009
New Revision: 61431

Modified:
   py/branch/pytestplugin/py/doc/path.txt
   py/branch/pytestplugin/py/path/common.py
   py/branch/pytestplugin/py/path/testing/common.py
   py/branch/pytestplugin/py/test/collect.py
   py/branch/pytestplugin/py/test/plugin/pytest_terminal.py
Log:
make getrelpath method a general method (bestrelpath()) on path objects. 
this gets rid of another "__" import from the terminal plugin.


Modified: py/branch/pytestplugin/py/doc/path.txt
==============================================================================
--- py/branch/pytestplugin/py/doc/path.txt	(original)
+++ py/branch/pytestplugin/py/doc/path.txt	Wed Jan 28 19:24:13 2009
@@ -132,6 +132,10 @@
   >>> sep = py.path.local.sep
   >>> p2.relto(p1).replace(sep, '/') # os-specific path sep in the string
   'baz/qux'
+  >>> p2.bestrelpath(p1)
+  '../..'
+  >>> p2.join(p2.bestrelpath(p1)) == p1 
+  True
   >>> p3 = p1 / 'baz/qux' # the / operator allows joining, too
   >>> p2 == p3
   True

Modified: py/branch/pytestplugin/py/path/common.py
==============================================================================
--- py/branch/pytestplugin/py/path/common.py	(original)
+++ py/branch/pytestplugin/py/path/common.py	Wed Jan 28 19:24:13 2009
@@ -152,6 +152,30 @@
             return strself[len(strrelpath):]
         return ""
 
+    def bestrelpath(self, dest): 
+        """ return relative path from self to dest
+            such that self.join(bestrelpath) == dest. 
+            if not such path can be determined return dest. 
+        """ 
+        try:
+            base = self.common(dest)
+            if not base:  # can be the case on windows
+                return dest
+            self2base = self.relto(base)
+            reldest = dest.relto(base)
+            if self2base:
+                n = self2base.count(self.sep) + 1
+            else:
+                n = 0
+            l = ['..'] * n
+            if reldest:
+                l.append(reldest)     
+            target = dest.sep.join(l)
+            return target 
+        except AttributeError:
+            return dest
+
+
     def parts(self, reverse=False):
         """ return a root-first list of all ancestor directories
             plus the path itself.

Modified: py/branch/pytestplugin/py/path/testing/common.py
==============================================================================
--- py/branch/pytestplugin/py/path/testing/common.py	(original)
+++ py/branch/pytestplugin/py/path/testing/common.py	Wed Jan 28 19:24:13 2009
@@ -120,6 +120,18 @@
         assert self.root.check(notrelto=l)
         assert not self.root.check(relto=l)
 
+    def test_bestrelpath(self):
+        curdir = self.root
+        sep = curdir.sep
+        s = curdir.bestrelpath(curdir.join("hello", "world"))
+        assert s == "hello" + sep + "world"
+
+        s = curdir.bestrelpath(curdir.dirpath().join("sister"))
+        assert s == ".." + sep + "sister"
+        assert curdir.bestrelpath(curdir.dirpath()) == ".."
+        
+        assert curdir.bestrelpath("hello") == "hello"
+
     def test_relto_not_relative(self):
         l1=self.root.join("bcde")
         l2=self.root.join("b")

Modified: py/branch/pytestplugin/py/test/collect.py
==============================================================================
--- py/branch/pytestplugin/py/test/collect.py	(original)
+++ py/branch/pytestplugin/py/test/collect.py	Wed Jan 28 19:24:13 2009
@@ -67,7 +67,7 @@
         params = self.__dict__.copy()
         if self.fspath:
             if basedir is not None:
-                params['fspath'] = getrelpath(basedir, self.fspath)
+                params['fspath'] = basedir.bestrelpath(self.fspath)
         if self.lineno is not None:
             params['lineno'] = self.lineno + 1
 
@@ -481,27 +481,6 @@
     def runtest(self):
         """ execute this test item."""
         
-        
-def getrelpath(curdir, dest): 
-    try:
-        base = curdir.common(dest)
-        if not base:  # can be the case on windows
-            return dest
-        curdir2base = curdir.relto(base)
-        reldest = dest.relto(base)
-        if curdir2base:
-            n = curdir2base.count(curdir.sep) + 1
-        else:
-            n = 0
-        l = ['..'] * n
-        if reldest:
-            l.append(reldest)     
-        target = dest.sep.join(l)
-        return target 
-    except AttributeError:
-        return dest
-
-
 def warnoldcollect():
     APIWARN("1.0", 
         "implement collector.collect() instead of "

Modified: py/branch/pytestplugin/py/test/plugin/pytest_terminal.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_terminal.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_terminal.py	Wed Jan 28 19:24:13 2009
@@ -1,6 +1,5 @@
 import py
 import sys
-from py.__.test.collect import getrelpath
 
 class Terminal(object):
     """ Terminal Reporter plugin to write information about test run to terminal. """ 
@@ -91,7 +90,7 @@
     def write_fspath_result(self, fspath, res):
         if fspath != self.currentfspath:
             self._tw.line()
-            relpath = getrelpath(self.curdir, fspath)
+            relpath = self.curdir.bestrelpath(fspath)
             self._tw.write(relpath + " ")
             self.currentfspath = fspath
         self._tw.write(res)
@@ -707,15 +706,3 @@
         assert repr_pythonversion() == str(x) 
     finally: 
         py.magic.revert(sys, 'version_info') 
-
-def test_getrelpath():
-    curdir = py.path.local()
-    sep = curdir.sep
-    s = getrelpath(curdir, curdir.join("hello", "world"))
-    assert s == "hello" + sep + "world"
-
-    s = getrelpath(curdir, curdir.dirpath().join("sister"))
-    assert s == ".." + sep + "sister"
-    assert getrelpath(curdir, curdir.dirpath()) == ".."
-    
-    assert getrelpath(curdir, "hello") == "hello"



More information about the pytest-commit mailing list