blob: c269deec1fba066392fd1252ab30fac2715e4ea5 [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([])
gayane3dff8c22014-12-04 17:09:5130
agrievef32bcc72016-04-04 14:57:4031 def AffectedFiles(self, file_filter=None, include_deletes=False):
gayane3dff8c22014-12-04 17:09:5132 return self.files
33
glidere61efad2015-02-18 17:39:4334 def AffectedSourceFiles(self, file_filter=None):
35 return self.files
36
davileene0426252015-03-02 21:10:4137 def LocalPaths(self):
38 return self.files
39
gayane3dff8c22014-12-04 17:09:5140 def PresubmitLocalPath(self):
41 return os.path.dirname(__file__)
42
43 def ReadFile(self, filename, mode='rU'):
glidere61efad2015-02-18 17:39:4344 if hasattr(filename, 'AbsoluteLocalPath'):
45 filename = filename.AbsoluteLocalPath()
gayane3dff8c22014-12-04 17:09:5146 for file_ in self.files:
47 if file_.LocalPath() == filename:
48 return '\n'.join(file_.NewContents())
49 # Otherwise, file is not in our mock API.
50 raise IOError, "No such file or directory: '%s'" % filename
51
52
53class MockOutputApi(object):
gayane860db5c32014-12-05 16:16:4654 """Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5155
56 An instance of this class can be passed to presubmit unittests for outputing
57 various types of results.
58 """
59
60 class PresubmitResult(object):
61 def __init__(self, message, items=None, long_text=''):
62 self.message = message
63 self.items = items
64 self.long_text = long_text
65
gayane940df072015-02-24 14:28:3066 def __repr__(self):
67 return self.message
68
gayane3dff8c22014-12-04 17:09:5169 class PresubmitError(PresubmitResult):
davileene0426252015-03-02 21:10:4170 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5171 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
72 self.type = 'error'
73
74 class PresubmitPromptWarning(PresubmitResult):
davileene0426252015-03-02 21:10:4175 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5176 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
77 self.type = 'warning'
78
79 class PresubmitNotifyResult(PresubmitResult):
davileene0426252015-03-02 21:10:4180 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5181 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
82 self.type = 'notify'
83
84 class PresubmitPromptOrNotify(PresubmitResult):
davileene0426252015-03-02 21:10:4185 def __init__(self, message, items=None, long_text=''):
gayane3dff8c22014-12-04 17:09:5186 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
87 self.type = 'promptOrNotify'
88
89
90class MockFile(object):
91 """Mock class for the File class.
92
93 This class can be used to form the mock list of changed files in
94 MockInputApi for presubmit unittests.
95 """
96
agrievef32bcc72016-04-04 14:57:4097 def __init__(self, local_path, new_contents, action='A'):
gayane3dff8c22014-12-04 17:09:5198 self._local_path = local_path
99 self._new_contents = new_contents
100 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
agrievef32bcc72016-04-04 14:57:40101 self._action = action
jbriance9e12f162016-11-25 07:57:50102 self._scm_diff = "--- /dev/null\n+++ %s\n@@ -0,0 +1,%d @@\n" % (local_path,
103 len(new_contents))
104 for l in new_contents:
105 self._scm_diff += "+%s\n" % l
gayane3dff8c22014-12-04 17:09:51106
dbeam37e8e7402016-02-10 22:58:20107 def Action(self):
agrievef32bcc72016-04-04 14:57:40108 return self._action
dbeam37e8e7402016-02-10 22:58:20109
gayane3dff8c22014-12-04 17:09:51110 def ChangedContents(self):
111 return self._changed_contents
112
113 def NewContents(self):
114 return self._new_contents
115
116 def LocalPath(self):
117 return self._local_path
118
rdevlin.cronin9ab806c2016-02-26 23:17:13119 def AbsoluteLocalPath(self):
120 return self._local_path
121
jbriance9e12f162016-11-25 07:57:50122 def GenerateScmDiff(self):
jbriance2c51e821a2016-12-12 08:24:31123 return self._scm_diff
jbriance9e12f162016-11-25 07:57:50124
davileene0426252015-03-02 21:10:41125 def rfind(self, p):
126 """os.path.basename is called on MockFile so we need an rfind method."""
127 return self._local_path.rfind(p)
128
129 def __getitem__(self, i):
130 """os.path.basename is called on MockFile so we need a get method."""
131 return self._local_path[i]
132
pastarmovj89f7ee12016-09-20 14:58:13133 def __len__(self):
134 """os.path.basename is called on MockFile so we need a len method."""
135 return len(self._local_path)
136
gayane3dff8c22014-12-04 17:09:51137
glidere61efad2015-02-18 17:39:43138class MockAffectedFile(MockFile):
139 def AbsoluteLocalPath(self):
140 return self._local_path
141
142
gayane3dff8c22014-12-04 17:09:51143class MockChange(object):
144 """Mock class for Change class.
145
146 This class can be used in presubmit unittests to mock the query of the
147 current change.
148 """
149
150 def __init__(self, changed_files):
151 self._changed_files = changed_files
152
153 def LocalPaths(self):
154 return self._changed_files
rdevlin.cronin113668252016-05-02 17:05:54155
156 def AffectedFiles(self, include_dirs=False, include_deletes=True,
157 file_filter=None):
158 return self._changed_files