OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for owners.py.""" | 6 """Unit tests for owners.py.""" |
7 | 7 |
8 import unittest | 8 import unittest |
9 | 9 |
10 import owners | 10 import owners |
11 from tests import filesystem_mock | 11 from tests import filesystem_mock |
12 | 12 |
| 13 ben = 'ben@example.com' |
| 14 brett = 'brett@example.com' |
| 15 darin = 'darin@example.com' |
| 16 john = 'john@example.com' |
| 17 ken = 'ken@example.com' |
| 18 peter = 'peter@example.com' |
| 19 |
| 20 def owners_file(*email_addresses, **kwargs): |
| 21 s = '' |
| 22 if kwargs.get('noparent'): |
| 23 s = 'set noparent\n' |
| 24 return s + '\n'.join(email_addresses) + '\n' |
| 25 |
13 | 26 |
14 def test_repo(): | 27 def test_repo(): |
15 return filesystem_mock.MockFileSystem(files={ | 28 return filesystem_mock.MockFileSystem(files={ |
16 '/DEPS' : '', | 29 '/DEPS' : '', |
17 '/OWNERS': | 30 '/OWNERS': owners_file('*'), |
18 ('# OWNERS' | 31 '/base/vlog.h': '', |
19 '*\n'), | 32 '/chrome/OWNERS': owners_file(ben, brett), |
20 '/base/vlog.h': | 33 '/chrome/gpu/OWNERS': owners_file(ken), |
21 '// vlog.h\n', | 34 '/chrome/gpu/gpu_channel.h': '', |
22 '/chrome/OWNERS': | 35 '/chrome/renderer/OWNERS': owners_file(peter), |
23 ('ben@example.com\n' | 36 '/chrome/renderer/gpu/gpu_channel_host.h': '', |
24 'brettw@example.com\n'), | 37 '/chrome/renderer/safe_browsing/scorer.h': '', |
25 '/chrome/gpu/OWNERS': | 38 '/content/OWNERS': owners_file(john, darin, noparent=True), |
26 ('kbr@example.com\n'), | 39 '/content/content.gyp': '', |
27 '/chrome/gpu/gpu_channel.h': | |
28 '// gpu_channel.h\n', | |
29 '/chrome/renderer/OWNERS': | |
30 ('pkasting@example.com\n'), | |
31 '/chrome/renderer/gpu/gpu_channel_host.h': | |
32 '// gpu_channel_host.h\n', | |
33 '/chrome/renderer/safe_browsing/scorer.h': | |
34 '// scorer.h\n', | |
35 '/content/OWNERS': | |
36 ('set noparent\n' | |
37 'jam@example.com\n' | |
38 'darin@example.com\n'), | |
39 '/content/content.gyp': | |
40 '# content.gyp\n', | |
41 }) | 40 }) |
42 | 41 |
43 | 42 |
44 ben = 'ben@example.com' | |
45 brett = 'brettw@example.com' | |
46 darin = 'darin@example.com' | |
47 jam = 'jam@example.com' | |
48 kbr = 'kbr@example.com' | |
49 pkasting = 'pkasting@example.com' | |
50 | |
51 | |
52 class OwnersDatabaseTest(unittest.TestCase): | 43 class OwnersDatabaseTest(unittest.TestCase): |
53 def setUp(self): | 44 def setUp(self): |
54 self.repo = test_repo() | 45 self.repo = test_repo() |
55 self.files = self.repo.files | 46 self.files = self.repo.files |
56 self.root = '/' | 47 self.root = '/' |
57 | |
58 # pylint: disable=W0108 | |
59 self.fopen = self.repo.open_for_reading | 48 self.fopen = self.repo.open_for_reading |
60 | 49 |
61 def db(self, root=None, fopen=None, os_path=None): | 50 def db(self, root=None, fopen=None, os_path=None): |
62 root = root or self.root | 51 root = root or self.root |
63 fopen = fopen or self.fopen | 52 fopen = fopen or self.fopen |
64 os_path = os_path or self.repo | 53 os_path = os_path or self.repo |
65 return owners.Database(root, fopen, os_path) | 54 return owners.Database(root, fopen, os_path) |
66 | 55 |
67 def assertReviewersFor(self, files, expected_reviewers): | 56 def assertReviewersFor(self, files, expected_reviewers): |
68 db = self.db() | 57 db = self.db() |
69 self.assertEquals(db.ReviewersFor(set(files)), set(expected_reviewers)) | 58 self.assertEquals(db.ReviewersFor(set(files)), set(expected_reviewers)) |
70 | 59 |
71 def assertCoveredBy(self, files, reviewers): | 60 def assertCoveredBy(self, files, reviewers): |
72 db = self.db() | 61 db = self.db() |
73 self.assertTrue(db.FilesAreCoveredBy(set(files), set(reviewers))) | 62 self.assertTrue(db.FilesAreCoveredBy(set(files), set(reviewers))) |
74 | 63 |
75 def assertNotCoveredBy(self, files, reviewers, unreviewed_files): | 64 def assertNotCoveredBy(self, files, reviewers, unreviewed_files): |
76 db = self.db() | 65 db = self.db() |
77 self.assertEquals(db.FilesNotCoveredBy(set(files), set(reviewers)), | 66 self.assertEquals(db.FilesNotCoveredBy(set(files), set(reviewers)), |
78 set(unreviewed_files)) | 67 set(unreviewed_files)) |
79 | 68 |
80 def test_constructor(self): | 69 def test_constructor(self): |
81 self.assertNotEquals(self.db(), None) | 70 self.assertNotEquals(self.db(), None) |
82 | 71 |
83 def test_owners_for(self): | 72 def test_owners_for(self): |
84 self.assertReviewersFor(['DEPS'], [owners.ANYONE]) | 73 self.assertReviewersFor(['DEPS'], [owners.ANYONE]) |
85 self.assertReviewersFor(['content/content.gyp'], [jam, darin]) | 74 self.assertReviewersFor(['content/content.gyp'], [john, darin]) |
86 self.assertReviewersFor(['chrome/gpu/gpu_channel.h'], [kbr]) | 75 self.assertReviewersFor(['chrome/gpu/gpu_channel.h'], [ken]) |
87 | 76 |
88 def test_covered_by(self): | 77 def test_covered_by(self): |
89 self.assertCoveredBy(['DEPS'], [jam]) | 78 self.assertCoveredBy(['DEPS'], [john]) |
90 self.assertCoveredBy(['DEPS'], [darin]) | 79 self.assertCoveredBy(['DEPS'], [darin]) |
91 self.assertCoveredBy(['content/content.gyp'], [jam]) | 80 self.assertCoveredBy(['content/content.gyp'], [john]) |
92 self.assertCoveredBy(['chrome/gpu/OWNERS'], [kbr]) | 81 self.assertCoveredBy(['chrome/gpu/OWNERS'], [ken]) |
93 self.assertCoveredBy(['chrome/gpu/OWNERS'], [ben]) | 82 self.assertCoveredBy(['chrome/gpu/OWNERS'], [ben]) |
94 | 83 |
95 def test_not_covered_by(self): | 84 def test_not_covered_by(self): |
96 self.assertNotCoveredBy(['DEPS'], [], ['DEPS']) | 85 self.assertNotCoveredBy(['DEPS'], [], ['DEPS']) |
97 self.assertNotCoveredBy(['content/content.gyp'], [ben], | 86 self.assertNotCoveredBy(['content/content.gyp'], [ben], |
98 ['content/content.gyp']) | 87 ['content/content.gyp']) |
99 self.assertNotCoveredBy( | 88 self.assertNotCoveredBy( |
100 ['chrome/gpu/gpu_channel.h', 'chrome/renderer/gpu/gpu_channel_host.h'], | 89 ['chrome/gpu/gpu_channel.h', 'chrome/renderer/gpu/gpu_channel_host.h'], |
101 [pkasting], ['chrome/gpu/gpu_channel.h']) | 90 [peter], ['chrome/gpu/gpu_channel.h']) |
102 self.assertNotCoveredBy( | 91 self.assertNotCoveredBy( |
103 ['chrome/gpu/gpu_channel.h', 'chrome/renderer/gpu/gpu_channel_host.h'], | 92 ['chrome/gpu/gpu_channel.h', 'chrome/renderer/gpu/gpu_channel_host.h'], |
104 [ben], []) | 93 [ben], []) |
105 | 94 |
106 def test_comments_in_owners_file(self): | 95 def test_comments_in_owners_file(self): |
107 # pylint: disable=W0212 | 96 # pylint: disable=W0212 |
108 db = self.db() | 97 db = self.db() |
109 # Tests that this doesn't raise an error. | 98 # Tests that this doesn't raise an error. |
110 db._ReadOwnersFile('OWNERS', 'DEPS') | 99 db._ReadOwnersFile('OWNERS', 'DEPS') |
111 | 100 |
112 def test_syntax_error_in_owners_file(self): | 101 def test_syntax_error_in_owners_file(self): |
113 # pylint: disable=W0212 | 102 # pylint: disable=W0212 |
114 db = self.db() | 103 db = self.db() |
115 self.files['/foo/OWNERS'] = '{}\n' | 104 self.files['/foo/OWNERS'] = '{}\n' |
116 self.files['/foo/DEPS'] = '# DEPS\n' | 105 self.files['/foo/DEPS'] = '# DEPS\n' |
117 self.assertRaises(owners.SyntaxErrorInOwnersFile, db._ReadOwnersFile, | 106 self.assertRaises(owners.SyntaxErrorInOwnersFile, db._ReadOwnersFile, |
118 '/foo/OWNERS', '/foo/DEPS') | 107 '/foo/OWNERS', '/foo/DEPS') |
119 | 108 |
120 self.files['/bar/OWNERS'] = 'set myparentislinus\n' | 109 self.files['/bar/OWNERS'] = 'set myparentislinus\n' |
121 self.files['/bar/DEPS'] = '# DEPS\n' | 110 self.files['/bar/DEPS'] = '# DEPS\n' |
122 self.assertRaises(owners.SyntaxErrorInOwnersFile, db._ReadOwnersFile, | 111 self.assertRaises(owners.SyntaxErrorInOwnersFile, db._ReadOwnersFile, |
123 '/bar/OWNERS', '/bar/DEPS') | 112 '/bar/OWNERS', '/bar/DEPS') |
124 | 113 |
125 def test_owners_propagates_down(self): | 114 def test_owners_propagates_down(self): |
126 self.assertCoveredBy(['/chrome/renderer/gpu/gpu_channel_host.h'], | 115 self.assertCoveredBy(['/chrome/renderer/gpu/gpu_channel_host.h'], [peter]) |
127 [pkasting]) | |
128 | 116 |
129 def test_set_noparent(self): | 117 def test_set_noparent(self): |
130 self.assertNotCoveredBy(['/content/content.gyp'], [pkasting], | 118 self.assertNotCoveredBy(['/content/content.gyp'], [peter], |
131 ['/content/content.gyp']) | 119 ['/content/content.gyp']) |
132 | 120 |
133 | 121 |
134 if __name__ == '__main__': | 122 if __name__ == '__main__': |
135 unittest.main() | 123 unittest.main() |
OLD | NEW |