Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: PRESUBMIT_test_mocks.py

Issue 966703003: Add PRESUBMIT tests for _CheckUserActionUpdate function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « PRESUBMIT_test.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json 5 import json
6 import os 6 import os
7 import re 7 import re
8 import subprocess 8 import subprocess
9 import sys 9 import sys
10 10
(...skipping 14 matching lines...) Expand all
25 self.files = [] 25 self.files = []
26 self.is_committing = False 26 self.is_committing = False
27 self.change = MockChange([]) 27 self.change = MockChange([])
28 28
29 def AffectedFiles(self, file_filter=None): 29 def AffectedFiles(self, file_filter=None):
30 return self.files 30 return self.files
31 31
32 def AffectedSourceFiles(self, file_filter=None): 32 def AffectedSourceFiles(self, file_filter=None):
33 return self.files 33 return self.files
34 34
35 def LocalPaths(self):
36 return self.files
37
35 def PresubmitLocalPath(self): 38 def PresubmitLocalPath(self):
36 return os.path.dirname(__file__) 39 return os.path.dirname(__file__)
37 40
38 def ReadFile(self, filename, mode='rU'): 41 def ReadFile(self, filename, mode='rU'):
39 if hasattr(filename, 'AbsoluteLocalPath'): 42 if hasattr(filename, 'AbsoluteLocalPath'):
40 filename = filename.AbsoluteLocalPath() 43 filename = filename.AbsoluteLocalPath()
41 for file_ in self.files: 44 for file_ in self.files:
42 if file_.LocalPath() == filename: 45 if file_.LocalPath() == filename:
43 return '\n'.join(file_.NewContents()) 46 return '\n'.join(file_.NewContents())
44 # Otherwise, file is not in our mock API. 47 # Otherwise, file is not in our mock API.
(...skipping 10 matching lines...) Expand all
55 class PresubmitResult(object): 58 class PresubmitResult(object):
56 def __init__(self, message, items=None, long_text=''): 59 def __init__(self, message, items=None, long_text=''):
57 self.message = message 60 self.message = message
58 self.items = items 61 self.items = items
59 self.long_text = long_text 62 self.long_text = long_text
60 63
61 def __repr__(self): 64 def __repr__(self):
62 return self.message 65 return self.message
63 66
64 class PresubmitError(PresubmitResult): 67 class PresubmitError(PresubmitResult):
65 def __init__(self, message, items, long_text=''): 68 def __init__(self, message, items=None, long_text=''):
66 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 69 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
67 self.type = 'error' 70 self.type = 'error'
68 71
69 class PresubmitPromptWarning(PresubmitResult): 72 class PresubmitPromptWarning(PresubmitResult):
70 def __init__(self, message, items, long_text=''): 73 def __init__(self, message, items=None, long_text=''):
71 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 74 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
72 self.type = 'warning' 75 self.type = 'warning'
73 76
74 class PresubmitNotifyResult(PresubmitResult): 77 class PresubmitNotifyResult(PresubmitResult):
75 def __init__(self, message, items, long_text=''): 78 def __init__(self, message, items=None, long_text=''):
76 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 79 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
77 self.type = 'notify' 80 self.type = 'notify'
78 81
79 class PresubmitPromptOrNotify(PresubmitResult): 82 class PresubmitPromptOrNotify(PresubmitResult):
80 def __init__(self, message, items, long_text=''): 83 def __init__(self, message, items=None, long_text=''):
81 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 84 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)
82 self.type = 'promptOrNotify' 85 self.type = 'promptOrNotify'
83 86
84 87
85 class MockFile(object): 88 class MockFile(object):
86 """Mock class for the File class. 89 """Mock class for the File class.
87 90
88 This class can be used to form the mock list of changed files in 91 This class can be used to form the mock list of changed files in
89 MockInputApi for presubmit unittests. 92 MockInputApi for presubmit unittests.
90 """ 93 """
91 94
92 def __init__(self, local_path, new_contents): 95 def __init__(self, local_path, new_contents):
93 self._local_path = local_path 96 self._local_path = local_path
94 self._new_contents = new_contents 97 self._new_contents = new_contents
95 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] 98 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
96 99
97 def ChangedContents(self): 100 def ChangedContents(self):
98 return self._changed_contents 101 return self._changed_contents
99 102
100 def NewContents(self): 103 def NewContents(self):
101 return self._new_contents 104 return self._new_contents
102 105
103 def LocalPath(self): 106 def LocalPath(self):
104 return self._local_path 107 return self._local_path
105 108
109 def rfind(self, p):
110 """os.path.basename is called on MockFile so we need an rfind method."""
111 return self._local_path.rfind(p)
112
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]
116
106 117
107 class MockAffectedFile(MockFile): 118 class MockAffectedFile(MockFile):
108 def AbsoluteLocalPath(self): 119 def AbsoluteLocalPath(self):
109 return self._local_path 120 return self._local_path
110 121
111 122
112 class MockChange(object): 123 class MockChange(object):
113 """Mock class for Change class. 124 """Mock class for Change class.
114 125
115 This class can be used in presubmit unittests to mock the query of the 126 This class can be used in presubmit unittests to mock the query of the
116 current change. 127 current change.
117 """ 128 """
118 129
119 def __init__(self, changed_files): 130 def __init__(self, changed_files):
120 self._changed_files = changed_files 131 self._changed_files = changed_files
121 132
122 def LocalPaths(self): 133 def LocalPaths(self):
123 return self._changed_files 134 return self._changed_files
OLDNEW
« no previous file with comments | « PRESUBMIT_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698