| 1 |
""" !Changing this line will break Test_findfile.test_found! |
| 2 |
Non-gui unit tests for grep.GrepDialog methods. |
| 3 |
dummy_command calls grep_it calls findfiles. |
| 4 |
An exception raised in one method will fail callers. |
| 5 |
Otherwise, tests are mostly independent. |
| 6 |
Currently only test grep_it, coverage 51%. |
| 7 |
""" |
| 8 |
from idlelib import grep |
| 9 |
import unittest |
| 10 |
from test.support import captured_stdout |
| 11 |
from idlelib.idle_test.mock_tk import Var |
| 12 |
import os |
| 13 |
import re |
| 14 |
|
| 15 |
|
| 16 |
class Dummy_searchengine: |
| 17 |
'''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the |
| 18 |
passed in SearchEngine instance as attribute 'engine'. Only a few of the |
| 19 |
many possible self.engine.x attributes are needed here. |
| 20 |
''' |
| 21 |
def getpat(self): |
| 22 |
return self._pat |
| 23 |
|
| 24 |
searchengine = Dummy_searchengine() |
| 25 |
|
| 26 |
|
| 27 |
class Dummy_grep: |
| 28 |
# Methods tested |
| 29 |
#default_command = GrepDialog.default_command |
| 30 |
grep_it = grep.GrepDialog.grep_it |
| 31 |
# Other stuff needed |
| 32 |
recvar = Var(False) |
| 33 |
engine = searchengine |
| 34 |
def close(self): # gui method |
| 35 |
pass |
| 36 |
|
| 37 |
_grep = Dummy_grep() |
| 38 |
|
| 39 |
|
| 40 |
class FindfilesTest(unittest.TestCase): |
| 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) |
| 116 |
|
| 117 |
|
| 118 |
class Grep_itTest(unittest.TestCase): |
| 119 |
# Test captured reports with 0 and some hits. |
| 120 |
# Should test file names, but Windows reports have mixed / and \ separators |
| 121 |
# from incomplete replacement, so 'later'. |
| 122 |
|
| 123 |
def report(self, pat): |
| 124 |
_grep.engine._pat = pat |
| 125 |
with captured_stdout() as s: |
| 126 |
_grep.grep_it(re.compile(pat), __file__) |
| 127 |
lines = s.getvalue().split('\n') |
| 128 |
lines.pop() # remove bogus '' after last \n |
| 129 |
return lines |
| 130 |
|
| 131 |
def test_unfound(self): |
| 132 |
pat = 'xyz*'*7 |
| 133 |
lines = self.report(pat) |
| 134 |
self.assertEqual(len(lines), 2) |
| 135 |
self.assertIn(pat, lines[0]) |
| 136 |
self.assertEqual(lines[1], 'No hits.') |
| 137 |
|
| 138 |
def test_found(self): |
| 139 |
|
| 140 |
pat = '""" !Changing this line will break Test_findfile.test_found!' |
| 141 |
lines = self.report(pat) |
| 142 |
self.assertEqual(len(lines), 5) |
| 143 |
self.assertIn(pat, lines[0]) |
| 144 |
self.assertIn('py: 1:', lines[1]) # line number 1 |
| 145 |
self.assertIn('2', lines[3]) # hits found 2 |
| 146 |
self.assertStartsWith(lines[4], '(Hint:') |
| 147 |
|
| 148 |
|
| 149 |
class Default_commandTest(unittest.TestCase): |
| 150 |
# To write this, move outwin import to top of GrepDialog |
| 151 |
# so it can be replaced by captured_stdout in class setup/teardown. |
| 152 |
pass |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == '__main__': |
| 156 |
unittest.main(verbosity=2) |