blob: 162570ef4c85b49d1e3d9f7f4b53b1bddb7503ed [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
agrievef32bcc72016-04-04 14:57:40100 def __init__(self, local_path, new_contents, 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))
107 for l in new_contents:
108 self._scm_diff += "+%s\n" % l
gayane3dff8c22014-12-04 17:09:51109
dbeam37e8e7402016-02-10 22:58:20110 def Action(self):
agrievef32bcc72016-04-04 14:57:40111 return self._action
dbeam37e8e7402016-02-10 22:58:20112
gayane3dff8c22014-12-04 17:09:51113 def ChangedContents(self):
114 return self._changed_contents
115
116 def NewContents(self):
117 return self._new_contents
118
119 def LocalPath(self):
120 return self._local_path
121
rdevlin.cronin9ab806c2016-02-26 23:17:13122 def AbsoluteLocalPath(self):
123 return self._local_path
124
jbriance9e12f162016-11-25 07:57:50125 def GenerateScmDiff(self):
jbriance2c51e821a2016-12-12 08:24:31126 return self._scm_diff
jbriance9e12f162016-11-25 07:57:50127
davileene0426252015-03-02 21:10:41128 def rfind(self, p):
129 """os.path.basename is called on MockFile so we need an rfind method."""
130 return self._local_path.rfind(p)
131
132 def __getitem__(self, i):
133 """os.path.basename is called on MockFile so we need a get method."""
134 return self._local_path[i]
135
pastarmovj89f7ee12016-09-20 14:58:13136 def __len__(self):
137 """os.path.basename is called on MockFile so we need a len method."""
138 return len(self._local_path)
139
gayane3dff8c22014-12-04 17:09:51140
glidere61efad2015-02-18 17:39:43141class MockAffectedFile(MockFile):
142 def AbsoluteLocalPath(self):
143 return self._local_path
144
145
gayane3dff8c22014-12-04 17:09:51146class MockChange(object):
147 """Mock class for Change class.
148
149 This class can be used in presubmit unittests to mock the query of the
150 current change.
151 """
152
153 def __init__(self, changed_files):
154 self._changed_files = changed_files
155
156 def LocalPaths(self):
157 return self._changed_files
rdevlin.cronin113668252016-05-02 17:05:54158
159 def AffectedFiles(self, include_dirs=False, include_deletes=True,
160 file_filter=None):
161 return self._changed_files