charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 1 | # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
| 8 | |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 9 | # This file is inspired to [1]. |
| 10 | # [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py |
| 11 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 12 | import os.path |
| 13 | import re |
| 14 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 15 | |
| 16 | class MockInputApi(object): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 17 | """Mock class for the InputApi class. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 18 | |
| 19 | This class can be used for unittests for presubmit by initializing the files |
| 20 | attribute as the list of changed files. |
| 21 | """ |
| 22 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 23 | def __init__(self): |
| 24 | self.change = MockChange([], []) |
| 25 | self.files = [] |
| 26 | self.presubmit_local_path = os.path.dirname(__file__) |
Mirko Bonadei | a639513 | 2021-07-22 17:35:59 +0200 | [diff] [blame^] | 27 | self.re = re # pylint: disable=invalid-name |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 28 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 29 | def AffectedSourceFiles(self, file_filter=None): |
| 30 | return self.AffectedFiles(file_filter=file_filter) |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 31 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 32 | def AffectedFiles(self, file_filter=None, include_deletes=False): |
Mirko Bonadei | a639513 | 2021-07-22 17:35:59 +0200 | [diff] [blame^] | 33 | for f in self.files: |
| 34 | if file_filter and not file_filter(f): |
| 35 | continue |
| 36 | if not include_deletes and f.Action() == 'D': |
| 37 | continue |
| 38 | yield f |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 39 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 40 | @classmethod |
| 41 | def FilterSourceFile(cls, |
| 42 | affected_file, |
| 43 | files_to_check=(), |
| 44 | files_to_skip=()): |
| 45 | # pylint: disable=unused-argument |
| 46 | return True |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 47 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 48 | def PresubmitLocalPath(self): |
| 49 | return self.presubmit_local_path |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 50 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 51 | def ReadFile(self, affected_file, mode='rU'): |
| 52 | filename = affected_file.AbsoluteLocalPath() |
| 53 | for f in self.files: |
| 54 | if f.LocalPath() == filename: |
| 55 | with open(filename, mode) as f: |
| 56 | return f.read() |
| 57 | # Otherwise, file is not in our mock API. |
| 58 | raise IOError, "No such file or directory: '%s'" % filename |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 59 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 60 | |
| 61 | class MockOutputApi(object): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 62 | """Mock class for the OutputApi class. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 63 | |
| 64 | An instance of this class can be passed to presubmit unittests for outputing |
| 65 | various types of results. |
| 66 | """ |
| 67 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 68 | class PresubmitResult(object): |
| 69 | def __init__(self, message, items=None, long_text=''): |
| 70 | self.message = message |
| 71 | self.items = items |
| 72 | self.long_text = long_text |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 73 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 74 | def __repr__(self): |
| 75 | return self.message |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 76 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 77 | class PresubmitError(PresubmitResult): |
| 78 | def __init__(self, message, items=None, long_text=''): |
| 79 | MockOutputApi.PresubmitResult.__init__(self, message, items, |
| 80 | long_text) |
| 81 | self.type = 'error' |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 82 | |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 83 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 84 | class MockChange(object): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 85 | """Mock class for Change class. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 86 | |
| 87 | This class can be used in presubmit unittests to mock the query of the |
| 88 | current change. |
| 89 | """ |
| 90 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 91 | def __init__(self, changed_files, bugs_from_description, tags=None): |
| 92 | self._changed_files = changed_files |
| 93 | self._bugs_from_description = bugs_from_description |
| 94 | self.tags = dict() if not tags else tags |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 95 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 96 | def BugsFromDescription(self): |
| 97 | return self._bugs_from_description |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 98 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 99 | def __getattr__(self, attr): |
| 100 | """Return tags directly as attributes on the object.""" |
| 101 | if not re.match(r"^[A-Z_]*$", attr): |
| 102 | raise AttributeError(self, attr) |
| 103 | return self.tags.get(attr) |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 104 | |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 105 | |
| 106 | class MockFile(object): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 107 | """Mock class for the File class. |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 108 | |
| 109 | This class can be used to form the mock list of changed files in |
| 110 | MockInputApi for presubmit unittests. |
| 111 | """ |
| 112 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 113 | def __init__(self, |
| 114 | local_path, |
| 115 | new_contents=None, |
| 116 | old_contents=None, |
| 117 | action='A'): |
| 118 | if new_contents is None: |
| 119 | new_contents = ["Data"] |
| 120 | self._local_path = local_path |
| 121 | self._new_contents = new_contents |
| 122 | self._changed_contents = [(i + 1, l) |
| 123 | for i, l in enumerate(new_contents)] |
| 124 | self._action = action |
| 125 | self._old_contents = old_contents |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 126 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 127 | def Action(self): |
| 128 | return self._action |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 129 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 130 | def ChangedContents(self): |
| 131 | return self._changed_contents |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 132 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 133 | def NewContents(self): |
| 134 | return self._new_contents |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 135 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 136 | def LocalPath(self): |
| 137 | return self._local_path |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 138 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 139 | def AbsoluteLocalPath(self): |
| 140 | return self._local_path |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 141 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 142 | def OldContents(self): |
| 143 | return self._old_contents |