blob: 015f6e7e151bd243ce468136b587e402930fbbeb [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 08:01:121#!/usr/bin/env vpython3
2
charujain9893e252017-09-14 11:33:223# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
Mirko Bonadeia730c1c2017-09-18 09:33:1311# This file is inspired to [1].
12# [1] - https://cs.chromium.org/chromium/src/PRESUBMIT_test_mocks.py
13
Christoffer Jansson4e8a7732022-02-08 08:01:1214from __future__ import absolute_import
Artem Titove92675b2018-05-22 08:21:2715import os.path
16import re
17
charujain9893e252017-09-14 11:33:2218
Christoffer Jansson4e8a7732022-02-08 08:01:1219class MockInputApi:
20 """Mock class for the InputApi class.
charujain9893e252017-09-14 11:33:2221
22 This class can be used for unittests for presubmit by initializing the files
23 attribute as the list of changed files.
24 """
25
Christoffer Jansson4e8a7732022-02-08 08:01:1226 def __init__(self):
27 self.change = MockChange([], [])
28 self.files = []
29 self.presubmit_local_path = os.path.dirname(__file__)
30 self.re = re # pylint: disable=invalid-name
Mirko Bonadeia730c1c2017-09-18 09:33:1331
Christoffer Jansson4e8a7732022-02-08 08:01:1232 def AffectedSourceFiles(self, file_filter=None):
33 return self.AffectedFiles(file_filter=file_filter)
Artem Titove92675b2018-05-22 08:21:2734
Christoffer Jansson4e8a7732022-02-08 08:01:1235 def AffectedFiles(self, file_filter=None, include_deletes=False):
36 for f in self.files:
37 if file_filter and not file_filter(f):
38 continue
39 if not include_deletes and f.Action() == 'D':
40 continue
41 yield f
charujain9893e252017-09-14 11:33:2242
Christoffer Jansson4e8a7732022-02-08 08:01:1243 @classmethod
44 def FilterSourceFile(cls, affected_file, files_to_check=(), files_to_skip=()):
45 # pylint: disable=unused-argument
46 return True
Artem Titove92675b2018-05-22 08:21:2747
Christoffer Jansson4e8a7732022-02-08 08:01:1248 def PresubmitLocalPath(self):
49 return self.presubmit_local_path
Artem Titove92675b2018-05-22 08:21:2750
Christoffer Jansson4e8a7732022-02-08 08:01:1251 def ReadFile(self, affected_file, mode='r'):
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 11:49:1659
charujain9893e252017-09-14 11:33:2260
Christoffer Jansson4e8a7732022-02-08 08:01:1261class MockOutputApi:
62 """Mock class for the OutputApi class.
charujain9893e252017-09-14 11:33:2263
64 An instance of this class can be passed to presubmit unittests for outputing
65 various types of results.
66 """
67
Christoffer Jansson4e8a7732022-02-08 08:01:1268 class PresubmitResult:
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 11:33:2273
Christoffer Jansson4e8a7732022-02-08 08:01:1274 def __repr__(self):
75 return self.message
charujain9893e252017-09-14 11:33:2276
Christoffer Jansson4e8a7732022-02-08 08:01:1277 class PresubmitError(PresubmitResult):
78 def __init__(self, message, items=None, long_text=''):
79 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
80 self.type = 'error'
charujain9893e252017-09-14 11:33:2281
Mirko Bonadeia730c1c2017-09-18 09:33:1382
Christoffer Jansson4e8a7732022-02-08 08:01:1283class MockChange:
84 """Mock class for Change class.
charujain9893e252017-09-14 11:33:2285
86 This class can be used in presubmit unittests to mock the query of the
87 current change.
88 """
89
Christoffer Jansson4e8a7732022-02-08 08:01:1290 def __init__(self, changed_files, bugs_from_description, tags=None):
91 self._changed_files = changed_files
92 self._bugs_from_description = bugs_from_description
93 self.tags = dict() if not tags else tags
Mirko Bonadei61880182017-10-12 13:12:3594
Christoffer Jansson4e8a7732022-02-08 08:01:1295 def BugsFromDescription(self):
96 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 09:33:1397
Christoffer Jansson4e8a7732022-02-08 08:01:1298 def __getattr__(self, attr):
99 """Return tags directly as attributes on the object."""
100 if not re.match(r"^[A-Z_]*$", attr):
101 raise AttributeError(self, attr)
102 return self.tags.get(attr)
Artem Titove92675b2018-05-22 08:21:27103
Mirko Bonadeia730c1c2017-09-18 09:33:13104
Christoffer Jansson4e8a7732022-02-08 08:01:12105class MockFile:
106 """Mock class for the File class.
Mirko Bonadeia730c1c2017-09-18 09:33:13107
108 This class can be used to form the mock list of changed files in
109 MockInputApi for presubmit unittests.
110 """
111
Christoffer Jansson4e8a7732022-02-08 08:01:12112 def __init__(self,
113 local_path,
114 new_contents=None,
115 old_contents=None,
116 action='A'):
117 if new_contents is None:
118 new_contents = ["Data"]
119 self._local_path = local_path
120 self._new_contents = new_contents
121 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
122 self._action = action
123 self._old_contents = old_contents
Artem Titove92675b2018-05-22 08:21:27124
Christoffer Jansson4e8a7732022-02-08 08:01:12125 def Action(self):
126 return self._action
Artem Titove92675b2018-05-22 08:21:27127
Christoffer Jansson4e8a7732022-02-08 08:01:12128 def ChangedContents(self):
129 return self._changed_contents
Artem Titove92675b2018-05-22 08:21:27130
Christoffer Jansson4e8a7732022-02-08 08:01:12131 def NewContents(self):
132 return self._new_contents
Mirko Bonadeia730c1c2017-09-18 09:33:13133
Christoffer Jansson4e8a7732022-02-08 08:01:12134 def LocalPath(self):
135 return self._local_path
Mirko Bonadei4dc4e252017-09-19 11:49:16136
Christoffer Jansson4e8a7732022-02-08 08:01:12137 def AbsoluteLocalPath(self):
138 return self._local_path
Artem Titove92675b2018-05-22 08:21:27139
Christoffer Jansson4e8a7732022-02-08 08:01:12140 def OldContents(self):
141 return self._old_contents