Skip to content

Commit

Permalink
Fix parsing of conditions (#405)
Browse files Browse the repository at this point in the history
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á
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jul 29, 2024
2 parents 122eefa + 0d28c5f commit 9930c15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
22 changes: 12 additions & 10 deletions specfile/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,23 @@ def expand(s):
branches.pop()
elif keyword.startswith("%el"):
result.append((line, branches[-2]))
branches[-1] = not branches[-1]
if branches[-2]:
branches[-1] = not branches[-1]
else:
result.append((line, branches[-1]))
expression = m.group("expr")
if expression:
if m.group("end") == "\\":
expression += "\\"
while expression.endswith("\\") and indexed_lines:
_, line = indexed_lines.pop(0)
result.append((line, branches[-1]))
expression = expression[:-1] + line
if keyword.startswith("%if") or keyword.startswith("%elif"):
expression = m.group("expr")
if expression:
if m.group("end") == "\\":
expression += "\\"
while expression.endswith("\\") and indexed_lines:
_, line = indexed_lines.pop(0)
result.append((line, branches[-1]))
expression = expression[:-1] + line
branch = (
False
if not branches[-1]
else resolve_expression(keyword, expression, context)
else resolve_expression(keyword, expression or "0", context)
)
if keyword.startswith("%el"):
branches[-1] = branch
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"BuildRequires: libX11-devel",
"%if 0%{?fedora}",
"Requires: desktop-file-utils",
"%else",
"Requires: gnome-desktop",
"%endif",
"BuildRequires: libXext-devel",
"%else",
Expand All @@ -69,6 +71,8 @@
False,
False,
False,
False,
False,
True,
True,
False,
Expand All @@ -82,6 +86,27 @@
else "1" if expr == "%{expr:0%{?fedora}}" else expr
),
),
(
[
"%if %{bcond_default_lto}",
"%bcond_without lto",
"%else",
"%bcond_with lto",
"%endif",
],
[
True,
False,
True,
True,
True,
],
lambda expr: (
""
if expr == "%{bcond_default_lto}"
else "0" if expr == "%{expr:0}" else expr
),
),
],
)
def test_process_conditions(lines, validity, expand_func):
Expand Down

0 comments on commit 9930c15

Please sign in to comment.