blob: e2e43e35372700e182081d511d096f560625ae56 [file] [log] [blame]
gayane3dff8c22014-12-04 17:09:511# 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
5import json
6import os
7import re
8import subprocess
9import sys
10
11
12class 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
agrievebb9c5b472016-04-22 15:13:0023 self.platform = sys.platform
gayane3dff8c22014-12-04 17:09:5124 self.python_executable = sys.executable
pastarmovj89f7ee12016-09-20 14:58:1325 self.platform = sys.platform
gayane3dff8c22014-12-04 17:09:5126 self.subprocess = subprocess
27 self.files = []
28 self.is_committing = False
gayanee1702662014-12-13 03:48:0929 self.change = MockChange([])
dpapad5c9c24e2017-05-31 20:51:3430 self.presubmit_local_path = os.path.dirname(__file__)
gayane3dff8c22014-12-04 17:09:5131
agrievef32bcc72016-04-04 14:57:4032 def AffectedFiles(self, file_filter=None, include_deletes=False):
gayane3dff8c22014-12-04 17:09:5133 return self.files
34
glidere61efad2015-02-18 17:39:4335 def AffectedSourceFiles(self, file_filter=None):
36 return self.files
37
davileene0426252015-03-02 21:10:4138 def LocalPaths(self):
39 return self.files
40
gayane3dff8c22014-12-04 17:09:5141 def PresubmitLocalPath(self):
dpapad5c9c24e2017-05-31 20:51:3442 return self.presubmit_local_path
gayane3dff8c22014-12-04 17:09:5143
44 def ReadFile(self, filename, mode='rU'):
glidere61efad2015-02-18 17:39:4345 if hasattr(filename, 'AbsoluteLocalPath'):
46 filename = filename.AbsoluteLocalPath()
gayane3dff8c22014-12-04 17:09:5147 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
54class MockOutputApi(object):
gayane860db5c32014-12-05 16:16:4655 """Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5156
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
gayane940df072015-02-24 14:28:3067 def __repr__(self):
68 return self.message
69
gayane3dff8c22014-12-04 17:09:5170 class PresubmitError(PresubmitResult):
davileene0426252015-03-02 21:10:4171 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5172 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
73 self.type = 'error'
74
75 class PresubmitPromptWarning(PresubmitResult):
davileene0426252015-03-02 21:10:4176 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5177 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
78 self.type = 'warning'
79
80 class PresubmitNotifyResult(PresubmitResult):
davileene0426252015-03-02 21:10:4181 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5182 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
83 self.type = 'notify'
84
85 class PresubmitPromptOrNotify(PresubmitResult):
davileene0426252015-03-02 21:10:4186 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5187 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
88 self.type = 'promptOrNotify'
89
90
91class 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
agrievef32bcc72016-04-04 14:57:4098 def __init__(self, local_path, new_contents, action='A'):
gayane3dff8c22014-12-04 17:09:5199 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)]
agrievef32bcc72016-04-04 14:57:40102 self._action = action
jbriance9e12f162016-11-25 07:57:50103 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
gayane3dff8c22014-12-04 17:09:51107
dbeam37e8e7402016-02-10 22:58:20108 def Action(self):
agrievef32bcc72016-04-04 14:57:40109 return self._action
dbeam37e8e7402016-02-10 22:58:20110
gayane3dff8c22014-12-04 17:09:51111 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.cronin9ab806c2016-02-26 23:17:13120 def AbsoluteLocalPath(self):
121 return self._local_path
122
jbriance9e12f162016-11-25 07:57:50123 def GenerateScmDiff(self):
jbriance2c51e821a2016-12-12 08:24:31124 return self._scm_diff
jbriance9e12f162016-11-25 07:57:50125
davileene0426252015-03-02 21:10:41126 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
pastarmovj89f7ee12016-09-20 14:58:13134 def __len__(self):
135 """os.path.basename is called on MockFile so we need a len method."""
136 return len(self._local_path)
137
gayane3dff8c22014-12-04 17:09:51138
glidere61efad2015-02-18 17:39:43139class MockAffectedFile(MockFile):
140 def AbsoluteLocalPath(self):
141 return self._local_path
142
143
gayane3dff8c22014-12-04 17:09:51144class 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.cronin113668252016-05-02 17:05:54156
157 def AffectedFiles(self, include_dirs=False, include_deletes=True,
158 file_filter=None):
159 return self._changed_files