Implement HasPermission() method in PermissionService.
[chromium-blink-merge.git] / PRESUBMIT_test_mocks.py
blobf99671dafda83d34461878b19598d62c1d913822
1 # 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.
5 import json
6 import os
7 import re
8 import subprocess
9 import sys
12 class MockInputApi(object):
13 """Mock class for the InputApi class.
15 This class can be used for unittests for presubmit by initializing the files
16 attribute as the list of changed files.
17 """
19 def __init__(self):
20 self.json = json
21 self.re = re
22 self.os_path = os.path
23 self.python_executable = sys.executable
24 self.subprocess = subprocess
25 self.files = []
26 self.is_committing = False
28 def AffectedFiles(self, file_filter=None):
29 return self.files
31 def PresubmitLocalPath(self):
32 return os.path.dirname(__file__)
34 def ReadFile(self, filename, mode='rU'):
35 for file_ in self.files:
36 if file_.LocalPath() == filename:
37 return '\n'.join(file_.NewContents())
38 # Otherwise, file is not in our mock API.
39 raise IOError, "No such file or directory: '%s'" % filename
42 class MockOutputApi(object):
43 """Mock class for the OutputApi class.
45 An instance of this class can be passed to presubmit unittests for outputing
46 various types of results.
47 """
49 class PresubmitResult(object):
50 def __init__(self, message, items=None, long_text=''):
51 self.message = message
52 self.items = items
53 self.long_text = long_text
55 class PresubmitError(PresubmitResult):
56 def __init__(self, message, items, long_text=''):
57 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
58 self.type = 'error'
60 class PresubmitPromptWarning(PresubmitResult):
61 def __init__(self, message, items, long_text=''):
62 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
63 self.type = 'warning'
65 class PresubmitNotifyResult(PresubmitResult):
66 def __init__(self, message, items, long_text=''):
67 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
68 self.type = 'notify'
70 class PresubmitPromptOrNotify(PresubmitResult):
71 def __init__(self, message, items, long_text=''):
72 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
73 self.type = 'promptOrNotify'
76 class MockFile(object):
77 """Mock class for the File class.
79 This class can be used to form the mock list of changed files in
80 MockInputApi for presubmit unittests.
81 """
83 def __init__(self, local_path, new_contents):
84 self._local_path = local_path
85 self._new_contents = new_contents
86 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
88 def ChangedContents(self):
89 return self._changed_contents
91 def NewContents(self):
92 return self._new_contents
94 def LocalPath(self):
95 return self._local_path
98 class MockChange(object):
99 """Mock class for Change class.
101 This class can be used in presubmit unittests to mock the query of the
102 current change.
105 def __init__(self, changed_files):
106 self._changed_files = changed_files
108 def LocalPaths(self):
109 return self._changed_files