[py-svn] py-trunk commit 742f38d8eb11: allow a path to explicity given for py.lookup

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Sep 23 04:08:35 CEST 2009


# HG changeset patch -- Bitbucket.org
# Project py-trunk
# URL http://bitbucket.org/hpk42/py-trunk/overview/
# User Benjamin Peterson <benjamin at python.org>
# Date 1253671465 18000
# Node ID 742f38d8eb11e1f32183b15f2de7c2997cd0ff9e
# Parent 917a59d062d50efafe444327f2986d2d6eb5dcd7
allow a path to explicity given for py.lookup

--- a/testing/cmdline/test_cmdline.py
+++ b/testing/cmdline/test_cmdline.py
@@ -15,4 +15,14 @@ class TestPyLookup:
         result.stdout.fnmatch_lines(
             ['*%s:*' %(p.basename)]
         )
-        
+
+    def test_with_explicit_path(self, testxbdir):
+        sub1 = testdir.mkdir("things")
+        sub2 = testdir.mkdir("foo")
+        sub1.join("pyfile.py").write("def stuff(): pass")
+        searched = sub2.join("other.py")
+        searched.write("stuff = x")
+        result = testdir.runpybin("py.lookup", sub2.basename, "stuff")
+        result.stdout.fnmatch_lines(
+            ["%s:1: stuff = x" % (searched.basename,)]
+        )

--- a/py/cmdline/pylookup.py
+++ b/py/cmdline/pylookup.py
@@ -12,7 +12,6 @@ import py
 from py.__.io.terminalwriter import ansi_print, terminal_width
 import re
 
-curdir = py.path.local()
 def rec(p):
     return p.check(dotfile=0)
 
@@ -35,12 +34,17 @@ def find_indexes(search_line, string):
 
 def main():
     (options, args) = parser.parse_args()
-    string = args[0]
+    if len(args) == 2:
+        search_dir, string = args
+        search_dir = py.path.local(search_dir)
+    else:
+        search_dir = py.path.local()
+        string = args[0]
     if options.ignorecase:
         string = string.lower()
-    for x in curdir.visit('*.py', rec):
+    for x in search_dir.visit('*.py', rec):
         # match filename directly
-        s = x.relto(curdir)
+        s = x.relto(search_dir)
         if options.ignorecase:
             s = s.lower()
         if s.find(string) != -1:
@@ -64,7 +68,7 @@ def main():
                 if not indexes:
                     continue
                 if not options.context:
-                    sys.stdout.write("%s:%d: " %(x.relto(curdir), i+1))
+                    sys.stdout.write("%s:%d: " %(x.relto(search_dir), i+1))
                     last_index = 0
                     for index in indexes:
                         sys.stdout.write(line[last_index: index])
@@ -75,5 +79,5 @@ def main():
                 else:
                     context = (options.context)/2
                     for count in range(max(0, i-context), min(len(lines) - 1, i+context+1)):
-                        print("%s:%d:  %s" %(x.relto(curdir), count+1, lines[count].rstrip()))
+                        print("%s:%d:  %s" %(x.relto(search_dir), count+1, lines[count].rstrip()))
                     print("-" * terminal_width)



More information about the pytest-commit mailing list