blob: b15eb74dd88b14dacb2e9d1d318fa3defbee1055 [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 Bonadeia730c1c2017-09-18 11:33:13 +020027
Mirko Bonadei8cc66952020-10-30 10:13:45 +010028 def AffectedSourceFiles(self, file_filter=None):
29 return self.AffectedFiles(file_filter=file_filter)
Artem Titove92675b2018-05-22 10:21:27 +020030
Mirko Bonadei8cc66952020-10-30 10:13:45 +010031 def AffectedFiles(self, file_filter=None, include_deletes=False):
32 # pylint: disable=unused-argument
33 return self.files
charujain9893e252017-09-14 13:33:22 +020034
Mirko Bonadei8cc66952020-10-30 10:13:45 +010035 @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 10:21:27 +020042
Mirko Bonadei8cc66952020-10-30 10:13:45 +010043 def PresubmitLocalPath(self):
44 return self.presubmit_local_path
Artem Titove92675b2018-05-22 10:21:27 +020045
Mirko Bonadei8cc66952020-10-30 10:13:45 +010046 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 13:49:16 +020054
charujain9893e252017-09-14 13:33:22 +020055
56class MockOutputApi(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010057 """Mock class for the OutputApi class.
charujain9893e252017-09-14 13:33:22 +020058
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 10:13:45 +010063 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 13:33:22 +020068
Mirko Bonadei8cc66952020-10-30 10:13:45 +010069 def __repr__(self):
70 return self.message
charujain9893e252017-09-14 13:33:22 +020071
Mirko Bonadei8cc66952020-10-30 10:13:45 +010072 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 13:33:22 +020077
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020078
charujain9893e252017-09-14 13:33:22 +020079class MockChange(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010080 """Mock class for Change class.
charujain9893e252017-09-14 13:33:22 +020081
82 This class can be used in presubmit unittests to mock the query of the
83 current change.
84 """
85
Mirko Bonadei8cc66952020-10-30 10:13:45 +010086 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 15:12:35 +020090
Mirko Bonadei8cc66952020-10-30 10:13:45 +010091 def BugsFromDescription(self):
92 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020093
Mirko Bonadei8cc66952020-10-30 10:13:45 +010094 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 10:21:27 +020099
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200100
101class MockFile(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102 """Mock class for the File class.
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200103
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 10:13:45 +0100108 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 10:21:27 +0200121
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100122 def Action(self):
123 return self._action
Artem Titove92675b2018-05-22 10:21:27 +0200124
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100125 def ChangedContents(self):
126 return self._changed_contents
Artem Titove92675b2018-05-22 10:21:27 +0200127
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100128 def NewContents(self):
129 return self._new_contents
Mirko Bonadeia730c1c2017-09-18 11:33:13 +0200130
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100131 def LocalPath(self):
132 return self._local_path
Mirko Bonadei4dc4e252017-09-19 13:49:16 +0200133
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100134 def AbsoluteLocalPath(self):
135 return self._local_path
Artem Titove92675b2018-05-22 10:21:27 +0200136
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100137 def OldContents(self):
138 return self._old_contents