| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import json |
| 7 import logging |
| 8 import os |
| 9 import subprocess |
| 10 import sys |
| 11 import tempfile |
| 12 import traceback |
| 13 |
| 14 from cStringIO import StringIO |
| 15 |
| 16 import expect_tests |
| 17 |
| 18 from infra.libs.git2.testing_support import TestClock |
| 19 from infra.libs.git2.testing_support import TestRepo |
| 20 from infra.services.gsubtreed import gsubtreed |
| 21 from infra.services.gsubtreed.test import gsubtreed_test_definitions |
| 22 |
| 23 |
| 24 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 25 |
| 26 |
| 27 class TestConfigRef(gsubtreed.GsubtreedConfigRef): |
| 28 def update(self, **values): |
| 29 new_config = self.current |
| 30 new_config.update(values) |
| 31 self.ref.make_commit( |
| 32 'update(%r)' % values.keys(), |
| 33 {'config.json': json.dumps(new_config)}) |
| 34 |
| 35 |
| 36 def RunTest(test_name): |
| 37 ret = [] |
| 38 clock = TestClock() |
| 39 origin = TestRepo('origin', clock) |
| 40 local = TestRepo('local', clock, origin.repo_path) |
| 41 |
| 42 base_repo_path = tempfile.mkdtemp() |
| 43 |
| 44 enabled_paths = ['mirrored_path/subpath', 'mirrored_path'] |
| 45 cref = TestConfigRef(origin) |
| 46 cref.update(enabled_paths=enabled_paths, base_url='file://' + base_repo_path) |
| 47 |
| 48 for path in enabled_paths: |
| 49 full_path = os.path.join(base_repo_path, path) |
| 50 try: |
| 51 os.makedirs(full_path) |
| 52 except OSError: |
| 53 pass |
| 54 with open(os.devnull, 'w') as dn: |
| 55 subprocess.check_call(['git', 'init', '--bare'], cwd=full_path, |
| 56 stdout=dn, stderr=dn) |
| 57 |
| 58 def checkpoint(message, include_committer=False, include_config=False): |
| 59 osnap = origin.snap(include_committer, include_config) |
| 60 for ref in osnap: |
| 61 # since the subtree-processed refs are actually tags on master, don't |
| 62 # bother showing them in their entirety |
| 63 if 'subtree-processed' in ref: |
| 64 if len(osnap[ref]) <= 1: |
| 65 continue # pragma: no cover |
| 66 osnap[ref] = collections.OrderedDict([ |
| 67 next(osnap[ref].iteritems()), |
| 68 ('etc', '...') |
| 69 ]) |
| 70 ret.append([message, {'origin': osnap}]) |
| 71 |
| 72 def run(): |
| 73 stdout = sys.stdout |
| 74 stderr = sys.stderr |
| 75 |
| 76 logout = StringIO() |
| 77 root_logger = logging.getLogger() |
| 78 shandler = logging.StreamHandler(logout) |
| 79 shandler.setFormatter( |
| 80 logging.Formatter('%(levelname)s: %(message)s')) |
| 81 root_logger.addHandler(shandler) |
| 82 shandler.setLevel(logging.INFO) |
| 83 |
| 84 success = False |
| 85 processed = {} |
| 86 try: |
| 87 with open(os.devnull, 'w') as dn: |
| 88 # TODO(iannucci): Let expect_tests absorb stdio |
| 89 sys.stderr = sys.stdout = dn |
| 90 local.reify() |
| 91 success, processed = gsubtreed.inner_loop(local, cref) |
| 92 except Exception: # pragma: no cover |
| 93 ret.append(traceback.format_exc().splitlines()) |
| 94 finally: |
| 95 sys.stdout = stdout |
| 96 sys.stderr = stderr |
| 97 |
| 98 root_logger.removeHandler(shandler) |
| 99 ret.append({'log output': logout.getvalue().splitlines()}) |
| 100 |
| 101 ret.append({ |
| 102 'inner_loop success': success, |
| 103 'processed': processed, |
| 104 }) |
| 105 |
| 106 gsubtreed_test_definitions.GSUBTREED_TESTS[test_name](origin, run, checkpoint) |
| 107 |
| 108 return expect_tests.Result(ret) |
| 109 |
| 110 |
| 111 @expect_tests.test_generator |
| 112 def GenTests(): |
| 113 for test_name, test in gsubtreed_test_definitions.GSUBTREED_TESTS.iteritems(): |
| 114 yield expect_tests.Test( |
| 115 __package__ + '.' + test_name, |
| 116 expect_tests.FuncCall(RunTest, test_name), |
| 117 expect_base=test_name, ext='yaml', break_funcs=[test], |
| 118 covers=( |
| 119 expect_tests.Test.covers_obj(RunTest) + |
| 120 expect_tests.Test.covers_obj(gsubtreed_test_definitions) |
| 121 )) |
| OLD | NEW |