Skip to content

Commit 9930c15

Browse files
Fix parsing of conditions (#405)
Fix parsing of conditions After 157a62b conditional expressions are parsed after macro expansion, which means that the actual expression can be empty. Account for that. Fixes #403. Reviewed-by: Laura Barcziová
2 parents 122eefa + 0d28c5f commit 9930c15

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

specfile/conditions.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,23 @@ def expand(s):
134134
branches.pop()
135135
elif keyword.startswith("%el"):
136136
result.append((line, branches[-2]))
137-
branches[-1] = not branches[-1]
137+
if branches[-2]:
138+
branches[-1] = not branches[-1]
138139
else:
139140
result.append((line, branches[-1]))
140-
expression = m.group("expr")
141-
if expression:
142-
if m.group("end") == "\\":
143-
expression += "\\"
144-
while expression.endswith("\\") and indexed_lines:
145-
_, line = indexed_lines.pop(0)
146-
result.append((line, branches[-1]))
147-
expression = expression[:-1] + line
141+
if keyword.startswith("%if") or keyword.startswith("%elif"):
142+
expression = m.group("expr")
143+
if expression:
144+
if m.group("end") == "\\":
145+
expression += "\\"
146+
while expression.endswith("\\") and indexed_lines:
147+
_, line = indexed_lines.pop(0)
148+
result.append((line, branches[-1]))
149+
expression = expression[:-1] + line
148150
branch = (
149151
False
150152
if not branches[-1]
151-
else resolve_expression(keyword, expression, context)
153+
else resolve_expression(keyword, expression or "0", context)
152154
)
153155
if keyword.startswith("%el"):
154156
branches[-1] = branch

tests/unit/test_conditions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"BuildRequires: libX11-devel",
5454
"%if 0%{?fedora}",
5555
"Requires: desktop-file-utils",
56+
"%else",
57+
"Requires: gnome-desktop",
5658
"%endif",
5759
"BuildRequires: libXext-devel",
5860
"%else",
@@ -69,6 +71,8 @@
6971
False,
7072
False,
7173
False,
74+
False,
75+
False,
7276
True,
7377
True,
7478
False,
@@ -82,6 +86,27 @@
8286
else "1" if expr == "%{expr:0%{?fedora}}" else expr
8387
),
8488
),
89+
(
90+
[
91+
"%if %{bcond_default_lto}",
92+
"%bcond_without lto",
93+
"%else",
94+
"%bcond_with lto",
95+
"%endif",
96+
],
97+
[
98+
True,
99+
False,
100+
True,
101+
True,
102+
True,
103+
],
104+
lambda expr: (
105+
""
106+
if expr == "%{bcond_default_lto}"
107+
else "0" if expr == "%{expr:0}" else expr
108+
),
109+
),
85110
],
86111
)
87112
def test_process_conditions(lines, validity, expand_func):

0 commit comments

Comments
 (0)