Add hacking check for vim headers

We have a number of commits that have removed the vim
headers from openstack/common components as well as
commits to remove the headers from drivers.  We, however,
haven't put a hacking check in to make sure that they don't
sneak back into the code as is evidenced by the fact that some
files failed the check.

This commit adds in the check, the associated test cases and
fixes the couple of files that had a vim header again.

Change-Id: I727bd2aff7ac7b909c9e7cd4f3639f5873793e97
This commit is contained in:
Jay S. Bryant 2014-07-28 22:03:42 -05:00
parent d95c0c3881
commit 6c272bcf2e
5 changed files with 33 additions and 5 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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

View File

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

View File

@ -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)