From 6c272bcf2e508deb7eab98c4042a78a80f2f75f8 Mon Sep 17 00:00:00 2001 From: "Jay S. Bryant" Date: Mon, 28 Jul 2014 22:03:42 -0500 Subject: [PATCH] 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 --- bin/cinder-rtstool | 1 - cinder/hacking/checks.py | 16 ++++++++++++++++ cinder/openstack/__init__.py | 2 -- cinder/tests/api/contrib/test_used_limits.py | 2 -- cinder/tests/test_hacking.py | 17 +++++++++++++++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/bin/cinder-rtstool b/bin/cinder-rtstool index a4300fc5c..fd61d2635 100755 --- a/bin/cinder-rtstool +++ b/bin/cinder-rtstool @@ -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. diff --git a/cinder/hacking/checks.py b/cinder/hacking/checks.py index fc213bc9f..053cf90e5 100644 --- a/cinder/hacking/checks.py +++ b/cinder/hacking/checks.py @@ -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) diff --git a/cinder/openstack/__init__.py b/cinder/openstack/__init__.py index 0a3b98867..00a266a59 100644 --- a/cinder/openstack/__init__.py +++ b/cinder/openstack/__init__.py @@ -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 diff --git a/cinder/tests/api/contrib/test_used_limits.py b/cinder/tests/api/contrib/test_used_limits.py index 4511cc83a..86971bf83 100644 --- a/cinder/tests/api/contrib/test_used_limits.py +++ b/cinder/tests/api/contrib/test_used_limits.py @@ -1,5 +1,3 @@ -# vim: tabstop=5 shiftwidth=4 softtabstop=4 - # Copyright 2012 OpenStack Foundation # All Rights Reserved. # diff --git a/cinder/tests/test_hacking.py b/cinder/tests/test_hacking.py index a58db7182..71793401e 100644 --- a/cinder/tests/test_hacking.py +++ b/cinder/tests/test_hacking.py @@ -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) -- 2.45.2