|
| 1 | +# ===--- SchemeMock.py ----------------------------------------------------===# |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https:#swift.org/LICENSE.txt for license information |
| 9 | +# See https:#swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +# |
| 11 | +# ===----------------------------------------------------------------------===# |
| 12 | +"""This file defines objects for mocking an update-checkout scheme. It creates |
| 13 | +a json .config file and a series of .git repos with "fake commits". |
| 14 | +""" |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import unittest |
| 20 | + |
| 21 | +# For now we only use a config with a single scheme. We should add support for |
| 22 | +# handling multiple schemes. |
| 23 | +MOCK_REMOTE = { |
| 24 | + 'repo1': [ |
| 25 | + # This is a series of changes to repo1. (File, NewContents) |
| 26 | + ('A.txt', 'A'), |
| 27 | + ('B.txt', 'B'), |
| 28 | + ('A.txt', 'a'), |
| 29 | + ], |
| 30 | + 'repo2': [ |
| 31 | + # This is a series of changes to repo1. (File, NewContents) |
| 32 | + ('X.txt', 'X'), |
| 33 | + ('Y.txt', 'Y'), |
| 34 | + ('X.txt', 'z'), |
| 35 | + ], |
| 36 | +} |
| 37 | + |
| 38 | +MOCK_CONFIG = { |
| 39 | + # We reset this value with our remote path when we process |
| 40 | + 'https-clone-pattern': '', |
| 41 | + 'repos': { |
| 42 | + 'repo1': { |
| 43 | + 'remote': {'id': 'repo1'}, |
| 44 | + }, |
| 45 | + 'repo2': { |
| 46 | + 'remote': {'id': 'repo2'}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + 'default-branch-scheme': 'master', |
| 50 | + 'branch-schemes': { |
| 51 | + 'master': { |
| 52 | + 'aliases': ['master'], |
| 53 | + 'repos': { |
| 54 | + 'repo1': 'master', |
| 55 | + 'repo2': 'master', |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +def call_quietly(*args, **kwargs): |
| 63 | + with open(os.devnull, 'w') as f: |
| 64 | + kwargs['stdout'] = f |
| 65 | + kwargs['stderr'] = f |
| 66 | + subprocess.check_call(*args, **kwargs) |
| 67 | + |
| 68 | + |
| 69 | +def create_dir(d): |
| 70 | + if not os.path.isdir(d): |
| 71 | + os.makedirs(d) |
| 72 | + |
| 73 | + |
| 74 | +def teardown_mock_remote(base_dir): |
| 75 | + call_quietly(['rm', '-rf', base_dir]) |
| 76 | + |
| 77 | + |
| 78 | +def get_config_path(base_dir): |
| 79 | + return os.path.join(base_dir, 'test-config.json') |
| 80 | + |
| 81 | + |
| 82 | +def setup_mock_remote(base_dir): |
| 83 | + create_dir(base_dir) |
| 84 | + |
| 85 | + # We use local as a workspace for creating commits. |
| 86 | + LOCAL_PATH = os.path.join(base_dir, 'local') |
| 87 | + # We use remote as a directory that simulates our remote unchecked out |
| 88 | + # repo. |
| 89 | + REMOTE_PATH = os.path.join(base_dir, 'remote') |
| 90 | + |
| 91 | + create_dir(REMOTE_PATH) |
| 92 | + create_dir(LOCAL_PATH) |
| 93 | + |
| 94 | + for (k, v) in MOCK_REMOTE.items(): |
| 95 | + local_repo_path = os.path.join(LOCAL_PATH, k) |
| 96 | + remote_repo_path = os.path.join(REMOTE_PATH, k) |
| 97 | + create_dir(remote_repo_path) |
| 98 | + create_dir(local_repo_path) |
| 99 | + call_quietly(['git', 'init', '--bare', remote_repo_path]) |
| 100 | + call_quietly(['git', 'clone', '-l', remote_repo_path, local_repo_path]) |
| 101 | + for (i, (filename, contents)) in enumerate(v): |
| 102 | + filename_path = os.path.join(local_repo_path, filename) |
| 103 | + with open(filename_path, 'w') as f: |
| 104 | + f.write(contents) |
| 105 | + call_quietly(['git', 'add', filename], cwd=local_repo_path) |
| 106 | + call_quietly(['git', 'commit', '-m', 'Commit %d' % i], |
| 107 | + cwd=local_repo_path) |
| 108 | + call_quietly(['git', 'push', 'origin', 'master'], |
| 109 | + cwd=local_repo_path) |
| 110 | + |
| 111 | + base_config = MOCK_CONFIG |
| 112 | + https_clone_pattern = os.path.join('file://%s' % REMOTE_PATH, '%s') |
| 113 | + base_config['https-clone-pattern'] = https_clone_pattern |
| 114 | + |
| 115 | + with open(get_config_path(base_dir), 'w') as f: |
| 116 | + json.dump(base_config, f) |
| 117 | + |
| 118 | + |
| 119 | +BASEDIR_ENV_VAR = 'UPDATECHECKOUT_TEST_WORKSPACE_DIR' |
| 120 | +CURRENT_FILE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 121 | +UPDATE_CHECKOUT_PATH = os.path.abspath(os.path.join(CURRENT_FILE_DIR, |
| 122 | + os.path.pardir, |
| 123 | + os.path.pardir, |
| 124 | + 'update-checkout')) |
| 125 | + |
| 126 | + |
| 127 | +class SchemeMockTestCase(unittest.TestCase): |
| 128 | + |
| 129 | + def __init__(self, *args, **kwargs): |
| 130 | + super(SchemeMockTestCase, self).__init__(*args, **kwargs) |
| 131 | + |
| 132 | + self.workspace = os.getenv(BASEDIR_ENV_VAR) |
| 133 | + if self.workspace is None: |
| 134 | + raise RuntimeError('Misconfigured test suite! Environment ' |
| 135 | + 'variable %s must be set!' % BASEDIR_ENV_VAR) |
| 136 | + self.config_path = get_config_path(self.workspace) |
| 137 | + self.update_checkout_path = UPDATE_CHECKOUT_PATH |
| 138 | + if not os.access(self.update_checkout_path, os.X_OK): |
| 139 | + raise RuntimeError('Error! Could not find executable ' |
| 140 | + 'update-checkout at path: %s' |
| 141 | + % self.update_checkout_path) |
| 142 | + self.source_root = os.path.join(self.workspace, 'source_root') |
| 143 | + |
| 144 | + def setUp(self): |
| 145 | + create_dir(self.source_root) |
| 146 | + setup_mock_remote(self.workspace) |
| 147 | + |
| 148 | + def tearDown(self): |
| 149 | + teardown_mock_remote(self.workspace) |
| 150 | + |
| 151 | + def call(self, *args, **kwargs): |
| 152 | + kwargs['cwd'] = self.source_root |
| 153 | + call_quietly(*args, **kwargs) |
0 commit comments