blob: f6933e4bc8ddc765cc1501f6f949f21e0a8af468 [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):
17 """Mock class for the InputApi class.
18
19 This class can be used for unittests for presubmit by initializing the files
20 attribute as the list of changed files.
21 """
22
23 def __init__(self):
Mirko Bonadei61880182017-10-12 15:12:35 +020024 self.change = MockChange([], [])
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020025 self.files = []
Artem Titove92675b2018-05-22 10:21:27 +020026 self.presubmit_local_path = os.path.dirname(__file__)
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020027
28 def AffectedSourceFiles(self, file_filter=None):
Artem Titove92675b2018-05-22 10:21:27 +020029 return self.AffectedFiles(file_filter=file_filter)
30
31 def AffectedFiles(self, file_filter=None, include_deletes=False):
Artem Titovb5750eb2018-05-18 12:46:18 +000032 # pylint: disable=unused-argument
33 return self.files
charujain9893e252017-09-14 13:33:22 +020034
Artem Titove92675b2018-05-22 10:21:27 +020035 @classmethod
Mirko Bonadei6c1278a2020-07-22 10:33:44 +020036 def FilterSourceFile(cls, affected_file, files_to_check=(),
37 files_to_skip=()):
Artem Titove92675b2018-05-22 10:21:27 +020038 # pylint: disable=unused-argument
39 return True
40
41 def PresubmitLocalPath(self):
42 return self.presubmit_local_path
43
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020044 def ReadFile(self, affected_file, mode='rU'):
45 filename = affected_file.AbsoluteLocalPath()
46 for f in self.files:
47 if f.LocalPath() == filename:
48 with open(filename, mode) as f:
49 return f.read()
50 # Otherwise, file is not in our mock API.
51 raise IOError, "No such file or directory: '%s'" % filename
52
charujain9893e252017-09-14 13:33:22 +020053
54class MockOutputApi(object):
55 """Mock class for the OutputApi class.
56
57 An instance of this class can be passed to presubmit unittests for outputing
58 various types of results.
59 """
60
61 class PresubmitResult(object):
62 def __init__(self, message, items=None, long_text=''):
63 self.message = message
64 self.items = items
65 self.long_text = long_text
66
67 def __repr__(self):
68 return self.message
69
70 class PresubmitError(PresubmitResult):
71 def __init__(self, message, items=None, long_text=''):
72 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
73 self.type = 'error'
74
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020075
charujain9893e252017-09-14 13:33:22 +020076class MockChange(object):
77 """Mock class for Change class.
78
79 This class can be used in presubmit unittests to mock the query of the
80 current change.
81 """
82
Artem Titove92675b2018-05-22 10:21:27 +020083 def __init__(self, changed_files, bugs_from_description, tags=None):
charujain9893e252017-09-14 13:33:22 +020084 self._changed_files = changed_files
Mirko Bonadei61880182017-10-12 15:12:35 +020085 self._bugs_from_description = bugs_from_description
Artem Titove92675b2018-05-22 10:21:27 +020086 self.tags = dict() if not tags else tags
Mirko Bonadei61880182017-10-12 15:12:35 +020087
88 def BugsFromDescription(self):
89 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020090
Artem Titove92675b2018-05-22 10:21:27 +020091 def __getattr__(self, attr):
92 """Return tags directly as attributes on the object."""
93 if not re.match(r"^[A-Z_]*$", attr):
94 raise AttributeError(self, attr)
95 return self.tags.get(attr)
96
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020097
98class MockFile(object):
99 """Mock class for the File class.
100
101 This class can be used to form the mock list of changed files in
102 MockInputApi for presubmit unittests.
103 """
104
Artem Titove92675b2018-05-22 10:21:27 +0200105 def __init__(self, local_path, new_contents=None, old_contents=None,
106 action='A'):
107 if new_contents is None:
108 new_contents = ["Data"]
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200109 self._local_path = local_path
Artem Titove92675b2018-05-22 10:21:27 +0200110 self._new_contents = new_contents
111 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
112 self._action = action
113 self._old_contents = old_contents
114
115 def Action(self):
116 return self._action
117
118 def ChangedContents(self):
119 return self._changed_contents
120
121 def NewContents(self):
122 return self._new_contents
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200123
124 def LocalPath(self):
125 return self._local_path
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200126
127 def AbsoluteLocalPath(self):
128 return self._local_path
Artem Titove92675b2018-05-22 10:21:27 +0200129
130 def OldContents(self):
131 return self._old_contents