test_grep.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class Dummy_searchengine:
  15. '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the
  16. passed in SearchEngine instance as attribute 'engine'. Only a few of the
  17. many possible self.engine.x attributes are needed here.
  18. '''
  19. def getpat(self):
  20. return self._pat
  21. searchengine = Dummy_searchengine()
  22. class Dummy_grep:
  23. # Methods tested
  24. #default_command = GrepDialog.default_command
  25. grep_it = grep.GrepDialog.grep_it
  26. # Other stuff needed
  27. recvar = Var(False)
  28. engine = searchengine
  29. def close(self): # gui method
  30. pass
  31. _grep = Dummy_grep()
  32. class FindfilesTest(unittest.TestCase):
  33. @classmethod
  34. def setUpClass(cls):
  35. cls.realpath = os.path.realpath(__file__)
  36. cls.path = os.path.dirname(cls.realpath)
  37. @classmethod
  38. def tearDownClass(cls):
  39. del cls.realpath, cls.path
  40. def test_invaliddir(self):
  41. with captured_stdout() as s:
  42. filelist = list(grep.findfiles('invaliddir', '*.*', False))
  43. self.assertEqual(filelist, [])
  44. self.assertIn('invalid', s.getvalue())
  45. def test_curdir(self):
  46. # Test os.curdir.
  47. ff = grep.findfiles
  48. save_cwd = os.getcwd()
  49. os.chdir(self.path)
  50. filename = 'test_grep.py'
  51. filelist = list(ff(os.curdir, filename, False))
  52. self.assertIn(os.path.join(os.curdir, filename), filelist)
  53. os.chdir(save_cwd)
  54. def test_base(self):
  55. ff = grep.findfiles
  56. readme = os.path.join(self.path, 'README.txt')
  57. # Check for Python files in path where this file lives.
  58. filelist = list(ff(self.path, '*.py', False))
  59. # This directory has many Python files.
  60. self.assertGreater(len(filelist), 10)
  61. self.assertIn(self.realpath, filelist)
  62. self.assertNotIn(readme, filelist)
  63. # Look for .txt files in path where this file lives.
  64. filelist = list(ff(self.path, '*.txt', False))
  65. self.assertNotEqual(len(filelist), 0)
  66. self.assertNotIn(self.realpath, filelist)
  67. self.assertIn(readme, filelist)
  68. # Look for non-matching pattern.
  69. filelist = list(ff(self.path, 'grep.*', False))
  70. self.assertEqual(len(filelist), 0)
  71. self.assertNotIn(self.realpath, filelist)
  72. def test_recurse(self):
  73. ff = grep.findfiles
  74. parent = os.path.dirname(self.path)
  75. grepfile = os.path.join(parent, 'grep.py')
  76. pat = '*.py'
  77. # Get Python files only in parent directory.
  78. filelist = list(ff(parent, pat, False))
  79. parent_size = len(filelist)
  80. # Lots of Python files in idlelib.
  81. self.assertGreater(parent_size, 20)
  82. self.assertIn(grepfile, filelist)
  83. # Without subdirectories, this file isn't returned.
  84. self.assertNotIn(self.realpath, filelist)
  85. # Include subdirectories.
  86. filelist = list(ff(parent, pat, True))
  87. # More files found now.
  88. self.assertGreater(len(filelist), parent_size)
  89. self.assertIn(grepfile, filelist)
  90. # This file exists in list now.
  91. self.assertIn(self.realpath, filelist)
  92. # Check another level up the tree.
  93. parent = os.path.dirname(parent)
  94. filelist = list(ff(parent, '*.py', True))
  95. self.assertIn(self.realpath, filelist)
  96. class Grep_itTest(unittest.TestCase):
  97. # Test captured reports with 0 and some hits.
  98. # Should test file names, but Windows reports have mixed / and \ separators
  99. # from incomplete replacement, so 'later'.
  100. def report(self, pat):
  101. _grep.engine._pat = pat
  102. with captured_stdout() as s:
  103. _grep.grep_it(re.compile(pat), __file__)
  104. lines = s.getvalue().split('\n')
  105. lines.pop() # remove bogus '' after last \n
  106. return lines
  107. def test_unfound(self):
  108. pat = 'xyz*'*7
  109. lines = self.report(pat)
  110. self.assertEqual(len(lines), 2)
  111. self.assertIn(pat, lines[0])
  112. self.assertEqual(lines[1], 'No hits.')
  113. def test_found(self):
  114. pat = '""" !Changing this line will break Test_findfile.test_found!'
  115. lines = self.report(pat)
  116. self.assertEqual(len(lines), 5)
  117. self.assertIn(pat, lines[0])
  118. self.assertIn('py: 1:', lines[1]) # line number 1
  119. self.assertIn('2', lines[3]) # hits found 2
  120. self.assertTrue(lines[4].startswith('(Hint:'))
  121. class Default_commandTest(unittest.TestCase):
  122. # To write this, move outwin import to top of GrepDialog
  123. # so it can be replaced by captured_stdout in class setup/teardown.
  124. pass
  125. if __name__ == '__main__':
  126. unittest.main(verbosity=2)