blob: 7d286b9daa13260d70db6b1b43b624067c656381 [file] [log] [blame]
gayane3dff8c22014-12-04 17:09:511# Copyright 2014 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 json
6import os
7import re
8import subprocess
9import sys
10
11
12class MockInputApi(object):
13"""Mock class for the InputApi class.
14
15This class can be used for unittests for presubmit by initializing the files
16attribute as the list of changed files.
17"""
18
19def __init__(self):
20self.json = json
21self.re = re
22self.os_path = os.path
23self.python_executable = sys.executable
24self.subprocess = subprocess
25self.files = []
26self.is_committing = False
gayanee1702662014-12-13 03:48:0927self.change = MockChange([])
gayane3dff8c22014-12-04 17:09:5128
29def AffectedFiles(self, file_filter=None):
30return self.files
31
32def PresubmitLocalPath(self):
33return os.path.dirname(__file__)
34
35def ReadFile(self, filename, mode='rU'):
36for file_ in self.files:
37if file_.LocalPath() == filename:
38return '\n'.join(file_.NewContents())
39# Otherwise, file is not in our mock API.
40raise IOError, "No such file or directory: '%s'" % filename
41
42
43class MockOutputApi(object):
gayane860db5c32014-12-05 16:16:4644"""Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5145
46An instance of this class can be passed to presubmit unittests for outputing
47various types of results.
48"""
49
50class PresubmitResult(object):
51def __init__(self, message, items=None, long_text=''):
52self.message = message
53self.items = items
54self.long_text = long_text
55
56class PresubmitError(PresubmitResult):
57def __init__(self, message, items, long_text=''):
58MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
59self.type = 'error'
60
61class PresubmitPromptWarning(PresubmitResult):
62def __init__(self, message, items, long_text=''):
63MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
64self.type = 'warning'
65
66class PresubmitNotifyResult(PresubmitResult):
67def __init__(self, message, items, long_text=''):
68MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
69self.type = 'notify'
70
71class PresubmitPromptOrNotify(PresubmitResult):
72def __init__(self, message, items, long_text=''):
73MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
74self.type = 'promptOrNotify'
75
76
77class MockFile(object):
78"""Mock class for the File class.
79
80This class can be used to form the mock list of changed files in
81MockInputApi for presubmit unittests.
82"""
83
84def __init__(self, local_path, new_contents):
85self._local_path = local_path
86self._new_contents = new_contents
87self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
88
89def ChangedContents(self):
90return self._changed_contents
91
92def NewContents(self):
93return self._new_contents
94
95def LocalPath(self):
96return self._local_path
97
98
99class MockChange(object):
100"""Mock class for Change class.
101
102This class can be used in presubmit unittests to mock the query of the
103current change.
104"""
105
106def __init__(self, changed_files):
107self._changed_files = changed_files
108
109def LocalPaths(self):
110return self._changed_files