| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import unittest | 10 import unittest |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 def test_per_file_glob_across_dirs_not_allowed(self): | 207 def test_per_file_glob_across_dirs_not_allowed(self): |
| 208 self.files['/OWNERS'] = 'per-file content/*=john@example.org\n' | 208 self.files['/OWNERS'] = 'per-file content/*=john@example.org\n' |
| 209 self.assertRaises(owners.SyntaxErrorInOwnersFile, | 209 self.assertRaises(owners.SyntaxErrorInOwnersFile, |
| 210 self.db().files_not_covered_by, ['DEPS'], [brett]) | 210 self.db().files_not_covered_by, ['DEPS'], [brett]) |
| 211 | 211 |
| 212 def assert_syntax_error(self, owners_file_contents): | 212 def assert_syntax_error(self, owners_file_contents): |
| 213 db = self.db() | 213 db = self.db() |
| 214 self.files['/foo/OWNERS'] = owners_file_contents | 214 self.files['/foo/OWNERS'] = owners_file_contents |
| 215 self.files['/foo/DEPS'] = '' | 215 self.files['/foo/DEPS'] = '' |
| 216 try: | 216 try: |
| 217 db.reviewers_for(['foo/DEPS']) | 217 db.reviewers_for(['foo/DEPS'], None) |
| 218 self.fail() # pragma: no cover | 218 self.fail() # pragma: no cover |
| 219 except owners.SyntaxErrorInOwnersFile, e: | 219 except owners.SyntaxErrorInOwnersFile, e: |
| 220 self.assertTrue(str(e).startswith('/foo/OWNERS:1')) | 220 self.assertTrue(str(e).startswith('/foo/OWNERS:1')) |
| 221 | 221 |
| 222 def test_syntax_error__unknown_token(self): | 222 def test_syntax_error__unknown_token(self): |
| 223 self.assert_syntax_error('{}\n') | 223 self.assert_syntax_error('{}\n') |
| 224 | 224 |
| 225 def test_syntax_error__unknown_set(self): | 225 def test_syntax_error__unknown_set(self): |
| 226 self.assert_syntax_error('set myfatherisbillgates\n') | 226 self.assert_syntax_error('set myfatherisbillgates\n') |
| 227 | 227 |
| 228 def test_syntax_error__bad_email(self): | 228 def test_syntax_error__bad_email(self): |
| 229 self.assert_syntax_error('ben\n') | 229 self.assert_syntax_error('ben\n') |
| 230 | 230 |
| 231 | 231 |
| 232 class ReviewersForTest(_BaseTestCase): | 232 class ReviewersForTest(_BaseTestCase): |
| 233 def assert_reviewers_for(self, files, *potential_suggested_reviewers): | 233 def assert_reviewers_for(self, files, potential_suggested_reviewers, |
| 234 author=None): |
| 234 db = self.db() | 235 db = self.db() |
| 235 suggested_reviewers = db.reviewers_for(set(files)) | 236 suggested_reviewers = db.reviewers_for(set(files), author) |
| 236 self.assertTrue(suggested_reviewers in | 237 self.assertTrue(suggested_reviewers in |
| 237 [set(suggestion) for suggestion in potential_suggested_reviewers]) | 238 [set(suggestion) for suggestion in potential_suggested_reviewers]) |
| 238 | 239 |
| 239 def test_reviewers_for__basic_functionality(self): | 240 def test_reviewers_for__basic_functionality(self): |
| 240 self.assert_reviewers_for(['chrome/gpu/gpu_channel.h'], | 241 self.assert_reviewers_for(['chrome/gpu/gpu_channel.h'], |
| 241 [ken]) | 242 [[ken]]) |
| 242 | 243 |
| 243 def test_reviewers_for__set_noparent_works(self): | 244 def test_reviewers_for__set_noparent_works(self): |
| 244 self.assert_reviewers_for(['content/content.gyp'], | 245 self.assert_reviewers_for(['content/content.gyp'], |
| 245 [john], | 246 [[john], |
| 246 [darin]) | 247 [darin]]) |
| 247 | 248 |
| 248 def test_reviewers_for__valid_inputs(self): | 249 def test_reviewers_for__valid_inputs(self): |
| 249 db = self.db() | 250 db = self.db() |
| 250 | 251 |
| 251 # Check that we're passed in a sequence that isn't a string. | 252 # Check that we're passed in a sequence that isn't a string. |
| 252 self.assertRaises(AssertionError, db.reviewers_for, 'foo') | 253 self.assertRaises(AssertionError, db.reviewers_for, 'foo', None) |
| 253 if hasattr(owners.collections, 'Iterable'): | 254 if hasattr(owners.collections, 'Iterable'): |
| 254 self.assertRaises(AssertionError, db.reviewers_for, | 255 self.assertRaises(AssertionError, db.reviewers_for, |
| 255 (f for f in ['x', 'y'])) | 256 (f for f in ['x', 'y']), None) |
| 256 | 257 |
| 257 # Check that the files are under the root. | 258 # Check that the files are under the root. |
| 258 db.root = '/checkout' | 259 db.root = '/checkout' |
| 259 self.assertRaises(AssertionError, db.reviewers_for, ['/OWNERS']) | 260 self.assertRaises(AssertionError, db.reviewers_for, ['/OWNERS'], None) |
| 260 | 261 |
| 261 def test_reviewers_for__wildcard_dir(self): | 262 def test_reviewers_for__wildcard_dir(self): |
| 262 self.assert_reviewers_for(['DEPS'], ['<anyone>']) | 263 self.assert_reviewers_for(['DEPS'], [['<anyone>']]) |
| 263 self.assert_reviewers_for(['DEPS', 'chrome/gpu/gpu_channel.h'], [ken]) | 264 self.assert_reviewers_for(['DEPS', 'chrome/gpu/gpu_channel.h'], [[ken]]) |
| 264 | 265 |
| 265 def test_reviewers_for__one_owner(self): | 266 def test_reviewers_for__one_owner(self): |
| 266 self.assert_reviewers_for([ | 267 self.assert_reviewers_for([ |
| 267 'chrome/gpu/gpu_channel.h', | 268 'chrome/gpu/gpu_channel.h', |
| 268 'content/baz/froboz.h', | 269 'content/baz/froboz.h', |
| 269 'chrome/renderer/gpu/gpu_channel_host.h'], | 270 'chrome/renderer/gpu/gpu_channel_host.h'], |
| 270 [brett]) | 271 [[brett]]) |
| 271 | 272 |
| 272 def test_reviewers_for__two_owners(self): | 273 def test_reviewers_for__two_owners(self): |
| 273 self.assert_reviewers_for([ | 274 self.assert_reviewers_for([ |
| 274 'chrome/gpu/gpu_channel.h', | 275 'chrome/gpu/gpu_channel.h', |
| 275 'content/content.gyp', | 276 'content/content.gyp', |
| 276 'content/baz/froboz.h', | 277 'content/baz/froboz.h', |
| 277 'content/views/pie.h'], | 278 'content/views/pie.h'], |
| 278 [ken, john]) | 279 [[ken, john]]) |
| 279 | 280 |
| 280 def test_reviewers_for__all_files(self): | 281 def test_reviewers_for__all_files(self): |
| 281 self.assert_reviewers_for([ | 282 self.assert_reviewers_for([ |
| 282 'chrome/gpu/gpu_channel.h', | 283 'chrome/gpu/gpu_channel.h', |
| 283 'chrome/renderer/gpu/gpu_channel_host.h', | 284 'chrome/renderer/gpu/gpu_channel_host.h', |
| 284 'chrome/renderer/safe_browsing/scorer.h', | 285 'chrome/renderer/safe_browsing/scorer.h', |
| 285 'content/content.gyp', | 286 'content/content.gyp', |
| 286 'content/bar/foo.cc', | 287 'content/bar/foo.cc', |
| 287 'content/baz/froboz.h', | 288 'content/baz/froboz.h', |
| 288 'content/views/pie.h'], | 289 'content/views/pie.h'], |
| 289 [peter, ken, john]) | 290 [[peter, ken, john]]) |
| 290 | 291 |
| 291 def test_reviewers_for__per_file_owners_file(self): | 292 def test_reviewers_for__per_file_owners_file(self): |
| 292 self.files['/content/baz/OWNERS'] = owners_file(lines=[ | 293 self.files['/content/baz/OWNERS'] = owners_file(lines=[ |
| 293 'per-file ugly.*=tom@example.com']) | 294 'per-file ugly.*=tom@example.com']) |
| 294 self.assert_reviewers_for(['content/baz/OWNERS'], | 295 self.assert_reviewers_for(['content/baz/OWNERS'], |
| 295 [john], | 296 [[john], |
| 296 [darin]) | 297 [darin]]) |
| 297 | 298 |
| 298 def test_reviewers_for__per_file(self): | 299 def test_reviewers_for__per_file(self): |
| 299 self.files['/content/baz/OWNERS'] = owners_file(lines=[ | 300 self.files['/content/baz/OWNERS'] = owners_file(lines=[ |
| 300 'per-file ugly.*=tom@example.com']) | 301 'per-file ugly.*=tom@example.com']) |
| 301 self.assert_reviewers_for(['content/baz/ugly.cc'], | 302 self.assert_reviewers_for(['content/baz/ugly.cc'], |
| 302 [tom]) | 303 [[tom]]) |
| 303 | 304 |
| 304 def test_reviewers_for__two_nested_dirs(self): | 305 def test_reviewers_for__two_nested_dirs(self): |
| 305 # The same owner is listed in two directories (one above the other) | 306 # The same owner is listed in two directories (one above the other) |
| 306 self.assert_reviewers_for(['chrome/browser/defaults.h'], | 307 self.assert_reviewers_for(['chrome/browser/defaults.h'], |
| 307 [brett]) | 308 [[brett]]) |
| 308 | 309 |
| 309 # Here, although either ben or brett could review both files, | 310 # Here, although either ben or brett could review both files, |
| 310 # someone closer to the gpu_channel_host.h should also be suggested. | 311 # someone closer to the gpu_channel_host.h should also be suggested. |
| 311 # This also tests that we can handle two suggested reviewers | 312 # This also tests that we can handle two suggested reviewers |
| 312 # with overlapping sets of directories properly. | 313 # with overlapping sets of directories properly. |
| 313 self.files['/chrome/renderer/gpu/OWNERS'] = owners_file(ken) | 314 self.files['/chrome/renderer/gpu/OWNERS'] = owners_file(ken) |
| 314 self.assert_reviewers_for(['chrome/OWNERS', | 315 self.assert_reviewers_for(['chrome/OWNERS', |
| 315 'chrome/renderer/gpu/gpu_channel_host.h'], | 316 'chrome/renderer/gpu/gpu_channel_host.h'], |
| 316 [ben, ken], | 317 [[ben, ken], |
| 317 [brett, ken]) | 318 [brett, ken]]) |
| 319 |
| 320 def test_reviewers_for__author_is_known(self): |
| 321 # We should never suggest ken as a reviewer for his own changes. |
| 322 self.assert_reviewers_for(['chrome/gpu/gpu_channel.h'], |
| 323 [[ben], [brett]], author=ken) |
| 318 | 324 |
| 319 | 325 |
| 320 class LowestCostOwnersTest(_BaseTestCase): | 326 class LowestCostOwnersTest(_BaseTestCase): |
| 321 # Keep the data in the test_lowest_cost_owner* methods as consistent with | 327 # Keep the data in the test_lowest_cost_owner* methods as consistent with |
| 322 # test_repo() where possible to minimize confusion. | 328 # test_repo() where possible to minimize confusion. |
| 323 | 329 |
| 324 def check(self, possible_owners, dirs, *possible_lowest_cost_owners): | 330 def check(self, possible_owners, dirs, *possible_lowest_cost_owners): |
| 325 suggested_owner = owners.Database.lowest_cost_owner(possible_owners, dirs) | 331 suggested_owner = owners.Database.lowest_cost_owner(possible_owners, dirs) |
| 326 self.assertTrue(suggested_owner in possible_lowest_cost_owners) | 332 self.assertTrue(suggested_owner in possible_lowest_cost_owners) |
| 327 | 333 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 ('chrome/browser', 2)], | 392 ('chrome/browser', 2)], |
| 387 ken: [('chrome/gpu', 1)], | 393 ken: [('chrome/gpu', 1)], |
| 388 peter: [('chrome/renderer', 1)], | 394 peter: [('chrome/renderer', 1)], |
| 389 brett: [('chrome/browser', 1)]}, | 395 brett: [('chrome/browser', 1)]}, |
| 390 ['chrome/gpu', 'chrome/renderer', | 396 ['chrome/gpu', 'chrome/renderer', |
| 391 'chrome/browser'], | 397 'chrome/browser'], |
| 392 ben) | 398 ben) |
| 393 | 399 |
| 394 if __name__ == '__main__': | 400 if __name__ == '__main__': |
| 395 unittest.main() | 401 unittest.main() |
| OLD | NEW |