Skip to content

Commit fab0c7a

Browse files
committed
Ignore sections in macro definitions
Signed-off-by: Nikola Forró <[email protected]>
1 parent c5d86c0 commit fab0c7a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

specfile/sections.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
SIMPLE_SCRIPT_SECTIONS,
1414
)
1515
from specfile.formatter import formatted
16+
from specfile.macro_definitions import MacroDefinitions
1617
from specfile.macros import Macros
1718
from specfile.options import Options
1819

@@ -258,12 +259,20 @@ def split_id(line):
258259
return name, options, delimiter, separator, content
259260
return tokens[0], None, "", separator, content
260261

262+
excluded_lines = []
263+
macro_definitions = MacroDefinitions.parse(lines)
264+
for md in macro_definitions:
265+
position = md.get_position(macro_definitions)
266+
excluded_lines.append(range(position, position + len(md.get_raw_data())))
261267
section_id_regexes = [
262-
re.compile(rf"^%{re.escape(n)}(\s+.*$|$)", re.IGNORECASE)
268+
re.compile(rf"^%{re.escape(n)}(\s+.*(?<!\\)$|$)", re.IGNORECASE)
263269
for n in SECTION_NAMES
264270
]
265271
section_starts = []
266272
for i, line in enumerate(lines):
273+
# section can not start inside macro definition body
274+
if any(i in r for r in excluded_lines):
275+
continue
267276
if line.startswith("%"):
268277
for r in section_id_regexes:
269278
if r.match(line):

tests/unit/test_sections.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ def test_parse_invalid_name():
103103
assert sections.description[2] == "%description(fr)"
104104

105105

106+
def test_parse_macro_definitions():
107+
sections = Sections.parse(
108+
[
109+
"%package -n test",
110+
"Summary: Subpackage test",
111+
"",
112+
"%description -n test",
113+
"Subpackage test.",
114+
"",
115+
"%define template1()\\",
116+
"%package -n %{1}\\",
117+
"Summary: Subpackage %{1}\\",
118+
"\\",
119+
"%description -n %{1}\\",
120+
"Subpackage %{1}.",
121+
"",
122+
"%define template2() %{expand:",
123+
"%package -n %{1}",
124+
"Summary: Subpackage %{1}",
125+
"",
126+
"%description -n %{1}",
127+
"Subpackage %{1}.}",
128+
]
129+
)
130+
assert len(sections) == 3
131+
assert sections[1].id == "package -n test"
132+
133+
106134
def test_get_raw_data():
107135
sections = Sections(
108136
[

0 commit comments

Comments
 (0)