8000 bpo-23205: IDLE: Add tests and refactor grep's findfiles by csabella · Pull Request #12203 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-23205: IDLE: Add tests and refactor grep's findfiles #12203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move findfiles to module function.
  • Loading branch information
csabella committed Mar 6, 2019
commit f8a5cb79b6c21d436c275214904326c33d508ce6
51 changes: 26 additions & 25 deletions Lib/idlelib/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ def grep(text, io=None, flist=None):
dialog.open(text, searchphrase, io)


def findfiles(dir, base, rec):
"""Return list of files in the dir that match the base pattern.

If rec is True, recursively iterate through subdirectories.
"""
try:
names = os.listdir(dir or os.curdir)
except OSError as msg:
print(msg)
return []
list = []
subdirs = []
for name in names:
fn = os.path.join(dir, name)
if os.path.isdir(fn):
subdirs.append(fn)
else:
if fnmatch.fnmatch(name, base):
list.append(fn)
if rec:
for subdir in subdirs:
list.extend(findfiles(subdir, base, rec))
return list


class GrepDialog(SearchDialogBase):
"Dialog for searching multiple files."

Expand Down Expand Up @@ -121,7 +146,7 @@ def grep_it(self, prog, path):
is an OutputWindow).
"""
dir, base = os.path.split(path)
list = self.findfiles(dir, base, self.recvar.get())
list = findfiles(dir, base, self.recvar.get())
list.sort()
self.close()
pat = self.engine.getpat()
Expand All @@ -146,30 +171,6 @@ def grep_it(self, prog, path):
# so in OW.write, OW.text.insert fails.
pass

def findfiles(self, dir, base, rec):
"""Return list of files in the dir that match the base pattern.

If rec is True, recursively iterate through subdirectories.
"""
try:
names = os.listdir(dir or os.curdir)
except OSError as msg:
print(msg)
return []
list = []
subdirs = []
for name in names:
fn = os.path.join(dir, name)
if os.path.isdir(fn):
subdirs.append(fn)
else:
if fnmatch.fnmatch(name, base):
list.append(fn)
if rec:
for subdir in subdirs:
list.extend(self.findfiles(subdir, base, rec))
return list


def _grep_dialog(parent): # htest #
from tkinter import Toplevel, Text, SEL, END
Expand Down
11 changes: 5 additions & 6 deletions Lib/idlelib/idle_test/test_grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Otherwise, tests are mostly independent.
Currently only test grep_it, coverage 51%.
"""
from idlelib.grep import GrepDialog
from idlelib import grep
import unittest
from test.support import captured_stdout
from idlelib.idle_test.mock_tk import Var
Expand All @@ -27,15 +27,14 @@ def getpat(self):
class Dummy_grep:
# Methods tested
#default_command = GrepDialog.default_command
grep_it = GrepDialog.grep_it
findfiles = GrepDialog.findfiles
grep_it = grep.GrepDialog.grep_it
# Other stuff needed
recvar = Var(False)
engine = searchengine
def close(self): # gui method
pass

grep = Dummy_grep()
_grep = Dummy_grep()


class FindfilesTest(unittest.TestCase):
Expand Down Expand Up @@ -123,9 +122,9 @@ class Grep_itTest(unittest.TestCase):
# from incomplete replacement, so 'later'.

def report(self, pat):
grep.engine._pat = pat
_grep.engine._pat = pat
with captured_stdout() as s:
grep.grep_it(re.compile(pat), __file__)
_grep.grep_it(re.compile(pat), __file__)
lines = s.getvalue().split('\n')
lines.pop() # remove bogus '' after last \n
return lines
Expand Down
0