diff -r 58674a027d10 Lib/idlelib/GrepDialog.py --- a/Lib/idlelib/GrepDialog.py Wed Jan 07 23:49:06 2015 -0500 +++ b/Lib/idlelib/GrepDialog.py Fri Jan 09 19:13:15 2015 +1100 @@ -105,32 +105,32 @@ # so in OW.write, OW.text.insert fails. pass - def findfiles(self, dir, base, rec): - 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 close(self, event=None): if self.top: self.top.grab_release() self.top.withdraw() +def findfiles(folder=os.curdir, pattern='*', recursive=True): + """ + A generator that returns matching file names. + + Keyword arguments: + folder -- the folder to search (default '.') + pattern -- the filename pattern to match (default '*') + recursive -- if True, recursively search subfolders (default True) + """ + try: + for dirpath, dirnames, filenames in os.walk(folder or os.curdir): + for name in filenames: + if fnmatch.fnmatch(name, pattern): + yield os.path.join(dirpath, name) + if not recursive: + break + except OSError as msg: + print(msg) + + def _grep_dialog(parent): # htest # from idlelib.PyShell import PyShellFileList root = Tk() diff -r 58674a027d10 Lib/idlelib/idle_test/test_grep.py --- a/Lib/idlelib/idle_test/test_grep.py Wed Jan 07 23:49:06 2015 -0500 +++ b/Lib/idlelib/idle_test/test_grep.py Fri Jan 09 19:13:15 2015 +1100 @@ -10,6 +10,8 @@ from idlelib.idle_test.mock_tk import Var from idlelib.GrepDialog import GrepDialog import re +import os +import idlelib.GrepDialog as GrepDialog_module class Dummy_searchengine: '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the @@ -25,7 +27,6 @@ # Methods tested #default_command = GrepDialog.default_command grep_it = GrepDialog.grep_it - findfiles = GrepDialog.findfiles # Other stuff needed recvar = Var(False) engine = searchengine @@ -35,11 +36,39 @@ grep = Dummy_grep() class FindfilesTest(unittest.TestCase): - # findfiles is really a function, not a method, could be iterator # test that filename return filename # test that idlelib has many .py files # test that recursive flag adds idle_test .py files - pass + def test_findfiles(self): + old_cwd = os.getcwd() + # change cwd to test_grep.py's folder + os.chdir(os.path.dirname(os.path.realpath(__file__))) + + # check if test_grep.py can be found + for dir_contents in (GrepDialog_module.findfiles(), + GrepDialog_module.findfiles(os.curdir), + GrepDialog_module.findfiles(os.curdir, '*'), + GrepDialog_module.findfiles(os.curdir, '*.py'), + GrepDialog_module.findfiles(os.curdir, '*.py', True), + GrepDialog_module.findfiles(os.curdir, '*.py', False),): + self.assertIn(os.path.join(os.curdir, 'test_grep.py'), dir_contents) + + # check if specifying the parent folder works + grep_dialog_file = os.path.join(os.pardir, 'GrepDialog.py') + parent_contents = GrepDialog_module.findfiles(os.pardir, '*', False) + self.assertIn(grep_dialog_file, parent_contents) + + # check that recursive=False works + has_recursed = any([name for name in parent_contents if 'idle_test' in name]) + self.assertFalse(has_recursed) + + # check if recursion works + test_grep_file = os.path.join(os.pardir, 'idle_test', 'test_grep.py') + parent_contents_rec = GrepDialog_module.findfiles(os.pardir, '*', True) + self.assertIn(test_grep_file, parent_contents_rec) + + # restore old cwd + os.chdir(old_cwd) class Grep_itTest(unittest.TestCase): # Test captured reports with 0 and some hits.