Merge "Add hacking check for vim headers"

This commit is contained in:
Jenkins 2014-07-29 17:59:16 +00:00 committed by Gerrit Code Review
commit 28647b4ecd
5 changed files with 33 additions and 5 deletions

@ -1,5 +1,4 @@
#!/usr/bin/env python
# vim: et tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 - 2013 Red Hat, Inc.
# All Rights Reserved.

@ -34,6 +34,21 @@ UNDERSCORE_IMPORT_FILES = []
log_translation = re.compile(
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)_\(\s*('|\")")
string_translation = re.compile(r"(.)*_\(\s*('|\")")
vi_header_re = re.compile(r"^#\s+vim?:.+")
def no_vi_headers(physical_line, line_number, lines):
"""Check for vi editor configuration in source files.
By default vi modelines can only appear in the first or
last 5 lines of a source file.
N314
"""
# NOTE(gilliard): line_number is 1-indexed
if line_number <= 5 or line_number > len(lines) - 5:
if vi_header_re.match(physical_line):
return 0, "N314: Don't put vi configuration in source files"
def no_translate_debug_logs(logical_line, filename):
@ -80,6 +95,7 @@ def check_explicit_underscore_import(logical_line, filename):
def factory(register):
register(no_vi_headers)
register(no_translate_debug_logs)
register(no_mutable_default_args)
register(check_explicit_underscore_import)

@ -1,5 +1,3 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may

@ -1,5 +1,3 @@
# vim: tabstop=5 shiftwidth=4 softtabstop=4
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#

@ -49,6 +49,23 @@ class HackingTestCase(test.TestCase):
should pass.
"""
def test_no_vi_headers(self):
lines = ['Line 1\n', 'Line 2\n', 'Line 3\n', 'Line 4\n', 'Line 5\n'
'Line 6\n', 'Line 7\n', 'Line 8\n', 'Line 9\n', 'Line 10\n']
self.assertEqual(checks.no_vi_headers(
"Test string foo", 1, lines), None)
self.assertEqual(len(list(checks.no_vi_headers(
"# vim: et tabstop=4 shiftwidth=4 softtabstop=4",
2, lines))), 2)
self.assertEqual(len(list(checks.no_vi_headers(
"# vim: et tabstop=4 shiftwidth=4 softtabstop=4",
8, lines))), 2)
self.assertEqual(checks.no_vi_headers(
"Test end string for vi",
9, lines), None)
def test_no_translate_debug_logs(self):
self.assertEqual(len(list(checks.no_translate_debug_logs(
"LOG.debug(_('foo'))", "cinder/scheduler/foo.py"))), 1)