From a66b6b9eb60d6e887b83d2891cca6f93a31a0ebd Mon Sep 17 00:00:00 2001 From: Nikita Gerasimov Date: Wed, 3 Apr 2024 16:50:16 +0300 Subject: [PATCH 1/2] Handle trailing newline in macro definitions Fixes #360 Signed-off-by: Nikita Gerasimov --- specfile/conditions.py | 2 +- specfile/macro_definitions.py | 8 +--- specfile/sections.py | 2 +- tests/unit/test_conditions.py | 4 ++ tests/unit/test_macro_definitions.py | 20 ++++++++++ tests/unit/test_sections.py | 56 +++++++++++++++------------- 6 files changed, 58 insertions(+), 34 deletions(-) diff --git a/specfile/conditions.py b/specfile/conditions.py index 257ac92..e56c2e8 100644 --- a/specfile/conditions.py +++ b/specfile/conditions.py @@ -82,7 +82,7 @@ def process_conditions( if macro_definitions: for md in macro_definitions: position = md.get_position(macro_definitions) - excluded_lines.append(range(position, position + len(md.body.splitlines()))) + excluded_lines.append(range(position, position + len(md.body.split("\n")))) condition_regex = re.compile( r""" ^ diff --git a/specfile/macro_definitions.py b/specfile/macro_definitions.py index 68a4316..9f136c9 100644 --- a/specfile/macro_definitions.py +++ b/specfile/macro_definitions.py @@ -156,11 +156,7 @@ def get_raw_data(self) -> List[str]: elif self.comment_out_style is CommentOutStyle.HASH: sc = "#" macro = "global" if self.is_global else "define" - body = self.body.splitlines() - if body: - body[-1] += ws[3] - else: - body = [ws[3]] + body = (self.body + ws[3]).split("\n") result.append(f"{ws[0]}{dnl}{pre}{sc}{macro}{ws[1]}{self.name}{ws[2]}{body[0]}") result.extend(body[1:]) return result @@ -383,7 +379,7 @@ def count_brackets(s): line, _ = pop(lines) body += "\n" + line bc, pc = count_brackets(body) - tokens = re.split(r"(\s+)$", body, maxsplit=1) + tokens = re.split(r"([^\S\n]+)$", body, maxsplit=1) if len(tokens) == 1: body = tokens[0] else: diff --git a/specfile/sections.py b/specfile/sections.py index 1057e88..7b5ae98 100644 --- a/specfile/sections.py +++ b/specfile/sections.py @@ -268,7 +268,7 @@ def split_id(line): macro_definitions = MacroDefinitions.parse(lines) for md in macro_definitions: position = md.get_position(macro_definitions) - excluded_lines.append(range(position, position + len(md.body.splitlines()))) + excluded_lines.append(range(position, position + len(md.body.split("\n")))) section_id_regexes = [ re.compile(rf"^%{re.escape(n)}(\s+.*(? Date: Wed, 3 Apr 2024 20:01:40 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unit/test_conditions.py | 4 +++- tests/unit/test_sections.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_conditions.py b/tests/unit/test_conditions.py index c9c598f..2fba827 100644 --- a/tests/unit/test_conditions.py +++ b/tests/unit/test_conditions.py @@ -78,6 +78,8 @@ def resolve_expression(kwd, exp, *_, **__): processed_lines, processed_validity = zip(*process_conditions(lines)) assert list(processed_lines) == lines assert list(processed_validity) == validity - processed_lines, processed_validity = zip(*process_conditions(lines, MacroDefinitions.parse(lines))) + processed_lines, processed_validity = zip( + *process_conditions(lines, MacroDefinitions.parse(lines)) + ) assert list(processed_lines) == lines assert list(processed_validity) == validity diff --git a/tests/unit/test_sections.py b/tests/unit/test_sections.py index 9e8cba6..ccc0736 100644 --- a/tests/unit/test_sections.py +++ b/tests/unit/test_sections.py @@ -6,9 +6,9 @@ import pytest from flexmock import flexmock +from specfile.macro_definitions import MacroDefinitions from specfile.options import Options, Token, TokenType from specfile.sections import Section, Sections -from specfile.macro_definitions import MacroDefinitions def test_find():