Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 1 | # Copyright 2020 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 re |
| 6 | |
| 7 | |
| 8 | class MockInputApi(object): |
| 9 | """Mock class for the InputApi class. |
| 10 | |
| 11 | This class can be used for unittests for presubmit by initializing the files |
| 12 | attribute as the list of changed files. |
| 13 | """ |
| 14 | |
| 15 | def __init__(self): |
| 16 | self.files = [] |
| 17 | self.re = re |
| 18 | |
| 19 | def AffectedFiles(self, file_filter=None, include_deletes=False): |
| 20 | # pylint: disable=unused-argument |
| 21 | return self.files |
| 22 | |
| 23 | |
| 24 | class MockOutputApi(object): |
| 25 | """Mock class for the OutputApi class. |
| 26 | |
| 27 | An instance of this class can be passed to presubmit unittests for outputting |
| 28 | various types of results. |
| 29 | """ |
| 30 | |
| 31 | class PresubmitResult(object): |
| 32 | |
| 33 | def __init__(self, message, items=None, long_text=''): |
| 34 | self.message = message |
| 35 | self.items = items |
| 36 | self.long_text = long_text |
| 37 | |
| 38 | def __repr__(self): |
| 39 | return self.message |
| 40 | |
| 41 | class PresubmitError(PresubmitResult): |
| 42 | |
| 43 | def __init__(self, message, items=None, long_text=''): |
| 44 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 45 | self.type = 'error' |
| 46 | |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 47 | class PresubmitPromptWarning(PresubmitResult): |
| 48 | |
| 49 | def __init__(self, message, items=None, long_text=''): |
| 50 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 51 | self.type = 'warning' |
| 52 | |
Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 53 | |
| 54 | class MockFile(object): |
| 55 | """Mock class for the File class. |
| 56 | |
| 57 | This class can be used to form the mock list of changed files in |
| 58 | MockInputApi for presubmit unittests. |
| 59 | """ |
| 60 | |
| 61 | def __init__(self, |
| 62 | local_path, |
| 63 | new_contents=None, |
| 64 | old_contents=None, |
| 65 | action='A'): |
| 66 | self._local_path = local_path |
| 67 | if new_contents is None: |
| 68 | new_contents = [] |
| 69 | self._new_contents = new_contents |
| 70 | self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
| 71 | self._action = action |
| 72 | self._old_contents = old_contents |
| 73 | |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 74 | def ChangedContents(self): |
| 75 | return self._changed_contents |
| 76 | |
Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 77 | def LocalPath(self): |
| 78 | return self._local_path |