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

Side by Side Diff: presubmit_canned_checks.py

Issue 118505: Add CheckChangeHasOnlyOneEol and CheckChangeHasNoCrAndHasOnlyOneEol. (Closed)
Patch Set: oops2 Created 11 years, 6 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 | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Generic presubmit checks that can be reused by other presubmit checks.""" 6 """Generic presubmit checks that can be reused by other presubmit checks."""
7 7
8 8
9 ### Description checks 9 ### Description checks
10 10
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 keyword = 'DO NOT ' + 'SUBMIT' 60 keyword = 'DO NOT ' + 'SUBMIT'
61 # We want to check every text files, not just source files. 61 # We want to check every text files, not just source files.
62 for f, line_num, line in input_api.RightHandSideLines(lambda x: x): 62 for f, line_num, line in input_api.RightHandSideLines(lambda x: x):
63 if keyword in line: 63 if keyword in line:
64 text = 'Found ' + keyword + ' in %s, line %s' % (f.LocalPath(), line_num) 64 text = 'Found ' + keyword + ' in %s, line %s' % (f.LocalPath(), line_num)
65 return [output_api.PresubmitError(text)] 65 return [output_api.PresubmitError(text)]
66 return [] 66 return []
67 67
68 68
69 def CheckChangeHasNoCR(input_api, output_api, source_file_filter=None): 69 def CheckChangeHasNoCR(input_api, output_api, source_file_filter=None):
70 """Checks that there are no \r, \r\n (CR or CRLF) characters in any of the 70 """Checks no '\r' (CR) character is in any source files."""
71 source files to be submitted. 71 cr_files = []
72 """
73 outputs = []
74 for f in input_api.AffectedSourceFiles(source_file_filter): 72 for f in input_api.AffectedSourceFiles(source_file_filter):
75 if '\r' in input_api.ReadFile(f, 'rb'): 73 if '\r' in input_api.ReadFile(f, 'rb'):
76 outputs.append(output_api.PresubmitPromptWarning( 74 cr_files.append(f.LocalPath())
77 "Found a CR character in %s" % f.LocalPath())) 75 if cr_files:
76 return [output_api.PresubmitPromptWarning(
77 "Found a CR character in these files:", items=cr_files)]
78 return []
79
80
81 def CheckChangeHasOnlyOneEol(input_api, output_api, source_file_filter=None):
82 """Checks the files ends with one and only one \n (LF)."""
83 eof_files = []
84 for f in input_api.AffectedSourceFiles(source_file_filter):
85 contents = input_api.ReadFile(f, 'rb')
86 # Check that the file ends in one and only one newline character.
87 if len(contents) > 1 and (contents[-1:] != "\n" or contents[-2:-1] == "\n"):
88 eof_files.append(f.LocalPath())
89
90 if eof_files:
91 return [output_api.PresubmitPromptWarning(
92 'These files should end in one (and only one) newline character:',
93 items=eof_files)]
94 return []
95
96
97 def CheckChangeHasNoCrAndHasOnlyOneEol(input_api, output_api,
98 source_file_filter=None):
99 """Runs both CheckChangeHasNoCR and CheckChangeHasOnlyOneEOL in one pass.
100
101 It is faster because it is reading the file only once.
Jói Sigurðsson 2009/06/10 18:02:33 this makes me think perhaps in the future we shoul
102 """
103 cr_files = []
104 eof_files = []
105 for f in input_api.AffectedSourceFiles(source_file_filter):
106 contents = input_api.ReadFile(f, 'rb')
107 if '\r' in contents:
108 cr_files.append(f.LocalPath())
109 # Check that the file ends in one and only one newline character.
110 if len(contents) > 1 and (contents[-1:] != "\n" or contents[-2:-1] == "\n"):
111 eof_files.append(f.LocalPath())
112 outputs = []
113 if cr_files:
114 outputs.append(output_api.PresubmitPromptWarning(
115 "Found a CR character in these files:", items=cr_files))
116 if eof_files:
117 outputs.append(output_api.PresubmitPromptWarning(
118 'These files should end in one (and only one) newline character:',
119 items=eof_files))
78 return outputs 120 return outputs
79 121
80 122
81 def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None): 123 def CheckChangeHasNoTabs(input_api, output_api, source_file_filter=None):
82 """Checks that there are no tab characters in any of the text files to be 124 """Checks that there are no tab characters in any of the text files to be
83 submitted. 125 submitted.
84 """ 126 """
127 tabs = []
85 for f, line_num, line in input_api.RightHandSideLines(source_file_filter): 128 for f, line_num, line in input_api.RightHandSideLines(source_file_filter):
86 if '\t' in line: 129 if '\t' in line:
87 return [output_api.PresubmitPromptWarning( 130 tabs.append("%s, line %s" % (f.LocalPath(), line_num))
88 "Found a tab character in %s, line %s" % 131 if tabs:
89 (f.LocalPath(), line_num))] 132 return [output_api.PresubmitPromptWarning("Found a tab character in:",
133 long_text="\n".join(tabs))]
90 return [] 134 return []
91 135
92 136
93 def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None): 137 def CheckLongLines(input_api, output_api, maxlen=80, source_file_filter=None):
94 """Checks that there aren't any lines longer than maxlen characters in any of 138 """Checks that there aren't any lines longer than maxlen characters in any of
95 the text files to be submitted. 139 the text files to be submitted.
96 """ 140 """
97 bad = [] 141 bad = []
98 for f, line_num, line in input_api.RightHandSideLines(source_file_filter): 142 for f, line_num, line in input_api.RightHandSideLines(source_file_filter):
99 # Allow lines with http://, https:// and #define/#pragma/#include/#if/#endif 143 # Allow lines with http://, https:// and #define/#pragma/#include/#if/#endif
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 long_text=input_api.traceback.format_exc())) 224 long_text=input_api.traceback.format_exc()))
181 225
182 buffer = input_api.cStringIO.StringIO() 226 buffer = input_api.cStringIO.StringIO()
183 results = input_api.unittest.TextTestRunner(stream=buffer, verbosity=0).run( 227 results = input_api.unittest.TextTestRunner(stream=buffer, verbosity=0).run(
184 input_api.unittest.TestSuite(tests_suite)) 228 input_api.unittest.TestSuite(tests_suite))
185 if not results.wasSuccessful(): 229 if not results.wasSuccessful():
186 outputs.append(message_type("%d unit tests failed." % 230 outputs.append(message_type("%d unit tests failed." %
187 (len(results.failures) + len(results.errors)), 231 (len(results.failures) + len(results.errors)),
188 long_text=buffer.getvalue())) 232 long_text=buffer.getvalue()))
189 return outputs 233 return outputs
OLDNEW
« no previous file with comments | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698