test_files.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Tests for files.py"""
  4. import os
  5. import os.path
  6. from coverage import files
  7. from coverage.files import (
  8. TreeMatcher, FnmatchMatcher, ModuleMatcher, PathAliases,
  9. find_python_files, abs_file, actual_path, flat_rootname,
  10. )
  11. from coverage.misc import CoverageException
  12. from coverage import env
  13. from tests.coveragetest import CoverageTest
  14. class FilesTest(CoverageTest):
  15. """Tests of coverage.files."""
  16. def abs_path(self, p):
  17. """Return the absolute path for `p`."""
  18. return os.path.join(os.getcwd(), os.path.normpath(p))
  19. def test_simple(self):
  20. self.make_file("hello.py")
  21. files.set_relative_directory()
  22. self.assertEqual(files.relative_filename(u"hello.py"), u"hello.py")
  23. a = self.abs_path("hello.py")
  24. self.assertNotEqual(a, "hello.py")
  25. self.assertEqual(files.relative_filename(a), "hello.py")
  26. def test_peer_directories(self):
  27. self.make_file("sub/proj1/file1.py")
  28. self.make_file("sub/proj2/file2.py")
  29. a1 = self.abs_path("sub/proj1/file1.py")
  30. a2 = self.abs_path("sub/proj2/file2.py")
  31. d = os.path.normpath("sub/proj1")
  32. os.chdir(d)
  33. files.set_relative_directory()
  34. self.assertEqual(files.relative_filename(a1), "file1.py")
  35. self.assertEqual(files.relative_filename(a2), a2)
  36. def test_filepath_contains_absolute_prefix_twice(self):
  37. # https://bitbucket.org/ned/coveragepy/issue/194
  38. # Build a path that has two pieces matching the absolute path prefix.
  39. # Technically, this test doesn't do that on Windows, but drive
  40. # letters make that impractical to achieve.
  41. files.set_relative_directory()
  42. d = abs_file(os.curdir)
  43. trick = os.path.splitdrive(d)[1].lstrip(os.path.sep)
  44. rel = os.path.join('sub', trick, 'file1.py')
  45. self.assertEqual(files.relative_filename(abs_file(rel)), rel)
  46. def test_flat_rootname(self):
  47. self.assertEqual(flat_rootname("a/b/c.py"), "a_b_c_py")
  48. self.assertEqual(flat_rootname(r"c:\foo\bar.html"), "_foo_bar_html")
  49. class MatcherTest(CoverageTest):
  50. """Tests of file matchers."""
  51. def setUp(self):
  52. super(MatcherTest, self).setUp()
  53. files.set_relative_directory()
  54. def assertMatches(self, matcher, filepath, matches):
  55. """The `matcher` should agree with `matches` about `filepath`."""
  56. canonical = files.canonical_filename(filepath)
  57. self.assertEqual(
  58. matcher.match(canonical), matches,
  59. "File %s should have matched as %s" % (filepath, matches)
  60. )
  61. def test_tree_matcher(self):
  62. matches_to_try = [
  63. (self.make_file("sub/file1.py"), True),
  64. (self.make_file("sub/file2.c"), True),
  65. (self.make_file("sub2/file3.h"), False),
  66. (self.make_file("sub3/file4.py"), True),
  67. (self.make_file("sub3/file5.c"), False),
  68. ]
  69. trees = [
  70. files.canonical_filename("sub"),
  71. files.canonical_filename("sub3/file4.py"),
  72. ]
  73. tm = TreeMatcher(trees)
  74. self.assertEqual(tm.info(), trees)
  75. for filepath, matches in matches_to_try:
  76. self.assertMatches(tm, filepath, matches)
  77. def test_module_matcher(self):
  78. matches_to_try = [
  79. ('test', True),
  80. ('trash', False),
  81. ('testing', False),
  82. ('test.x', True),
  83. ('test.x.y.z', True),
  84. ('py', False),
  85. ('py.t', False),
  86. ('py.test', True),
  87. ('py.testing', False),
  88. ('py.test.buz', True),
  89. ('py.test.buz.baz', True),
  90. ('__main__', False),
  91. ('mymain', True),
  92. ('yourmain', False),
  93. ]
  94. modules = ['test', 'py.test', 'mymain']
  95. mm = ModuleMatcher(modules)
  96. self.assertEqual(
  97. mm.info(),
  98. modules
  99. )
  100. for modulename, matches in matches_to_try:
  101. self.assertEqual(
  102. mm.match(modulename),
  103. matches,
  104. modulename,
  105. )
  106. def test_fnmatch_matcher(self):
  107. matches_to_try = [
  108. (self.make_file("sub/file1.py"), True),
  109. (self.make_file("sub/file2.c"), False),
  110. (self.make_file("sub2/file3.h"), True),
  111. (self.make_file("sub3/file4.py"), True),
  112. (self.make_file("sub3/file5.c"), False),
  113. ]
  114. fnm = FnmatchMatcher(["*.py", "*/sub2/*"])
  115. self.assertEqual(fnm.info(), ["*.py", "*/sub2/*"])
  116. for filepath, matches in matches_to_try:
  117. self.assertMatches(fnm, filepath, matches)
  118. def test_fnmatch_matcher_overload(self):
  119. fnm = FnmatchMatcher(["*x%03d*.txt" % i for i in range(500)])
  120. self.assertMatches(fnm, "x007foo.txt", True)
  121. self.assertMatches(fnm, "x123foo.txt", True)
  122. self.assertMatches(fnm, "x798bar.txt", False)
  123. def test_fnmatch_windows_paths(self):
  124. # We should be able to match Windows paths even if we are running on
  125. # a non-Windows OS.
  126. fnm = FnmatchMatcher(["*/foo.py"])
  127. self.assertMatches(fnm, r"dir\foo.py", True)
  128. fnm = FnmatchMatcher([r"*\foo.py"])
  129. self.assertMatches(fnm, r"dir\foo.py", True)
  130. class PathAliasesTest(CoverageTest):
  131. """Tests for coverage/files.py:PathAliases"""
  132. run_in_temp_dir = False
  133. def assert_mapped(self, aliases, inp, out):
  134. """Assert that `inp` mapped through `aliases` produces `out`.
  135. `out` is canonicalized first, since aliases always produce
  136. canonicalized paths.
  137. """
  138. self.assertEqual(aliases.map(inp), files.canonical_filename(out))
  139. def assert_not_mapped(self, aliases, inp):
  140. """Assert that `inp` mapped through `aliases` is unchanged."""
  141. self.assertEqual(aliases.map(inp), inp)
  142. def test_noop(self):
  143. aliases = PathAliases()
  144. self.assert_not_mapped(aliases, '/ned/home/a.py')
  145. def test_nomatch(self):
  146. aliases = PathAliases()
  147. aliases.add('/home/*/src', './mysrc')
  148. self.assert_not_mapped(aliases, '/home/foo/a.py')
  149. def test_wildcard(self):
  150. aliases = PathAliases()
  151. aliases.add('/ned/home/*/src', './mysrc')
  152. self.assert_mapped(aliases, '/ned/home/foo/src/a.py', './mysrc/a.py')
  153. aliases = PathAliases()
  154. aliases.add('/ned/home/*/src/', './mysrc')
  155. self.assert_mapped(aliases, '/ned/home/foo/src/a.py', './mysrc/a.py')
  156. def test_no_accidental_match(self):
  157. aliases = PathAliases()
  158. aliases.add('/home/*/src', './mysrc')
  159. self.assert_not_mapped(aliases, '/home/foo/srcetc')
  160. def test_multiple_patterns(self):
  161. aliases = PathAliases()
  162. aliases.add('/home/*/src', './mysrc')
  163. aliases.add('/lib/*/libsrc', './mylib')
  164. self.assert_mapped(aliases, '/home/foo/src/a.py', './mysrc/a.py')
  165. self.assert_mapped(aliases, '/lib/foo/libsrc/a.py', './mylib/a.py')
  166. def test_cant_have_wildcard_at_end(self):
  167. aliases = PathAliases()
  168. msg = "Pattern must not end with wildcards."
  169. with self.assertRaisesRegex(CoverageException, msg):
  170. aliases.add("/ned/home/*", "fooey")
  171. with self.assertRaisesRegex(CoverageException, msg):
  172. aliases.add("/ned/home/*/", "fooey")
  173. with self.assertRaisesRegex(CoverageException, msg):
  174. aliases.add("/ned/home/*/*/", "fooey")
  175. def test_no_accidental_munging(self):
  176. aliases = PathAliases()
  177. aliases.add(r'c:\Zoo\boo', 'src/')
  178. aliases.add('/home/ned$', 'src/')
  179. self.assert_mapped(aliases, r'c:\Zoo\boo\foo.py', 'src/foo.py')
  180. self.assert_mapped(aliases, r'/home/ned$/foo.py', 'src/foo.py')
  181. def test_paths_are_os_corrected(self):
  182. aliases = PathAliases()
  183. aliases.add('/home/ned/*/src', './mysrc')
  184. aliases.add(r'c:\ned\src', './mysrc')
  185. self.assert_mapped(aliases, r'C:\Ned\src\sub\a.py', './mysrc/sub/a.py')
  186. aliases = PathAliases()
  187. aliases.add('/home/ned/*/src', r'.\mysrc')
  188. aliases.add(r'c:\ned\src', r'.\mysrc')
  189. self.assert_mapped(aliases, r'/home/ned/foo/src/sub/a.py', r'.\mysrc\sub\a.py')
  190. def test_leading_wildcard(self):
  191. aliases = PathAliases()
  192. aliases.add('*/d1', './mysrc1')
  193. aliases.add('*/d2', './mysrc2')
  194. self.assert_mapped(aliases, '/foo/bar/d1/x.py', './mysrc1/x.py')
  195. self.assert_mapped(aliases, '/foo/bar/d2/y.py', './mysrc2/y.py')
  196. def test_dot(self):
  197. for d in ('.', '..', '../other', '~'):
  198. aliases = PathAliases()
  199. aliases.add(d, '/the/source')
  200. the_file = os.path.join(d, 'a.py')
  201. the_file = os.path.expanduser(the_file)
  202. the_file = os.path.abspath(os.path.realpath(the_file))
  203. assert '~' not in the_file # to be sure the test is pure.
  204. self.assert_mapped(aliases, the_file, '/the/source/a.py')
  205. class FindPythonFilesTest(CoverageTest):
  206. """Tests of `find_python_files`."""
  207. def test_find_python_files(self):
  208. self.make_file("sub/a.py")
  209. self.make_file("sub/b.py")
  210. self.make_file("sub/x.c") # nope: not .py
  211. self.make_file("sub/ssub/__init__.py")
  212. self.make_file("sub/ssub/s.py")
  213. self.make_file("sub/ssub/~s.py") # nope: editor effluvia
  214. self.make_file("sub/lab/exp.py") # nope: no __init__.py
  215. self.make_file("sub/windows.pyw")
  216. py_files = set(find_python_files("sub"))
  217. self.assert_same_files(py_files, [
  218. "sub/a.py", "sub/b.py",
  219. "sub/ssub/__init__.py", "sub/ssub/s.py",
  220. "sub/windows.pyw",
  221. ])
  222. class WindowsFileTest(CoverageTest):
  223. """Windows-specific tests of file name handling."""
  224. run_in_temp_dir = False
  225. def setUp(self):
  226. if not env.WINDOWS:
  227. self.skipTest("Only need to run Windows tests on Windows.")
  228. super(WindowsFileTest, self).setUp()
  229. def test_actual_path(self):
  230. self.assertEquals(actual_path(r'c:\Windows'), actual_path(r'C:\wINDOWS'))