55Otherwise, tests are mostly independent.
66Currently only test grep_it, coverage 51%.
77"""
8- from idlelib . grep import GrepDialog
8+ from idlelib import grep
99import unittest
1010from test .support import captured_stdout
1111from idlelib .idle_test .mock_tk import Var
12+ import os
1213import re
1314
1415
@@ -26,23 +27,92 @@ def getpat(self):
2627class Dummy_grep :
2728 # Methods tested
2829 #default_command = GrepDialog.default_command
29- grep_it = GrepDialog .grep_it
30- findfiles = GrepDialog .findfiles
30+ grep_it = grep .GrepDialog .grep_it
3131 # Other stuff needed
3232 recvar = Var (False )
3333 engine = searchengine
3434 def close (self ): # gui method
3535 pass
3636
37- grep = Dummy_grep ()
37+ _grep = Dummy_grep ()
3838
3939
4040class FindfilesTest (unittest .TestCase ):
41- # findfiles is really a function, not a method, could be iterator
42- # test that filename return filename
43- # test that idlelib has many .py files
44- # test that recursive flag adds idle_test .py files
45- pass
41+
42+ @classmethod
43+ def setUpClass (cls ):
44+ cls .realpath = os .path .realpath (__file__ )
45+ cls .path = os .path .dirname (cls .realpath )
46+
47+ @classmethod
48+ def tearDownClass (cls ):
49+ del cls .realpath , cls .path
50+
51+ def test_invaliddir (self ):
52+ with captured_stdout () as s :
53+ filelist = list (grep .findfiles ('invaliddir' , '*.*' , False ))
54+ self .assertEqual (filelist , [])
55+ self .assertIn ('invalid' , s .getvalue ())
56+
57+ def test_curdir (self ):
58+ # Test os.curdir.
59+ ff = grep .findfiles
60+ save_cwd = os .getcwd ()
61+ os .chdir (self .path )
62+ filename = 'test_grep.py'
63+ filelist = list (ff (os .curdir , filename , False ))
64+ self .assertIn (os .path .join (os .curdir , filename ), filelist )
65+ os .chdir (save_cwd )
66+
67+ def test_base (self ):
68+ ff = grep .findfiles
69+ readme = os .path .join (self .path , 'README.txt' )
70+
71+ # Check for Python files in path where this file lives.
72+ filelist = list (ff (self .path , '*.py' , False ))
73+ # This directory has many Python files.
74+ self .assertGreater (len (filelist ), 10 )
75+ self .assertIn (self .realpath , filelist )
76+ self .assertNotIn (readme , filelist )
77+
78+ # Look for .txt files in path where this file lives.
79+ filelist = list (ff (self .path , '*.txt' , False ))
80+ self .assertNotEqual (len (filelist ), 0 )
81+ self .assertNotIn (self .realpath , filelist )
82+ self .assertIn (readme , filelist )
83+
84+ # Look for non-matching pattern.
85+ filelist = list (ff (self .path , 'grep.*' , False ))
86+ self .assertEqual (len (filelist ), 0 )
87+ self .assertNotIn (self .realpath , filelist )
88+
89+ def test_recurse (self ):
90+ ff = grep .findfiles
91+ parent = os .path .dirname (self .path )
92+ grepfile = os .path .join (parent , 'grep.py' )
93+ pat = '*.py'
94+
95+ # Get Python files only in parent directory.
96+ filelist = list (ff (parent , pat , False ))
97+ parent_size = len (filelist )
98+ # Lots of Python files in idlelib.
99+ self .assertGreater (parent_size , 20 )
100+ self .assertIn (grepfile , filelist )
101+ # Without subdirectories, this file isn't returned.
102+ self .assertNotIn (self .realpath , filelist )
103+
104+ # Include subdirectories.
105+ filelist = list (ff (parent , pat , True ))
106+ # More files found now.
107+ self .assertGreater (len (filelist ), parent_size )
108+ self .assertIn (grepfile , filelist )
109+ # This file exists in list now.
110+ self .assertIn (self .realpath , filelist )
111+
112+ # Check another level up the tree.
113+ parent = os .path .dirname (parent )
114+ filelist = list (ff (parent , '*.py' , True ))
115+ self .assertIn (self .realpath , filelist )
46116
47117
48118class Grep_itTest (unittest .TestCase ):
@@ -51,9 +121,9 @@ class Grep_itTest(unittest.TestCase):
51121 # from incomplete replacement, so 'later'.
52122
53123 def report (self , pat ):
54- grep .engine ._pat = pat
124+ _grep .engine ._pat = pat
55125 with captured_stdout () as s :
56- grep .grep_it (re .compile (pat ), __file__ )
126+ _grep .grep_it (re .compile (pat ), __file__ )
57127 lines = s .getvalue ().split ('\n ' )
58128 lines .pop () # remove bogus '' after last \n
59129 return lines
0 commit comments