From ab5de186cf326c5ac95ed5f36a8dccff0a21f96c Mon Sep 17 00:00:00 2001 From: Ilya Maximets Date: Wed, 23 Feb 2022 00:52:42 +0100 Subject: [PATCH] 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 --- hacking/checks/comments.py | 16 ++++++++-------- hacking/tests/checks/test_comments.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/hacking/checks/comments.py b/hacking/checks/comments.py index 4034c1a..3ab6887 100644 --- a/hacking/checks/comments.py +++ b/hacking/checks/comments.py @@ -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("!=:\n'%s' !=\n'%s'" % - (content, stripped_apache2)) - return False + return (False, "\n!=:\n'%s' !=\n'%s'" % + (content, stripped_apache2)) @core.flake8ext diff --git a/hacking/tests/checks/test_comments.py b/hacking/tests/checks/test_comments.py index f837ee2..231672e 100644 --- a/hacking/tests/checks/test_comments.py +++ b/hacking/tests/checks/test_comments.py @@ -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' + '!=:\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,