Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: tools/telemetry/third_party/coverage/tests/test_files.py

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

Powered by Google App Engine
This is Rietveld 408576698