Add a check for file with only comments
That should catch things like __init__.py files with only license headers inside. Change-Id: I6a2078f6264ef252d4eb431c8df33db0913ba3bf
This commit is contained in:
parent
56c491c0e2
commit
0ffdb453f3
@ -285,6 +285,8 @@ All source files should have the following header:
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
Files with no code shouldn't contain any license header nor comments, and
|
||||
must be left completely empty.
|
||||
|
||||
Commit Messages
|
||||
---------------
|
||||
|
@ -1,14 +0,0 @@
|
||||
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
@ -228,6 +228,19 @@ def hacking_has_correct_license(physical_line, filename, lines, line_number):
|
||||
"License notice")
|
||||
|
||||
|
||||
EMPTY_LINE_RE = re.compile("^\s*(#.*|$)")
|
||||
|
||||
|
||||
@flake8ext
|
||||
def hacking_has_only_comments(physical_line, filename, lines, line_number):
|
||||
"""Check for empty files with only comments
|
||||
|
||||
H104 empty file with only comments
|
||||
"""
|
||||
if line_number == 1 and all(map(EMPTY_LINE_RE.match, lines)):
|
||||
return (0, "H104: File contains nothing but comments")
|
||||
|
||||
|
||||
@flake8ext
|
||||
def hacking_except_format(logical_line, physical_line):
|
||||
r"""Check for 'except:'.
|
||||
|
62
hacking/tests/test_core.py
Normal file
62
hacking/tests/test_core.py
Normal file
@ -0,0 +1,62 @@
|
||||
# Copyright (c) 2013 eNovance
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from hacking import core
|
||||
from hacking import tests
|
||||
|
||||
|
||||
class CoreTestCase(tests.TestCase):
|
||||
def test_H104_regex(self):
|
||||
"""Verify that the H104 regex matches correct lines."""
|
||||
self.assertTrue(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo',
|
||||
'# bar'],
|
||||
1))
|
||||
self.assertTrue(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo',
|
||||
'# bar',
|
||||
''],
|
||||
1))
|
||||
self.assertTrue(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo',
|
||||
' ',
|
||||
'# bar'],
|
||||
1))
|
||||
|
||||
self.assertIsNone(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo',
|
||||
' ',
|
||||
'"""foobar"""'],
|
||||
1))
|
||||
self.assertIsNone(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo',
|
||||
'',
|
||||
'print(42)'],
|
||||
1))
|
||||
self.assertIsNone(core.hacking_has_only_comments(
|
||||
None,
|
||||
None,
|
||||
['# foo'],
|
||||
100))
|
@ -66,7 +66,10 @@ def _get_lines(check):
|
||||
|
||||
def load_tests(loader, tests, pattern):
|
||||
|
||||
flake8_style = engine.get_style_guide(parse_argv=False, ignore='F')
|
||||
flake8_style = engine.get_style_guide(parse_argv=False,
|
||||
# Ignore H104 otherwise it's
|
||||
# raised on doctests.
|
||||
ignore=('F', 'H104'))
|
||||
options = flake8_style.options
|
||||
|
||||
for name, check in hacking.core.__dict__.items():
|
||||
|
@ -26,6 +26,7 @@ flake8.extension =
|
||||
H101 = hacking.core:hacking_todo_format
|
||||
H102 = hacking.core:hacking_has_license
|
||||
H103 = hacking.core:hacking_has_correct_license
|
||||
H104 = hacking.core:hacking_has_only_comments
|
||||
H201 = hacking.core:hacking_except_format
|
||||
H202 = hacking.core:hacking_except_format_assert
|
||||
H231 = hacking.core:hacking_python3x_except_compatible
|
||||
|
Loading…
Reference in New Issue
Block a user