blob: f7ead25fab812e915d2f45d7b90f1b66689f9615 [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):
21 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
28
29class MockOutputApi(object):
30 """Mock class for the OutputApi class.
31
32 An instance of this class can be passed to presubmit unittests for outputing
33 various types of results.
34 """
35
36 class PresubmitResult(object):
37 def __init__(self, message, items=None, long_text=''):
38 self.message = message
39 self.items = items
40 self.long_text = long_text
41
42 def __repr__(self):
43 return self.message
44
45 class PresubmitError(PresubmitResult):
46 def __init__(self, message, items=None, long_text=''):
47 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
48 self.type = 'error'
49
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020050
charujain9893e252017-09-14 13:33:22 +020051class MockChange(object):
52 """Mock class for Change class.
53
54 This class can be used in presubmit unittests to mock the query of the
55 current change.
56 """
57
58 def __init__(self, changed_files):
59 self._changed_files = changed_files
Mirko Bonadeia730c1c2017-09-18 11:33:13 +020060
61
62class MockFile(object):
63 """Mock class for the File class.
64
65 This class can be used to form the mock list of changed files in
66 MockInputApi for presubmit unittests.
67 """
68
69 def __init__(self, local_path):
70 self._local_path = local_path
71
72 def LocalPath(self):
73 return self._local_path