| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The LUCI Authors. All rights reserved. | 2 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import copy | 6 import copy |
| 7 import doctest | 7 import doctest |
| 8 import os | 8 import os |
| 9 import subprocess | 9 import subprocess |
| 10 import unittest | 10 import unittest |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 'repo/root', self.proto_file, allow_fetch=False) | 407 'repo/root', self.proto_file, allow_fetch=False) |
| 408 | 408 |
| 409 def test_dump_load_inverses(self): | 409 def test_dump_load_inverses(self): |
| 410 # Doubles as a test for equality reflexivity. | 410 # Doubles as a test for equality reflexivity. |
| 411 package_spec = package.PackageSpec.load_proto(self.proto_file) | 411 package_spec = package.PackageSpec.load_proto(self.proto_file) |
| 412 self.assertEqual(self.proto_file.to_raw(package_spec.dump()), | 412 self.assertEqual(self.proto_file.to_raw(package_spec.dump()), |
| 413 self.proto_text) | 413 self.proto_text) |
| 414 self.assertEqual(package.PackageSpec.load_proto(self.proto_file), | 414 self.assertEqual(package.PackageSpec.load_proto(self.proto_file), |
| 415 package_spec) | 415 package_spec) |
| 416 | 416 |
| 417 def test_dump_round_trips_text(self): | 417 def test_dump_round_trips(self): |
| 418 proto_text = """ | |
| 419 api_version: 1 | |
| 420 """.lstrip() | |
| 421 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) | |
| 422 package_spec = package.PackageSpec.load_proto(proto_file) | |
| 423 self.assertEqual(proto_file.to_raw(package_spec.dump()), proto_text) | |
| 424 | |
| 425 def test_dump_round_trips_json(self): | |
| 426 proto_text = """ | 418 proto_text = """ |
| 427 {"api_version": 1} | 419 {"api_version": 1} |
| 428 """.lstrip() | 420 """.lstrip() |
| 429 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) | 421 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) |
| 430 package_spec = package.PackageSpec.load_proto(proto_file) | 422 package_spec = package.PackageSpec.load_proto(proto_file) |
| 431 self.assertEqual(proto_file.to_raw(package_spec.dump()), | 423 self.assertEqual(proto_file.to_raw(package_spec.dump()), |
| 432 '{\n "api_version": 1\n}') | 424 '{\n "api_version": 1\n}') |
| 433 | 425 |
| 434 def test_no_version(self): | 426 def test_no_version(self): |
| 435 proto_text = """\ | 427 proto_text = """{ |
| 436 { | |
| 437 "project_id": "foo", | 428 "project_id": "foo", |
| 438 "recipes_path": "path/to/recipes" | 429 "recipes_path": "path/to/recipes" |
| 439 } | 430 } |
| 440 """ | 431 """ |
| 441 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) | 432 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) |
| 442 | 433 |
| 443 with self.assertRaises(AssertionError): | 434 with self.assertRaises(AssertionError): |
| 444 package.PackageSpec.load_proto(proto_file) | 435 package.PackageSpec.load_proto(proto_file) |
| 445 | 436 |
| 446 def test_unsupported_version(self): | 437 def test_unsupported_version(self): |
| 447 proto_text = """\ | 438 proto_text = """{ |
| 448 api_version: 99999999 | 439 "api_version": 99999999, |
| 449 project_id: "fizzbar" | 440 "project_id": "fizzbar", |
| 450 recipes_path: "path/to/recipes" | 441 "recipes_path": "path/to/recipes" |
| 451 """ | 442 }""" |
| 452 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) | 443 proto_file = MockProtoFile('repo/root/infra/config/recipes.cfg', proto_text) |
| 453 | 444 |
| 454 with self.assertRaises(AssertionError): | 445 with self.assertRaises(AssertionError): |
| 455 package.PackageSpec.load_proto(proto_file) | 446 package.PackageSpec.load_proto(proto_file) |
| 456 | 447 |
| 457 | 448 |
| 458 class TestPackageDeps(MockIOThings, unittest.TestCase): | 449 class TestPackageDeps(MockIOThings, unittest.TestCase): |
| 459 | 450 |
| 460 def test_create_with_overrides(self): | 451 def test_create_with_overrides(self): |
| 461 base_proto_text = """ | 452 base_proto_text = """{ |
| 462 api_version: 1 | 453 "api_version": 1, |
| 463 project_id: "base_package" | 454 "project_id": "base_package", |
| 464 recipes_path: "path/to/recipes" | 455 "recipes_path": "path/to/recipes", |
| 465 deps { | 456 "deps": [ |
| 466 project_id: "foo" | 457 { |
| 467 url: "https://repo.com/foo.git" | 458 "project_id": "foo", |
| 468 branch: "foobranch" | 459 "url": "https://repo.com/foo.git", |
| 469 revision: "deadd00d" | 460 "branch": "foobranch", |
| 461 "revision": "deadd00d" |
| 462 } |
| 463 ] |
| 470 } | 464 } |
| 471 """ | 465 """ |
| 472 base_proto_file = MockProtoFile('base/infra/config/recipes.cfg', | 466 base_proto_file = MockProtoFile('base/infra/config/recipes.cfg', |
| 473 base_proto_text) | 467 base_proto_text) |
| 474 | 468 |
| 475 foo_proto_text = """ | 469 foo_proto_text = """{ |
| 476 api_version: 1 | 470 "api_version": 1, |
| 477 project_id: "foo" | 471 "project_id": "foo", |
| 478 recipes_path: "path/to/recipes" | 472 "recipes_path": "path/to/recipes" |
| 479 """ | 473 }""" |
| 480 foo_proto_file = MockProtoFile('foo/infra/config/recipes.cfg', | 474 foo_proto_file = MockProtoFile('foo/infra/config/recipes.cfg', |
| 481 foo_proto_text) | 475 foo_proto_text) |
| 482 | 476 |
| 483 with mock.patch.object(package.GitRepoSpec, 'checkout') as checkout: | 477 with mock.patch.object(package.GitRepoSpec, 'checkout') as checkout: |
| 484 with mock.patch.object(package.PathRepoSpec, 'proto_file', | 478 with mock.patch.object(package.PathRepoSpec, 'proto_file', |
| 485 return_value=foo_proto_file): | 479 return_value=foo_proto_file): |
| 486 deps = package.PackageDeps.create('base', base_proto_file, overrides={ | 480 deps = package.PackageDeps.create('base', base_proto_file, overrides={ |
| 487 'foo': '/path/to/local/foo', | 481 'foo': '/path/to/local/foo', |
| 488 }) | 482 }) |
| 489 | 483 |
| 490 foo_deps = deps.get_package('foo') | 484 foo_deps = deps.get_package('foo') |
| 491 self.assertIsInstance(foo_deps.repo_spec, package.PathRepoSpec) | 485 self.assertIsInstance(foo_deps.repo_spec, package.PathRepoSpec) |
| 492 self.assertEqual(foo_deps.repo_spec.path, '/path/to/local/foo') | 486 self.assertEqual(foo_deps.repo_spec.path, '/path/to/local/foo') |
| 493 self.assertFalse(checkout.called) | 487 self.assertFalse(checkout.called) |
| 494 | 488 |
| 495 | 489 |
| 496 def load_tests(_loader, tests, _ignore): | 490 def load_tests(_loader, tests, _ignore): |
| 497 tests.addTests(doctest.DocTestSuite(package)) | 491 tests.addTests(doctest.DocTestSuite(package)) |
| 498 return tests | 492 return tests |
| 499 | 493 |
| 500 | 494 |
| 501 if __name__ == '__main__': | 495 if __name__ == '__main__': |
| 502 result = unittest.main() | 496 result = unittest.main() |
| OLD | NEW |