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