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