All guestview sizing now utilizes the new sizing infrastructure from https://coderevi...
[chromium-blink-merge.git] / PRESUBMIT_test_mocks.py
blob03d9232cfa11c9317d630a01eb59e87b8b7cef3a
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
27 self.change = MockChange([])
29 def AffectedFiles(self, file_filter=None):
30 return self.files
32 def AffectedSourceFiles(self, file_filter=None):
33 return self.files
35 def PresubmitLocalPath(self):
36 return os.path.dirname(__file__)
38 def ReadFile(self, filename, mode='rU'):
39 if hasattr(filename, 'AbsoluteLocalPath'):
40 filename = filename.AbsoluteLocalPath()
41 for file_ in self.files:
42 if file_.LocalPath() == filename:
43 return '\n'.join(file_.NewContents())
44 # Otherwise, file is not in our mock API.
45 raise IOError, "No such file or directory: '%s'" % filename
48 class MockOutputApi(object):
49 """Mock class for the OutputApi class.
51 An instance of this class can be passed to presubmit unittests for outputing
52 various types of results.
53 """
55 class PresubmitResult(object):
56 def __init__(self, message, items=None, long_text=''):
57 self.message = message
58 self.items = items
59 self.long_text = long_text
61 def __repr__(self):
62 return self.message
64 class PresubmitError(PresubmitResult):
65 def __init__(self, message, items, long_text=''):
66 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
67 self.type = 'error'
69 class PresubmitPromptWarning(PresubmitResult):
70 def __init__(self, message, items, long_text=''):
71 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
72 self.type = 'warning'
74 class PresubmitNotifyResult(PresubmitResult):
75 def __init__(self, message, items, long_text=''):
76 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
77 self.type = 'notify'
79 class PresubmitPromptOrNotify(PresubmitResult):
80 def __init__(self, message, items, long_text=''):
81 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
82 self.type = 'promptOrNotify'
85 class MockFile(object):
86 """Mock class for the File class.
88 This class can be used to form the mock list of changed files in
89 MockInputApi for presubmit unittests.
90 """
92 def __init__(self, local_path, new_contents):
93 self._local_path = local_path
94 self._new_contents = new_contents
95 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
97 def ChangedContents(self):
98 return self._changed_contents
100 def NewContents(self):
101 return self._new_contents
103 def LocalPath(self):
104 return self._local_path
107 class MockAffectedFile(MockFile):
108 def AbsoluteLocalPath(self):
109 return self._local_path
112 class MockChange(object):
113 """Mock class for Change class.
115 This class can be used in presubmit unittests to mock the query of the
116 current change.
119 def __init__(self, changed_files):
120 self._changed_files = changed_files
122 def LocalPaths(self):
123 return self._changed_files