blob: 4b2b3c752289f2dfb1312dc8cfece081e8e77201 [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
charujain9893e252017-09-14 13:33:22 +020012
13class MockInputApi(object):
14 """Mock class for the InputApi class.
15
16 This class can be used for unittests for presubmit by initializing the files
17 attribute as the list of changed files.
18 """
19
20 def __init__(self):
Mirko Bonadei61880182017-10-12 15:12:35 +020021 self.change = MockChange([], [])
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020022 self.files = []
23
24 def AffectedSourceFiles(self, file_filter=None):
25 # pylint: disable=unused-argument
26 return self.files
charujain9893e252017-09-14 13:33:22 +020027
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020028 def ReadFile(self, affected_file, mode='rU'):
29 filename = affected_file.AbsoluteLocalPath()
30 for f in self.files:
31 if f.LocalPath() == filename:
32 with open(filename, mode) as f:
33 return f.read()
34 # Otherwise, file is not in our mock API.
35 raise IOError, "No such file or directory: '%s'" % filename
36
charujain9893e252017-09-14 13:33:22 +020037
38class MockOutputApi(object):
39 """Mock class for the OutputApi class.
40
41 An instance of this class can be passed to presubmit unittests for outputing
42 various types of results.
43 """
44
45 class PresubmitResult(object):
46 def __init__(self, message, items=None, long_text=''):
47 self.message = message
48 self.items = items
49 self.long_text = long_text
50
51 def __repr__(self):
52 return self.message
53
54 class PresubmitError(PresubmitResult):
55 def __init__(self, message, items=None, long_text=''):
56 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
57 self.type = 'error'
58
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020059
charujain9893e252017-09-14 13:33:22 +020060class MockChange(object):
61 """Mock class for Change class.
62
63 This class can be used in presubmit unittests to mock the query of the
64 current change.
65 """
66
Mirko Bonadei61880182017-10-12 15:12:35 +020067 def __init__(self, changed_files, bugs_from_description):
charujain9893e252017-09-14 13:33:22 +020068 self._changed_files = changed_files
Mirko Bonadei61880182017-10-12 15:12:35 +020069 self._bugs_from_description = bugs_from_description
70
71 def BugsFromDescription(self):
72 return self._bugs_from_description
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020073
74
75class MockFile(object):
76 """Mock class for the File class.
77
78 This class can be used to form the mock list of changed files in
79 MockInputApi for presubmit unittests.
80 """
81
82 def __init__(self, local_path):
83 self._local_path = local_path
84
85 def LocalPath(self):
86 return self._local_path
Mirko Bonadei4dc4e252017-09-19 13:49:16 +020087
88 def AbsoluteLocalPath(self):
89 return self._local_path