[go: nahoru, domu]

blob: 5caa156593415fca998701940c2f03add77e52cf [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
15 This class can be used for unittests for presubmit by initializing the files
16 attribute as the list of changed files.
17 """
18
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
gayanee1702662014-12-13 03:48:0927 self.change = MockChange([])
gayane3dff8c22014-12-04 17:09:5128
29 def AffectedFiles(self, file_filter=None):
30 return self.files
31
glidere61efad2015-02-18 17:39:4332 def AffectedSourceFiles(self, file_filter=None):
33 return self.files
34
gayane3dff8c22014-12-04 17:09:5135 def PresubmitLocalPath(self):
36 return os.path.dirname(__file__)
37
38 def ReadFile(self, filename, mode='rU'):
glidere61efad2015-02-18 17:39:4339 if hasattr(filename, 'AbsoluteLocalPath'):
40 filename = filename.AbsoluteLocalPath()
gayane3dff8c22014-12-04 17:09:5141 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
46
47
48class MockOutputApi(object):
gayane860db5c2014-12-05 16:16:4649 """Mock class for the OutputApi class.
gayane3dff8c22014-12-04 17:09:5150
51 An instance of this class can be passed to presubmit unittests for outputing
52 various types of results.
53 """
54
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
60
61 class PresubmitError(PresubmitResult):
62 def __init__(self, message, items, long_text=''):
63 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
64 self.type = 'error'
65
66 class PresubmitPromptWarning(PresubmitResult):
67 def __init__(self, message, items, long_text=''):
68 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
69 self.type = 'warning'
70
71 class PresubmitNotifyResult(PresubmitResult):
72 def __init__(self, message, items, long_text=''):
73 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
74 self.type = 'notify'
75
76 class PresubmitPromptOrNotify(PresubmitResult):
77 def __init__(self, message, items, long_text=''):
78 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
79 self.type = 'promptOrNotify'
80
81
82class MockFile(object):
83 """Mock class for the File class.
84
85 This class can be used to form the mock list of changed files in
86 MockInputApi for presubmit unittests.
87 """
88
89 def __init__(self, local_path, new_contents):
90 self._local_path = local_path
91 self._new_contents = new_contents
92 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
93
94 def ChangedContents(self):
95 return self._changed_contents
96
97 def NewContents(self):
98 return self._new_contents
99
100 def LocalPath(self):
101 return self._local_path
102
103
glidere61efad2015-02-18 17:39:43104class MockAffectedFile(MockFile):
105 def AbsoluteLocalPath(self):
106 return self._local_path
107
108
gayane3dff8c22014-12-04 17:09:51109class MockChange(object):
110 """Mock class for Change class.
111
112 This class can be used in presubmit unittests to mock the query of the
113 current change.
114 """
115
116 def __init__(self, changed_files):
117 self._changed_files = changed_files
118
119 def LocalPaths(self):
120 return self._changed_files