blob: 373b52c6d3c68643cfe0622f1a622d1098dca7f8 [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
23 self.python_executable = sys.executable
24 self.subprocess = subprocess
25 self.files = []
26 self.is_committing = False
gayanee1702662014-12-13 03:48:0927 self.change = MockChange([])
gayane3dff8c22014-12-04 17:09:5128
agrievef32bcc72016-04-04 14:57:4029 def AffectedFiles(self, file_filter=None, include_deletes=False):
gayane3dff8c22014-12-04 17:09:5130 return self.files
31
glidere61efad2015-02-18 17:39:4332 def AffectedSourceFiles(self, file_filter=None):
33 return self.files
34
davileene0426252015-03-02 21:10:4135 def LocalPaths(self):
36 return self.files
37
gayane3dff8c22014-12-04 17:09:5138 def PresubmitLocalPath(self):
39 return os.path.dirname(__file__)
40
41 def ReadFile(self, filename, mode='rU'):
glidere61efad2015-02-18 17:39:4342 if hasattr(filename, 'AbsoluteLocalPath'):
43 filename = filename.AbsoluteLocalPath()
gayane3dff8c22014-12-04 17:09:5144 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
51class MockOutputApi(object):
gayane860db5c32014-12-05 16:16:4652 """Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5153
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
gayane940df072015-02-24 14:28:3064 def __repr__(self):
65 return self.message
66
gayane3dff8c22014-12-04 17:09:5167 class PresubmitError(PresubmitResult):
davileene0426252015-03-02 21:10:4168 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5169 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
70 self.type = 'error'
71
72 class PresubmitPromptWarning(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 = 'warning'
76
77 class PresubmitNotifyResult(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 = 'notify'
81
82 class PresubmitPromptOrNotify(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 = 'promptOrNotify'
86
87
88class 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
agrievef32bcc72016-04-04 14:57:4095 def __init__(self, local_path, new_contents, action='A'):
gayane3dff8c22014-12-04 17:09:5196 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)]
agrievef32bcc72016-04-04 14:57:4099 self._action = action
gayane3dff8c22014-12-04 17:09:51100
dbeam37e8e7402016-02-10 22:58:20101 def Action(self):
agrievef32bcc72016-04-04 14:57:40102 return self._action
dbeam37e8e7402016-02-10 22:58:20103
gayane3dff8c22014-12-04 17:09:51104 def ChangedContents(self):
105 return self._changed_contents
106
107 def NewContents(self):
108 return self._new_contents
109
110 def LocalPath(self):
111 return self._local_path
112
rdevlin.cronin9ab806c2016-02-26 23:17:13113 def AbsoluteLocalPath(self):
114 return self._local_path
115
davileene0426252015-03-02 21:10:41116 def rfind(self, p):
117 """os.path.basename is called on MockFile so we need an rfind method."""
118 return self._local_path.rfind(p)
119
120 def __getitem__(self, i):
121 """os.path.basename is called on MockFile so we need a get method."""
122 return self._local_path[i]
123
gayane3dff8c22014-12-04 17:09:51124
glidere61efad2015-02-18 17:39:43125class MockAffectedFile(MockFile):
126 def AbsoluteLocalPath(self):
127 return self._local_path
128
129
gayane3dff8c22014-12-04 17:09:51130class MockChange(object):
131 """Mock class for Change class.
132
133 This class can be used in presubmit unittests to mock the query of the
134 current change.
135 """
136
137 def __init__(self, changed_files):
138 self._changed_files = changed_files
139
140 def LocalPaths(self):
141 return self._changed_files