gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 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 json |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | |
| 12 | class MockInputApi(object): |
| 13 | """Mock class for the InputApi class. |
| 14 | |
| 15 | This class can be used for unittests for presubmit by initializing the files |
| 16 | attribute as the list of changed files. |
| 17 | """ |
| 18 | |
| 19 | def __init__(self): |
| 20 | self.json = json |
| 21 | self.re = re |
| 22 | self.os_path = os.path |
agrieve | bb9c5b47 | 2016-04-22 15:13:00 | [diff] [blame] | 23 | self.platform = sys.platform |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 24 | self.python_executable = sys.executable |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 25 | self.platform = sys.platform |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 26 | self.subprocess = subprocess |
| 27 | self.files = [] |
| 28 | self.is_committing = False |
gayane | e170266 | 2014-12-13 03:48:09 | [diff] [blame] | 29 | self.change = MockChange([]) |
dpapad | 5c9c24e | 2017-05-31 20:51:34 | [diff] [blame] | 30 | self.presubmit_local_path = os.path.dirname(__file__) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 31 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 32 | def AffectedFiles(self, file_filter=None, include_deletes=False): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 33 | return self.files |
| 34 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 35 | def AffectedSourceFiles(self, file_filter=None): |
| 36 | return self.files |
| 37 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 38 | def LocalPaths(self): |
| 39 | return self.files |
| 40 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 41 | def PresubmitLocalPath(self): |
dpapad | 5c9c24e | 2017-05-31 20:51:34 | [diff] [blame] | 42 | return self.presubmit_local_path |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 43 | |
| 44 | def ReadFile(self, filename, mode='rU'): |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 45 | if hasattr(filename, 'AbsoluteLocalPath'): |
| 46 | filename = filename.AbsoluteLocalPath() |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 47 | for file_ in self.files: |
| 48 | if file_.LocalPath() == filename: |
| 49 | return '\n'.join(file_.NewContents()) |
| 50 | # Otherwise, file is not in our mock API. |
| 51 | raise IOError, "No such file or directory: '%s'" % filename |
| 52 | |
| 53 | |
| 54 | class MockOutputApi(object): |
gayane | 860db5c3 | 2014-12-05 16:16:46 | [diff] [blame] | 55 | """Mock class for the OutputApi class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 56 | |
| 57 | An instance of this class can be passed to presubmit unittests for outputing |
| 58 | various types of results. |
| 59 | """ |
| 60 | |
| 61 | class PresubmitResult(object): |
| 62 | def __init__(self, message, items=None, long_text=''): |
| 63 | self.message = message |
| 64 | self.items = items |
| 65 | self.long_text = long_text |
| 66 | |
gayane | 940df07 | 2015-02-24 14:28:30 | [diff] [blame] | 67 | def __repr__(self): |
| 68 | return self.message |
| 69 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 70 | class PresubmitError(PresubmitResult): |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 71 | def __init__(self, message, items=None, long_text=''): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 72 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 73 | self.type = 'error' |
| 74 | |
| 75 | class PresubmitPromptWarning(PresubmitResult): |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 76 | def __init__(self, message, items=None, long_text=''): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 77 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 78 | self.type = 'warning' |
| 79 | |
| 80 | class PresubmitNotifyResult(PresubmitResult): |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 81 | def __init__(self, message, items=None, long_text=''): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 82 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 83 | self.type = 'notify' |
| 84 | |
| 85 | class PresubmitPromptOrNotify(PresubmitResult): |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 86 | def __init__(self, message, items=None, long_text=''): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 87 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 88 | self.type = 'promptOrNotify' |
| 89 | |
| 90 | |
| 91 | class MockFile(object): |
| 92 | """Mock class for the File class. |
| 93 | |
| 94 | This class can be used to form the mock list of changed files in |
| 95 | MockInputApi for presubmit unittests. |
| 96 | """ |
| 97 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 98 | def __init__(self, local_path, new_contents, action='A'): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 99 | self._local_path = local_path |
| 100 | self._new_contents = new_contents |
| 101 | self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 102 | self._action = action |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 103 | self._scm_diff = "--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n" % (local_path, |
| 104 | len(new_contents)) |
| 105 | for l in new_contents: |
| 106 | self._scm_diff += "+%s\n" % l |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 107 | |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 108 | def Action(self): |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 109 | return self._action |
dbeam | 37e8e740 | 2016-02-10 22:58:20 | [diff] [blame] | 110 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 111 | def ChangedContents(self): |
| 112 | return self._changed_contents |
| 113 | |
| 114 | def NewContents(self): |
| 115 | return self._new_contents |
| 116 | |
| 117 | def LocalPath(self): |
| 118 | return self._local_path |
| 119 | |
rdevlin.cronin | 9ab806c | 2016-02-26 23:17:13 | [diff] [blame] | 120 | def AbsoluteLocalPath(self): |
| 121 | return self._local_path |
| 122 | |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 123 | def GenerateScmDiff(self): |
jbriance | 2c51e821a | 2016-12-12 08:24:31 | [diff] [blame] | 124 | return self._scm_diff |
jbriance | 9e12f16 | 2016-11-25 07:57:50 | [diff] [blame] | 125 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 126 | def rfind(self, p): |
| 127 | """os.path.basename is called on MockFile so we need an rfind method.""" |
| 128 | return self._local_path.rfind(p) |
| 129 | |
| 130 | def __getitem__(self, i): |
| 131 | """os.path.basename is called on MockFile so we need a get method.""" |
| 132 | return self._local_path[i] |
| 133 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 134 | def __len__(self): |
| 135 | """os.path.basename is called on MockFile so we need a len method.""" |
| 136 | return len(self._local_path) |
| 137 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 138 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 139 | class MockAffectedFile(MockFile): |
| 140 | def AbsoluteLocalPath(self): |
| 141 | return self._local_path |
| 142 | |
| 143 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 144 | class MockChange(object): |
| 145 | """Mock class for Change class. |
| 146 | |
| 147 | This class can be used in presubmit unittests to mock the query of the |
| 148 | current change. |
| 149 | """ |
| 150 | |
| 151 | def __init__(self, changed_files): |
| 152 | self._changed_files = changed_files |
| 153 | |
| 154 | def LocalPaths(self): |
| 155 | return self._changed_files |
rdevlin.cronin | 11366825 | 2016-05-02 17:05:54 | [diff] [blame] | 156 | |
| 157 | def AffectedFiles(self, include_dirs=False, include_deletes=True, |
| 158 | file_filter=None): |
| 159 | return self._changed_files |