blob: c15d245110a3060de4732b4e09e8c1f3d3290d3a [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
27
28def AffectedFiles(self, file_filter=None):
29return self.files
30
31def PresubmitLocalPath(self):
32return os.path.dirname(__file__)
33
34def ReadFile(self, filename, mode='rU'):
35for file_ in self.files:
36if file_.LocalPath() == filename:
37return '\n'.join(file_.NewContents())
38# Otherwise, file is not in our mock API.
39raise IOError, "No such file or directory: '%s'" % filename
40
41
42class MockOutputApi(object):
43""Mock class for the OutputApi class.
44
45An instance of this class can be passed to presubmit unittests for outputing
46various types of results.
47"""
48
49class PresubmitResult(object):
50def __init__(self, message, items=None, long_text=''):
51self.message = message
52self.items = items
53self.long_text = long_text
54
55class PresubmitError(PresubmitResult):
56def __init__(self, message, items, long_text=''):
57MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
58self.type = 'error'
59
60class PresubmitPromptWarning(PresubmitResult):
61def __init__(self, message, items, long_text=''):
62MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
63self.type = 'warning'
64
65class PresubmitNotifyResult(PresubmitResult):
66def __init__(self, message, items, long_text=''):
67MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
68self.type = 'notify'
69
70class PresubmitPromptOrNotify(PresubmitResult):
71def __init__(self, message, items, long_text=''):
72MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
73self.type = 'promptOrNotify'
74
75
76class MockFile(object):
77"""Mock class for the File class.
78
79This class can be used to form the mock list of changed files in
80MockInputApi for presubmit unittests.
81"""
82
83def __init__(self, local_path, new_contents):
84self._local_path = local_path
85self._new_contents = new_contents
86self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
87
88def ChangedContents(self):
89return self._changed_contents
90
91def NewContents(self):
92return self._new_contents
93
94def LocalPath(self):
95return self._local_path
96
97
98class MockChange(object):
99"""Mock class for Change class.
100
101This class can be used in presubmit unittests to mock the query of the
102current change.
103"""
104
105def __init__(self, changed_files):
106self._changed_files = changed_files
107
108def LocalPaths(self):
109return self._changed_files