blob: 4ed7947530571d59878bf381d2e752b15b02afe0 [file] [log] [blame]
charujain9893e252017-09-14 13:33:22 +02001# 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 11:33:13 +02009# This file is inspired to [1].
10# [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py
11
Artem Titove92675b2018-05-22 10:21:27 +020012import os.path
13import re
14
charujain9893e252017-09-14 13:33:22 +020015
16class MockInputApi(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010017 """Mock class for the InputApi class.
charujain9893e252017-09-14 13:33:22 +020018
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 10:13:45 +010023 def __init__(self):
24 self.change = MockChange([], [])
25 self.files = []
26 self.presubmit_local_path = os.path.dirname(__file__)
Mirko Bonadeia6395132021-07-22 17:35:59 +020027 self.re = re # pylint: disable=invalid-name
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020028
Mirko Bonadei8cc66952020-10-30 10:13:45 +010029 def AffectedSourceFiles(self, file_filter=None):
30 return self.AffectedFiles(file_filter=file_filter)
Artem Titove92675b2018-05-22 10:21:27 +020031
Mirko Bonadei8cc66952020-10-30 10:13:45 +010032 def AffectedFiles(self, file_filter=None, include_deletes=False):
Mirko Bonadeia6395132021-07-22 17:35:59 +020033 for f in self.files:
34 if file_filter and not file_filter(f):
35 continue
36 if not include_deletes and f.Action() == 'D':
37 continue
38 yield f
charujain9893e252017-09-14 13:33:22 +020039
Mirko Bonadei8cc66952020-10-30 10:13:45 +010040 @classmethod
41 def FilterSourceFile(cls,
42 affected_file,
43 files_to_check=(),
44 files_to_skip=()):
45 # pylint: disable=unused-argument
46 return True
Artem Titove92675b2018-05-22 10:21:27 +020047
Mirko Bonadei8cc66952020-10-30 10:13:45 +010048 def PresubmitLocalPath(self):
49 return self.presubmit_local_path
Artem Titove92675b2018-05-22 10:21:27 +020050
Mirko Bonadei8cc66952020-10-30 10:13:45 +010051 def ReadFile(self, affected_file, mode='rU'):
52 filename = affected_file.AbsoluteLocalPath()
53 for f in self.files:
54 if f.LocalPath() == filename:
55 with open(filename, mode) as f:
56 return f.read()
57 # Otherwise, file is not in our mock API.
58 raise IOError, "No such file or directory: '%s'" % filename
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020059
charujain9893e252017-09-14 13:33:22 +020060
61class MockOutputApi(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010062 """Mock class for the OutputApi class.
charujain9893e252017-09-14 13:33:22 +020063
64 An instance of this class can be passed to presubmit unittests for outputing
65 various types of results.
66 """
67
Mirko Bonadei8cc66952020-10-30 10:13:45 +010068 class PresubmitResult(object):
69 def __init__(self, message, items=None, long_text=''):
70 self.message = message
71 self.items = items
72 self.long_text = long_text
charujain9893e252017-09-14 13:33:22 +020073
Mirko Bonadei8cc66952020-10-30 10:13:45 +010074 def __repr__(self):
75 return self.message
charujain9893e252017-09-14 13:33:22 +020076
Mirko Bonadei8cc66952020-10-30 10:13:45 +010077 class PresubmitError(PresubmitResult):
78 def __init__(self, message, items=None, long_text=''):
79 MockOutputApi.PresubmitResult.__init__(self, message, items,
80 long_text)
81 self.type = 'error'
charujain9893e252017-09-14 13:33:22 +020082
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020083
charujain9893e252017-09-14 13:33:22 +020084class MockChange(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010085 """Mock class for Change class.
charujain9893e252017-09-14 13:33:22 +020086
87 This class can be used in presubmit unittests to mock the query of the
88 current change.
89 """
90
Mirko Bonadei8cc66952020-10-30 10:13:45 +010091 def __init__(self, changed_files, bugs_from_description, tags=None):
92 self._changed_files = changed_files
93 self._bugs_from_description = bugs_from_description
94 self.tags = dict() if not tags else tags
Mirko Bonadei61880182017-10-12 15:12:35 +020095
Mirko Bonadei8cc66952020-10-30 10:13:45 +010096 def BugsFromDescription(self):
97 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020098
Mirko Bonadei8cc66952020-10-30 10:13:45 +010099 def __getattr__(self, attr):
100 """Return tags directly as attributes on the object."""
101 if not re.match(r"^[A-Z_]*$", attr):
102 raise AttributeError(self, attr)
103 return self.tags.get(attr)
Artem Titove92675b2018-05-22 10:21:27 +0200104
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200105
106class MockFile(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100107 """Mock class for the File class.
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200108
109 This class can be used to form the mock list of changed files in
110 MockInputApi for presubmit unittests.
111 """
112
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100113 def __init__(self,
114 local_path,
115 new_contents=None,
116 old_contents=None,
117 action='A'):
118 if new_contents is None:
119 new_contents = ["Data"]
120 self._local_path = local_path
121 self._new_contents = new_contents
122 self._changed_contents = [(i + 1, l)
123 for i, l in enumerate(new_contents)]
124 self._action = action
125 self._old_contents = old_contents
Artem Titove92675b2018-05-22 10:21:27 +0200126
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100127 def Action(self):
128 return self._action
Artem Titove92675b2018-05-22 10:21:27 +0200129
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100130 def ChangedContents(self):
131 return self._changed_contents
Artem Titove92675b2018-05-22 10:21:27 +0200132
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100133 def NewContents(self):
134 return self._new_contents
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200135
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100136 def LocalPath(self):
137 return self._local_path
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200138
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100139 def AbsoluteLocalPath(self):
140 return self._local_path
Artem Titove92675b2018-05-22 10:21:27 +0200141
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100142 def OldContents(self):
143 return self._old_contents