Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of conditions #405

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading