Skip to content

Commit

Permalink
Handle trailing newline in macro definitions (#361)
Browse files Browse the repository at this point in the history
Handle trailing newline in macro definitions

Fixes #360.

Reviewed-by: Nikola Forró
  • Loading branch information
2 parents a14e688 + 2f21f13 commit 6fc5bee
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion specfile/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
^
Expand Down
8 changes: 2 additions & 6 deletions specfile/macro_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion specfile/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+.*(?<!\\)$|$)", re.IGNORECASE)
for n in SECTION_NAMES
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import specfile.conditions
from specfile.conditions import process_conditions
from specfile.macro_definitions import MacroDefinitions


@pytest.mark.parametrize(
Expand Down Expand Up @@ -77,3 +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))
)
assert list(processed_lines) == lines
assert list(processed_validity) == validity
20 changes: 20 additions & 0 deletions tests/unit/test_macro_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def test_parse():
"%define desc(x) Test spec file containing several \\",
"macro definitions in various formats (%?1)",
"",
"%global trailing_newline \\",
"body with trailing newline \\",
"",
"%define example() %{expand:",
"This an example of a macro definition with body ",
"spawning across mutiple lines}",
Expand All @@ -124,6 +127,12 @@ def test_parse():
"Test spec file containing several \\\n"
"macro definitions in various formats (%?1)"
)
assert macro_definitions[7].name == "trailing_newline"
assert macro_definitions[7].body == "\\\nbody with trailing newline \\\n"
assert macro_definitions[7].is_global
assert not macro_definitions[7].commented_out
assert macro_definitions[7]._whitespace == ("", " ", " ", "")
assert macro_definitions[7].valid
assert macro_definitions[-1].name == "example()"
assert macro_definitions[-1].body == (
"%{expand:\n"
Expand Down Expand Up @@ -220,6 +229,14 @@ def test_get_raw_data():
True,
[""],
),
MacroDefinition(
"trailing_newline",
"\\\nbody with trailing newline \\\n",
True,
False,
CommentOutStyle.DNL,
("", " ", " ", ""),
),
]
)
assert macro_definitions.get_raw_data() == [
Expand All @@ -240,6 +257,9 @@ def test_get_raw_data():
"%define example() %{expand:",
"This an example of a macro definition with body ",
"spawning across mutiple lines}",
"%global trailing_newline \\",
"body with trailing newline \\",
"",
]


Expand Down
56 changes: 30 additions & 26 deletions tests/unit/test_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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

Expand Down Expand Up @@ -104,32 +105,35 @@ def test_parse_invalid_name():


def test_parse_macro_definitions():
sections = Sections.parse(
[
"%package -n test",
"Summary: Subpackage test",
"",
"%description -n test",
"Subpackage test.",
"",
"%define template1()\\",
"%package -n %{1}\\",
"Summary: Subpackage %{1}\\",
"\\",
"%description -n %{1}\\",
"Subpackage %{1}.",
"",
"%define template2() %{expand:",
"%package -n %{1}",
"Summary: Subpackage %{1}",
"",
"%description -n %{1}",
"Subpackage %{1}.}",
"",
"%prep",
"%autosetup",
]
)
lines = [
"%package -n test",
"Summary: Subpackage test",
"",
"%description -n test",
"Subpackage test.",
"",
"%define template1()\\",
"%package -n %{1}\\",
"Summary: Subpackage %{1}\\",
"\\",
"%description -n %{1}\\",
"Subpackage %{1}.",
"",
"%define template2() %{expand:",
"%package -n %{1}",
"Summary: Subpackage %{1}",
"",
"%description -n %{1}",
"Subpackage %{1}.}",
"",
"%prep",
"%autosetup",
]
sections = Sections.parse(lines)
assert len(sections) == 4
assert sections[1].id == "package -n test"
assert sections[-1].id == "prep"
sections = Sections.parse(lines, MacroDefinitions.parse(lines))
assert len(sections) == 4
assert sections[1].id == "package -n test"
assert sections[-1].id == "prep"
Expand Down

0 comments on commit 6fc5bee

Please sign in to comment.