blob: 772f405725be0f575c193ad0ecac46d93d973d06 [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
gayane3dff8c22014-12-04 17:09:51102
dbeam37e8e7402016-02-10 22:58:20103 def Action(self):
agrievef32bcc72016-04-04 14:57:40104 return self._action
dbeam37e8e7402016-02-10 22:58:20105
gayane3dff8c22014-12-04 17:09:51106 def ChangedContents(self):
107 return self._changed_contents
108
109 def NewContents(self):
110 return self._new_contents
111
112 def LocalPath(self):
113 return self._local_path
114
rdevlin.cronin9ab806c2016-02-26 23:17:13115 def AbsoluteLocalPath(self):
116 return self._local_path
117
davileene0426252015-03-02 21:10:41118 def rfind(self, p):
119 """os.path.basename is called on MockFile so we need an rfind method."""
120 return self._local_path.rfind(p)
121
122 def __getitem__(self, i):
123 """os.path.basename is called on MockFile so we need a get method."""
124 return self._local_path[i]
125
pastarmovj89f7ee12016-09-20 14:58:13126 def __len__(self):
127 """os.path.basename is called on MockFile so we need a len method."""
128 return len(self._local_path)
129
gayane3dff8c22014-12-04 17:09:51130
glidere61efad2015-02-18 17:39:43131class MockAffectedFile(MockFile):
132 def AbsoluteLocalPath(self):
133 return self._local_path
134
135
gayane3dff8c22014-12-04 17:09:51136class MockChange(object):
137 """Mock class for Change class.
138
139 This class can be used in presubmit unittests to mock the query of the
140 current change.
141 """
142
143 def __init__(self, changed_files):
144 self._changed_files = changed_files
145
146 def LocalPaths(self):
147 return self._changed_files
rdevlin.cronin113668252016-05-02 17:05:54148
149 def AffectedFiles(self, include_dirs=False, include_deletes=True,
150 file_filter=None):
151 return self._changed_files