blob: ce9fcb0f7c26df0767669d507bf37b51101b0d82 [file] [log] [blame]
Hui Yingst2aa0a4a2020-04-09 19:04:21 +00001# Copyright 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import re
6
7
8class MockInputApi(object):
9 """Mock class for the InputApi class.
10
11 This class can be used for unittests for presubmit by initializing the files
12 attribute as the list of changed files.
13 """
14
15 def __init__(self):
16 self.files = []
17 self.re = re
18
19 def AffectedFiles(self, file_filter=None, include_deletes=False):
20 # pylint: disable=unused-argument
21 return self.files
22
23
24class MockOutputApi(object):
25 """Mock class for the OutputApi class.
26
27 An instance of this class can be passed to presubmit unittests for outputting
28 various types of results.
29 """
30
31 class PresubmitResult(object):
32
33 def __init__(self, message, items=None, long_text=''):
34 self.message = message
35 self.items = items
36 self.long_text = long_text
37
38 def __repr__(self):
39 return self.message
40
41 class PresubmitError(PresubmitResult):
42
43 def __init__(self, message, items=None, long_text=''):
44 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
45 self.type = 'error'
46
47
48class MockFile(object):
49 """Mock class for the File class.
50
51 This class can be used to form the mock list of changed files in
52 MockInputApi for presubmit unittests.
53 """
54
55 def __init__(self,
56 local_path,
57 new_contents=None,
58 old_contents=None,
59 action='A'):
60 self._local_path = local_path
61 if new_contents is None:
62 new_contents = []
63 self._new_contents = new_contents
64 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
65 self._action = action
66 self._old_contents = old_contents
67
68 def LocalPath(self):
69 return self._local_path