blob: b15eb74dd88b14dacb2e9d1d318fa3defbee1055 [file] [log] [blame]
charujain9893e252017-09-14 11:33:221# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS. All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
8
Mirko Bonadeia730c1c2017-09-18 09:33:139# This file is inspired to [1].
10# [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py
11
Artem Titove92675b2018-05-22 08:21:2712import os.path
13import re
14
charujain9893e252017-09-14 11:33:2215
16class MockInputApi(object):
Mirko Bonadei8cc66952020-10-30 09:13:4517 """Mock class for the InputApi class.
charujain9893e252017-09-14 11:33:2218
19 This class can be used for unittests for presubmit by initializing the files
20 attribute as the list of changed files.
21 """
22
Mirko Bonadei8cc66952020-10-30 09:13:4523 def __init__(self):
24 self.change = MockChange([], [])
25 self.files = []
26 self.presubmit_local_path = os.path.dirname(__file__)
Mirko Bonadeia730c1c2017-09-18 09:33:1327
Mirko Bonadei8cc66952020-10-30 09:13:4528 def AffectedSourceFiles(self, file_filter=None):
29 return self.AffectedFiles(file_filter=file_filter)
Artem Titove92675b2018-05-22 08:21:2730
Mirko Bonadei8cc66952020-10-30 09:13:4531 def AffectedFiles(self, file_filter=None, include_deletes=False):
32 # pylint: disable=unused-argument
33 return self.files
charujain9893e252017-09-14 11:33:2234
Mirko Bonadei8cc66952020-10-30 09:13:4535 @classmethod
36 def FilterSourceFile(cls,
37 affected_file,
38 files_to_check=(),
39 files_to_skip=()):
40 # pylint: disable=unused-argument
41 return True
Artem Titove92675b2018-05-22 08:21:2742
Mirko Bonadei8cc66952020-10-30 09:13:4543 def PresubmitLocalPath(self):
44 return self.presubmit_local_path
Artem Titove92675b2018-05-22 08:21:2745
Mirko Bonadei8cc66952020-10-30 09:13:4546 def ReadFile(self, affected_file, mode='rU'):
47 filename = affected_file.AbsoluteLocalPath()
48 for f in self.files:
49 if f.LocalPath() == filename:
50 with open(filename, mode) as f:
51 return f.read()
52 # Otherwise, file is not in our mock API.
53 raise IOError, "No such file or directory: '%s'" % filename
Mirko Bonadei4dc4e252017-09-19 11:49:1654
charujain9893e252017-09-14 11:33:2255
56class MockOutputApi(object):
Mirko Bonadei8cc66952020-10-30 09:13:4557 """Mock class for the OutputApi class.
charujain9893e252017-09-14 11:33:2258
59 An instance of this class can be passed to presubmit unittests for outputing
60 various types of results.
61 """
62
Mirko Bonadei8cc66952020-10-30 09:13:4563 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
charujain9893e252017-09-14 11:33:2268
Mirko Bonadei8cc66952020-10-30 09:13:4569 def __repr__(self):
70 return self.message
charujain9893e252017-09-14 11:33:2271
Mirko Bonadei8cc66952020-10-30 09:13:4572 class PresubmitError(PresubmitResult):
73 def __init__(self, message, items=None, long_text=''):
74 MockOutputApi.PresubmitResult.__init__(self, message, items,
75 long_text)
76 self.type = 'error'
charujain9893e252017-09-14 11:33:2277
Mirko Bonadeia730c1c2017-09-18 09:33:1378
charujain9893e252017-09-14 11:33:2279class MockChange(object):
Mirko Bonadei8cc66952020-10-30 09:13:4580 """Mock class for Change class.
charujain9893e252017-09-14 11:33:2281
82 This class can be used in presubmit unittests to mock the query of the
83 current change.
84 """
85
Mirko Bonadei8cc66952020-10-30 09:13:4586 def __init__(self, changed_files, bugs_from_description, tags=None):
87 self._changed_files = changed_files
88 self._bugs_from_description = bugs_from_description
89 self.tags = dict() if not tags else tags
Mirko Bonadei61880182017-10-12 13:12:3590
Mirko Bonadei8cc66952020-10-30 09:13:4591 def BugsFromDescription(self):
92 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 09:33:1393
Mirko Bonadei8cc66952020-10-30 09:13:4594 def __getattr__(self, attr):
95 """Return tags directly as attributes on the object."""
96 if not re.match(r"^[A-Z_]*$", attr):
97 raise AttributeError(self, attr)
98 return self.tags.get(attr)
Artem Titove92675b2018-05-22 08:21:2799
Mirko Bonadeia730c1c2017-09-18 09:33:13100
101class MockFile(object):
Mirko Bonadei8cc66952020-10-30 09:13:45102 """Mock class for the File class.
Mirko Bonadeia730c1c2017-09-18 09:33:13103
104 This class can be used to form the mock list of changed files in
105 MockInputApi for presubmit unittests.
106 """
107
Mirko Bonadei8cc66952020-10-30 09:13:45108 def __init__(self,
109 local_path,
110 new_contents=None,
111 old_contents=None,
112 action='A'):
113 if new_contents is None:
114 new_contents = ["Data"]
115 self._local_path = local_path
116 self._new_contents = new_contents
117 self._changed_contents = [(i + 1, l)
118 for i, l in enumerate(new_contents)]
119 self._action = action
120 self._old_contents = old_contents
Artem Titove92675b2018-05-22 08:21:27121
Mirko Bonadei8cc66952020-10-30 09:13:45122 def Action(self):
123 return self._action
Artem Titove92675b2018-05-22 08:21:27124
Mirko Bonadei8cc66952020-10-30 09:13:45125 def ChangedContents(self):
126 return self._changed_contents
Artem Titove92675b2018-05-22 08:21:27127
Mirko Bonadei8cc66952020-10-30 09:13:45128 def NewContents(self):
129 return self._new_contents
Mirko Bonadeia730c1c2017-09-18 09:33:13130
Mirko Bonadei8cc66952020-10-30 09:13:45131 def LocalPath(self):
132 return self._local_path
Mirko Bonadei4dc4e252017-09-19 11:49:16133
Mirko Bonadei8cc66952020-10-30 09:13:45134 def AbsoluteLocalPath(self):
135 return self._local_path
Artem Titove92675b2018-05-22 08:21:27136
Mirko Bonadei8cc66952020-10-30 09:13:45137 def OldContents(self):
138 return self._old_contents