blob: c6aa84ab6002764c9b0fdd4a0761a119937d0ebd [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
Daniel Cheng13ca61a882017-08-25 15:11:255import fnmatch
gayane3dff8c22014-12-04 17:09:516import json
7import os
8import re
9import subprocess
10import sys
11
12
13class 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 Cheng13ca61a882017-08-25 15:11:2521 self.fnmatch = fnmatch
gayane3dff8c22014-12-04 17:09:5122 self.json = json
23 self.re = re
24 self.os_path = os.path
agrievebb9c5b472016-04-22 15:13:0025 self.platform = sys.platform
gayane3dff8c22014-12-04 17:09:5126 self.python_executable = sys.executable
pastarmovj89f7ee12016-09-20 14:58:1327 self.platform = sys.platform
gayane3dff8c22014-12-04 17:09:5128 self.subprocess = subprocess
29 self.files = []
30 self.is_committing = False
gayanee1702662014-12-13 03:48:0931 self.change = MockChange([])
dpapad5c9c24e2017-05-31 20:51:3432 self.presubmit_local_path = os.path.dirname(__file__)
gayane3dff8c22014-12-04 17:09:5133
agrievef32bcc72016-04-04 14:57:4034 def AffectedFiles(self, file_filter=None, include_deletes=False):
gayane3dff8c22014-12-04 17:09:5135 return self.files
36
glidere61efad2015-02-18 17:39:4337 def AffectedSourceFiles(self, file_filter=None):
38 return self.files
39
davileene0426252015-03-02 21:10:4140 def LocalPaths(self):
41 return self.files
42
gayane3dff8c22014-12-04 17:09:5143 def PresubmitLocalPath(self):
dpapad5c9c24e2017-05-31 20:51:3444 return self.presubmit_local_path
gayane3dff8c22014-12-04 17:09:5145
46 def ReadFile(self, filename, mode='rU'):
glidere61efad2015-02-18 17:39:4347 if hasattr(filename, 'AbsoluteLocalPath'):
48 filename = filename.AbsoluteLocalPath()
gayane3dff8c22014-12-04 17:09:5149 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
56class MockOutputApi(object):
gayane860db5c32014-12-05 16:16:4657 """Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5158
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
gayane940df072015-02-24 14:28:3069 def __repr__(self):
70 return self.message
71
gayane3dff8c22014-12-04 17:09:5172 class PresubmitError(PresubmitResult):
davileene0426252015-03-02 21:10:4173 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5174 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
75 self.type = 'error'
76
77 class PresubmitPromptWarning(PresubmitResult):
davileene0426252015-03-02 21:10:4178 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5179 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
80 self.type = 'warning'
81
82 class PresubmitNotifyResult(PresubmitResult):
davileene0426252015-03-02 21:10:4183 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5184 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
85 self.type = 'notify'
86
87 class PresubmitPromptOrNotify(PresubmitResult):
davileene0426252015-03-02 21:10:4188 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5189 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
90 self.type = 'promptOrNotify'
91
92
93class 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 Yanb92fa522017-08-28 17:37:06100 def __init__(self, local_path, new_contents, old_contents=None, action='A'):
gayane3dff8c22014-12-04 17:09:51101 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)]
agrievef32bcc72016-04-04 14:57:40104 self._action = action
jbriance9e12f162016-11-25 07:57:50105 self._scm_diff = "--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n" % (local_path,
106 len(new_contents))
Yoland Yanb92fa522017-08-28 17:37:06107 self._old_contents = old_contents
jbriance9e12f162016-11-25 07:57:50108 for l in new_contents:
109 self._scm_diff += "+%s\n" % l
gayane3dff8c22014-12-04 17:09:51110
dbeam37e8e7402016-02-10 22:58:20111 def Action(self):
agrievef32bcc72016-04-04 14:57:40112 return self._action
dbeam37e8e7402016-02-10 22:58:20113
gayane3dff8c22014-12-04 17:09:51114 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.cronin9ab806c2016-02-26 23:17:13123 def AbsoluteLocalPath(self):
124 return self._local_path
125
jbriance9e12f162016-11-25 07:57:50126 def GenerateScmDiff(self):
jbriance2c51e821a2016-12-12 08:24:31127 return self._scm_diff
jbriance9e12f162016-11-25 07:57:50128
Yoland Yanb92fa522017-08-28 17:37:06129 def OldContents(self):
130 return self._old_contents
131
davileene0426252015-03-02 21:10:41132 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
pastarmovj89f7ee12016-09-20 14:58:13140 def __len__(self):
141 """os.path.basename is called on MockFile so we need a len method."""
142 return len(self._local_path)
143
gayane3dff8c22014-12-04 17:09:51144
glidere61efad2015-02-18 17:39:43145class MockAffectedFile(MockFile):
146 def AbsoluteLocalPath(self):
147 return self._local_path
148
149
gayane3dff8c22014-12-04 17:09:51150class 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.cronin113668252016-05-02 17:05:54162
163 def AffectedFiles(self, include_dirs=False, include_deletes=True,
164 file_filter=None):
165 return self._changed_files