]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add hacking check for vim headers
authorJay S. Bryant <jsbryant@us.ibm.com>
Tue, 29 Jul 2014 03:03:42 +0000 (22:03 -0500)
committerJay S. Bryant <jsbryant@us.ibm.com>
Tue, 29 Jul 2014 03:26:25 +0000 (22:26 -0500)
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

bin/cinder-rtstool
cinder/hacking/checks.py
cinder/openstack/__init__.py
cinder/tests/api/contrib/test_used_limits.py
cinder/tests/test_hacking.py

index a4300fc5cc0fe4e710d2561fab59a8f6ede893aa..fd61d2635592b4d9f8605a0cfe9543cf99a9cf52 100755 (executable)
@@ -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.
index fc213bc9f24fbcbc02446b9b7fa240085e548cab..053cf90e525e49f8a35d686e51b99c1c73d19d2c 100644 (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)
index 0a3b98867a26034118b09e0ad8ae0a3b43f8c881..00a266a597794ca596c72c2b7f5d2401eca77763 100644 (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
index 4511cc83af11950892479e104fd347f1c2040144..86971bf831105ce793e0a5460a55c76ae74dce2e 100644 (file)
@@ -1,5 +1,3 @@
-# vim: tabstop=5 shiftwidth=4 softtabstop=4
-
 # Copyright 2012 OpenStack Foundation
 # All Rights Reserved.
 #
index a58db7182a62325e9bb93cd205bd9e2c402d56e0..71793401e3c70b6a50ed8627d5315b1b7b0c0bfe 100644 (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)