Fix unconditional license print if H103 is not selected

Regardless of H103 being selected by the user, if the license is not
an exact apache2 license, it will be printed out poluting the output.

With this change the comparison will be printed along with the error
message, so will not be visible if H103 was not selected.

Change-Id: Id526d83626e377aedd0bd2a998fc7d6e01058d3e
This commit is contained in:
Ilya Maximets 2022-02-23 00:52:42 +01:00
parent e42996fea9
commit ab5de186cf
2 changed files with 22 additions and 9 deletions

View File

@ -86,11 +86,12 @@ def hacking_has_correct_license(physical_line, filename, lines, line_number):
if line_number == 1 and len(lines) > 10 and _project_is_apache():
for idx, line in enumerate(lines):
column = line.find('Licensed under the Apache License')
if (0 < column < 10 and not
_check_for_exact_apache(idx, lines)):
if (line.find('SPDX-License-Identifier: Apache-2.0') <= 0):
if 0 < column < 10:
exact, cmp_str = _check_for_exact_apache(idx, lines)
if (not exact and
line.find('SPDX-License-Identifier: Apache-2.0') <= 0):
return (column, "H103: Header does not match Apache 2.0 "
"License notice")
"License notice" + cmp_str)
EMPTY_LINE_RE = re.compile(r"^\s*(#.*|$)")
@ -163,11 +164,10 @@ under the License."""
stripped_apache2 = re.sub(r'\s+', ' ', APACHE2).strip()
if stripped_apache2 in content:
return True
return (True, None)
else:
print("<license>!=<apache2>:\n'%s' !=\n'%s'" %
(content, stripped_apache2))
return False
return (False, "\n<license>!=<apache2>:\n'%s' !=\n'%s'" %
(content, stripped_apache2))
@core.flake8ext

View File

@ -90,7 +90,20 @@ class CoreTestCase(tests.TestCase):
def test_H103_full_fail(self):
"""Verify that the H103 check finds an SPDX header"""
self.assertEqual(
(2, 'H103: Header does not match Apache 2.0 License notice'),
(2, 'H103: Header does not match Apache 2.0 License notice\n'
'<license>!=<apache2>:\n'
'\'Licensed under the Apache License, Version 2.0 foo bar foo '
'bar foo bar foo bar foo bar\' !=\n'
'\'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.\''),
comments.hacking_has_correct_license(
None,
None,