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.
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.
22 self
.os_path
= os
.path
23 self
.python_executable
= sys
.executable
24 self
.subprocess
= subprocess
26 self
.is_committing
= False
27 self
.change
= MockChange([])
29 def AffectedFiles(self
, file_filter
=None):
32 def AffectedSourceFiles(self
, file_filter
=None):
38 def PresubmitLocalPath(self
):
39 return os
.path
.dirname(__file__
)
41 def ReadFile(self
, filename
, mode
='rU'):
42 if hasattr(filename
, 'AbsoluteLocalPath'):
43 filename
= filename
.AbsoluteLocalPath()
44 for file_
in self
.files
:
45 if file_
.LocalPath() == filename
:
46 return '\n'.join(file_
.NewContents())
47 # Otherwise, file is not in our mock API.
48 raise IOError, "No such file or directory: '%s'" % filename
51 class MockOutputApi(object):
52 """Mock class for the OutputApi class.
54 An instance of this class can be passed to presubmit unittests for outputing
55 various types of results.
58 class PresubmitResult(object):
59 def __init__(self
, message
, items
=None, long_text
=''):
60 self
.message
= message
62 self
.long_text
= long_text
67 class PresubmitError(PresubmitResult
):
68 def __init__(self
, message
, items
=None, long_text
=''):
69 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
72 class PresubmitPromptWarning(PresubmitResult
):
73 def __init__(self
, message
, items
=None, long_text
=''):
74 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
77 class PresubmitNotifyResult(PresubmitResult
):
78 def __init__(self
, message
, items
=None, long_text
=''):
79 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
82 class PresubmitPromptOrNotify(PresubmitResult
):
83 def __init__(self
, message
, items
=None, long_text
=''):
84 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
85 self
.type = 'promptOrNotify'
88 class MockFile(object):
89 """Mock class for the File class.
91 This class can be used to form the mock list of changed files in
92 MockInputApi for presubmit unittests.
95 def __init__(self
, local_path
, new_contents
):
96 self
._local
_path
= local_path
97 self
._new
_contents
= new_contents
98 self
._changed
_contents
= [(i
+ 1, l
) for i
, l
in enumerate(new_contents
)]
100 def ChangedContents(self
):
101 return self
._changed
_contents
103 def NewContents(self
):
104 return self
._new
_contents
107 return self
._local
_path
110 """os.path.basename is called on MockFile so we need an rfind method."""
111 return self
._local
_path
.rfind(p
)
113 def __getitem__(self
, i
):
114 """os.path.basename is called on MockFile so we need a get method."""
115 return self
._local
_path
[i
]
118 class MockAffectedFile(MockFile
):
119 def AbsoluteLocalPath(self
):
120 return self
._local
_path
123 class MockChange(object):
124 """Mock class for Change class.
126 This class can be used in presubmit unittests to mock the query of the
130 def __init__(self
, changed_files
):
131 self
._changed
_files
= changed_files
133 def LocalPaths(self
):
134 return self
._changed
_files