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 |
| 23 | self.python_executable = sys.executable |
| 24 | self.subprocess = subprocess |
| 25 | self.files = [] |
| 26 | self.is_committing = False |
gayane | e170266 | 2014-12-13 03:48:09 | [diff] [blame] | 27 | self.change = MockChange([]) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 28 | |
| 29 | def AffectedFiles(self, file_filter=None): |
| 30 | return self.files |
| 31 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 32 | def AffectedSourceFiles(self, file_filter=None): |
| 33 | return self.files |
| 34 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 35 | def LocalPaths(self): |
| 36 | return self.files |
| 37 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 38 | def PresubmitLocalPath(self): |
| 39 | return os.path.dirname(__file__) |
| 40 | |
| 41 | def ReadFile(self, filename, mode='rU'): |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 42 | if hasattr(filename, 'AbsoluteLocalPath'): |
| 43 | filename = filename.AbsoluteLocalPath() |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 44 | for file_ in self.files: |
| 45 | if file_.LocalPath() == filename: |
| 46 | return '\n'.join(file_.NewContents()) |
| 47 | # Otherwise, file is not in our mock API. |
| 48 | raise IOError, "No such file or directory: '%s'" % filename |
| 49 | |
| 50 | |
| 51 | class MockOutputApi(object): |
gayane | 860db5c3 | 2014-12-05 16:16:46 | [diff] [blame] | 52 | """Mock class for the OutputApi class. |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 53 | |
| 54 | An instance of this class can be passed to presubmit unittests for outputing |
| 55 | various types of results. |
| 56 | """ |
| 57 | |
| 58 | class PresubmitResult(object): |
| 59 | def __init__(self, message, items=None, long_text=''): |
| 60 | self.message = message |
| 61 | self.items = items |
| 62 | self.long_text = long_text |
| 63 | |
gayane | 940df07 | 2015-02-24 14:28:30 | [diff] [blame] | 64 | def __repr__(self): |
| 65 | return self.message |
| 66 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 67 | class PresubmitError(PresubmitResult): |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 68 | def __init__(self, message, items=None, long_text=''): |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 69 | MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) |
| 70 | self.type = 'error' |
| 71 | |
| 72 | class PresubmitPromptWarning(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 = 'warning' |
| 76 | |
| 77 | class PresubmitNotifyResult(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 = 'notify' |
| 81 | |
| 82 | class PresubmitPromptOrNotify(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 = 'promptOrNotify' |
| 86 | |
| 87 | |
| 88 | class MockFile(object): |
| 89 | """Mock class for the File class. |
| 90 | |
| 91 | This class can be used to form the mock list of changed files in |
| 92 | MockInputApi for presubmit unittests. |
| 93 | """ |
| 94 | |
| 95 | def __init__(self, local_path, new_contents): |
| 96 | self._local_path = local_path |
| 97 | self._new_contents = new_contents |
| 98 | self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] |
| 99 | |
| 100 | def ChangedContents(self): |
| 101 | return self._changed_contents |
| 102 | |
| 103 | def NewContents(self): |
| 104 | return self._new_contents |
| 105 | |
| 106 | def LocalPath(self): |
| 107 | return self._local_path |
| 108 | |
davileen | e042625 | 2015-03-02 21:10:41 | [diff] [blame] | 109 | def rfind(self, p): |
| 110 | """os.path.basename is called on MockFile so we need an rfind method.""" |
| 111 | return self._local_path.rfind(p) |
| 112 | |
| 113 | def __getitem__(self, i): |
| 114 | """os.path.basename is called on MockFile so we need a get method.""" |
| 115 | return self._local_path[i] |
| 116 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 117 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 118 | class MockAffectedFile(MockFile): |
| 119 | def AbsoluteLocalPath(self): |
| 120 | return self._local_path |
| 121 | |
| 122 | |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 123 | class MockChange(object): |
| 124 | """Mock class for Change class. |
| 125 | |
| 126 | This class can be used in presubmit unittests to mock the query of the |
| 127 | current change. |
| 128 | """ |
| 129 | |
| 130 | def __init__(self, changed_files): |
| 131 | self._changed_files = changed_files |
| 132 | |
| 133 | def LocalPaths(self): |
| 134 | return self._changed_files |