[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for owners_finder.py.""" |
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import unittest |
| 11 | |
| 12 | |
| 13 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | |
| 15 | from testing_support import filesystem_mock |
| 16 | |
| 17 | import owners_finder |
| 18 | import owners |
| 19 | |
| 20 | |
| 21 | ben = '[email protected]' |
| 22 | brett = '[email protected]' |
| 23 | darin = '[email protected]' |
Jochen Eisinger | 72606f8 | 2017-04-04 08:44:18 | [diff] [blame] | 24 | jochen = '[email protected]' |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 25 | john = '[email protected]' |
| 26 | ken = '[email protected]' |
| 27 | peter = '[email protected]' |
| 28 | tom = '[email protected]' |
| 29 | |
| 30 | |
| 31 | def owners_file(*email_addresses, **kwargs): |
| 32 | s = '' |
| 33 | if kwargs.get('comment'): |
| 34 | s += '# %s\n' % kwargs.get('comment') |
| 35 | if kwargs.get('noparent'): |
| 36 | s += 'set noparent\n' |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 37 | return s + '\n'.join(email_addresses) + '\n' |
| 38 | |
| 39 | |
| 40 | def test_repo(): |
| 41 | return filesystem_mock.MockFileSystem(files={ |
| 42 | '/DEPS': '', |
Jochen Eisinger | eb74476 | 2017-04-05 09:00:05 | [diff] [blame] | 43 | '/OWNERS': owners_file(ken, peter, tom, |
| 44 | comment='OWNERS_STATUS = build/OWNERS.status'), |
Jochen Eisinger | 72606f8 | 2017-04-04 08:44:18 | [diff] [blame] | 45 | '/build/OWNERS.status': '%s: bar' % jochen, |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 46 | '/base/vlog.h': '', |
| 47 | '/chrome/OWNERS': owners_file(ben, brett), |
| 48 | '/chrome/browser/OWNERS': owners_file(brett), |
| 49 | '/chrome/browser/defaults.h': '', |
| 50 | '/chrome/gpu/OWNERS': owners_file(ken), |
| 51 | '/chrome/gpu/gpu_channel.h': '', |
| 52 | '/chrome/renderer/OWNERS': owners_file(peter), |
| 53 | '/chrome/renderer/gpu/gpu_channel_host.h': '', |
| 54 | '/chrome/renderer/safe_browsing/scorer.h': '', |
| 55 | '/content/OWNERS': owners_file(john, darin, comment='foo', noparent=True), |
| 56 | '/content/content.gyp': '', |
| 57 | '/content/bar/foo.cc': '', |
| 58 | '/content/baz/OWNERS': owners_file(brett), |
| 59 | '/content/baz/froboz.h': '', |
| 60 | '/content/baz/ugly.cc': '', |
| 61 | '/content/baz/ugly.h': '', |
Jochen Eisinger | 72606f8 | 2017-04-04 08:44:18 | [diff] [blame] | 62 | '/content/common/OWNERS': owners_file(jochen), |
| 63 | '/content/common/common.cc': '', |
| 64 | '/content/foo/OWNERS': owners_file(jochen, comment='foo'), |
| 65 | '/content/foo/foo.cc': '', |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 66 | '/content/views/OWNERS': owners_file(ben, john, owners.EVERYONE, |
| 67 | noparent=True), |
| 68 | '/content/views/pie.h': '', |
| 69 | }) |
| 70 | |
| 71 | |
| 72 | class OutputInterceptedOwnersFinder(owners_finder.OwnersFinder): |
| 73 | def __init__(self, files, local_root, |
dtu | 944b605 | 2016-07-14 21:48:21 | [diff] [blame] | 74 | fopen, os_path, disable_color=False): |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 75 | super(OutputInterceptedOwnersFinder, self).__init__( |
Jochen Eisinger | eb74476 | 2017-04-05 09:00:05 | [diff] [blame] | 76 | files, local_root, None, fopen, os_path, disable_color=disable_color) |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 77 | self.output = [] |
| 78 | self.indentation_stack = [] |
| 79 | |
| 80 | def resetText(self): |
| 81 | self.output = [] |
| 82 | self.indentation_stack = [] |
| 83 | |
| 84 | def indent(self): |
| 85 | self.indentation_stack.append(self.output) |
| 86 | self.output = [] |
| 87 | |
| 88 | def unindent(self): |
| 89 | block = self.output |
| 90 | self.output = self.indentation_stack.pop() |
| 91 | self.output.append(block) |
| 92 | |
| 93 | def writeln(self, text=''): |
| 94 | self.output.append(text) |
| 95 | |
| 96 | |
| 97 | class _BaseTestCase(unittest.TestCase): |
| 98 | default_files = [ |
| 99 | 'base/vlog.h', |
| 100 | 'chrome/browser/defaults.h', |
| 101 | 'chrome/gpu/gpu_channel.h', |
| 102 | 'chrome/renderer/gpu/gpu_channel_host.h', |
| 103 | 'chrome/renderer/safe_browsing/scorer.h', |
| 104 | 'content/content.gyp', |
| 105 | 'content/bar/foo.cc', |
| 106 | 'content/baz/ugly.cc', |
| 107 | 'content/baz/ugly.h', |
| 108 | 'content/views/pie.h' |
| 109 | ] |
| 110 | |
| 111 | def setUp(self): |
| 112 | self.repo = test_repo() |
| 113 | self.root = '/' |
| 114 | self.fopen = self.repo.open_for_reading |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 115 | |
| 116 | def ownersFinder(self, files): |
| 117 | finder = OutputInterceptedOwnersFinder(files, self.root, |
| 118 | fopen=self.fopen, |
| 119 | os_path=self.repo, |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 120 | disable_color=True) |
| 121 | return finder |
| 122 | |
| 123 | def defaultFinder(self): |
| 124 | return self.ownersFinder(self.default_files) |
| 125 | |
| 126 | |
| 127 | class OwnersFinderTests(_BaseTestCase): |
| 128 | def test_constructor(self): |
| 129 | self.assertNotEquals(self.defaultFinder(), None) |
| 130 | |
| 131 | def test_reset(self): |
| 132 | finder = self.defaultFinder() |
| 133 | i = 0 |
| 134 | while i < 2: |
| 135 | i += 1 |
| 136 | self.assertEqual(finder.owners_queue, |
| 137 | [brett, john, darin, peter, ken, ben, tom]) |
| 138 | self.assertEqual(finder.unreviewed_files, { |
| 139 | 'base/vlog.h', |
| 140 | 'chrome/browser/defaults.h', |
| 141 | 'chrome/gpu/gpu_channel.h', |
| 142 | 'chrome/renderer/gpu/gpu_channel_host.h', |
| 143 | 'chrome/renderer/safe_browsing/scorer.h', |
| 144 | 'content/content.gyp', |
| 145 | 'content/bar/foo.cc', |
| 146 | 'content/baz/ugly.cc', |
| 147 | 'content/baz/ugly.h' |
| 148 | }) |
| 149 | self.assertEqual(finder.selected_owners, set()) |
| 150 | self.assertEqual(finder.deselected_owners, set()) |
| 151 | self.assertEqual(finder.reviewed_by, {}) |
| 152 | self.assertEqual(finder.output, []) |
| 153 | |
| 154 | finder.select_owner(john) |
| 155 | finder.reset() |
| 156 | finder.resetText() |
| 157 | |
| 158 | def test_select(self): |
| 159 | finder = self.defaultFinder() |
| 160 | finder.select_owner(john) |
| 161 | self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom]) |
| 162 | self.assertEqual(finder.selected_owners, {john}) |
| 163 | self.assertEqual(finder.deselected_owners, {darin}) |
| 164 | self.assertEqual(finder.reviewed_by, {'content/bar/foo.cc': john, |
| 165 | 'content/baz/ugly.cc': john, |
| 166 | 'content/baz/ugly.h': john, |
| 167 | 'content/content.gyp': john}) |
| 168 | self.assertEqual(finder.output, |
| 169 | ['Selected: ' + john, 'Deselected: ' + darin]) |
| 170 | |
| 171 | finder = self.defaultFinder() |
| 172 | finder.select_owner(darin) |
| 173 | self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom]) |
| 174 | self.assertEqual(finder.selected_owners, {darin}) |
| 175 | self.assertEqual(finder.deselected_owners, {john}) |
| 176 | self.assertEqual(finder.reviewed_by, {'content/bar/foo.cc': darin, |
| 177 | 'content/baz/ugly.cc': darin, |
| 178 | 'content/baz/ugly.h': darin, |
| 179 | 'content/content.gyp': darin}) |
| 180 | self.assertEqual(finder.output, |
| 181 | ['Selected: ' + darin, 'Deselected: ' + john]) |
| 182 | |
| 183 | finder = self.defaultFinder() |
| 184 | finder.select_owner(brett) |
| 185 | self.assertEqual(finder.owners_queue, [john, darin, peter, ken, tom]) |
| 186 | self.assertEqual(finder.selected_owners, {brett}) |
| 187 | self.assertEqual(finder.deselected_owners, {ben}) |
| 188 | self.assertEqual(finder.reviewed_by, |
| 189 | {'chrome/browser/defaults.h': brett, |
| 190 | 'chrome/gpu/gpu_channel.h': brett, |
| 191 | 'chrome/renderer/gpu/gpu_channel_host.h': brett, |
| 192 | 'chrome/renderer/safe_browsing/scorer.h': brett, |
| 193 | 'content/baz/ugly.cc': brett, |
| 194 | 'content/baz/ugly.h': brett}) |
| 195 | self.assertEqual(finder.output, |
| 196 | ['Selected: ' + brett, 'Deselected: ' + ben]) |
| 197 | |
| 198 | def test_deselect(self): |
| 199 | finder = self.defaultFinder() |
| 200 | finder.deselect_owner(john) |
| 201 | self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom]) |
| 202 | self.assertEqual(finder.selected_owners, {darin}) |
| 203 | self.assertEqual(finder.deselected_owners, {john}) |
| 204 | self.assertEqual(finder.reviewed_by, {'content/bar/foo.cc': darin, |
| 205 | 'content/baz/ugly.cc': darin, |
| 206 | 'content/baz/ugly.h': darin, |
| 207 | 'content/content.gyp': darin}) |
| 208 | self.assertEqual(finder.output, |
| 209 | ['Deselected: ' + john, 'Selected: ' + darin]) |
| 210 | |
| 211 | def test_print_file_info(self): |
| 212 | finder = self.defaultFinder() |
| 213 | finder.print_file_info('chrome/browser/defaults.h') |
| 214 | self.assertEqual(finder.output, ['chrome/browser/defaults.h [5]']) |
| 215 | finder.resetText() |
| 216 | |
| 217 | finder.print_file_info('chrome/renderer/gpu/gpu_channel_host.h') |
| 218 | self.assertEqual(finder.output, |
| 219 | ['chrome/renderer/gpu/gpu_channel_host.h [5]']) |
| 220 | |
| 221 | def test_print_file_info_detailed(self): |
| 222 | finder = self.defaultFinder() |
| 223 | finder.print_file_info_detailed('chrome/browser/defaults.h') |
| 224 | self.assertEqual(finder.output, |
| 225 | ['chrome/browser/defaults.h', |
| 226 | [ben, brett, ken, peter, tom]]) |
| 227 | finder.resetText() |
| 228 | |
| 229 | finder.print_file_info_detailed('chrome/renderer/gpu/gpu_channel_host.h') |
| 230 | self.assertEqual(finder.output, |
| 231 | ['chrome/renderer/gpu/gpu_channel_host.h', |
| 232 | [ben, brett, ken, peter, tom]]) |
| 233 | |
| 234 | def test_print_comments(self): |
| 235 | finder = self.defaultFinder() |
| 236 | finder.print_comments(darin) |
| 237 | self.assertEqual(finder.output, |
| 238 | [darin + ' is commented as:', ['foo (at content)']]) |
| 239 | |
Jochen Eisinger | 72606f8 | 2017-04-04 08:44:18 | [diff] [blame] | 240 | def test_print_global_comments(self): |
| 241 | finder = self.ownersFinder(['content/common/common.cc']) |
| 242 | finder.print_comments(jochen) |
| 243 | self.assertEqual(finder.output, |
| 244 | [jochen + ' is commented as:', ['bar (global status)']]) |
| 245 | |
| 246 | finder = self.ownersFinder(['content/foo/foo.cc']) |
| 247 | finder.print_comments(jochen) |
| 248 | self.assertEqual(finder.output, |
| 249 | [jochen + ' is commented as:', ['bar (global status)', |
| 250 | 'foo (at content/foo)']]) |
[email protected] | faf3fdf | 2013-09-20 02:11:48 | [diff] [blame] | 251 | |
| 252 | if __name__ == '__main__': |
| 253 | unittest.main() |