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