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

Attempt to expand macros before processing conditions #394

Merged
merged 2 commits into from
Jul 2, 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
20 changes: 17 additions & 3 deletions specfile/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def expand(s):
return True
elif keyword.endswith("arch"):
target_cpu = expand("%{_target_cpu}")
match = any(t for t in expression.split() if t == target_cpu)
match = any(t for t in expand(expression).split() if t == target_cpu)
return not match if keyword == "%ifnarch" else match
elif keyword.endswith("os"):
target_os = expand("%{_target_os}")
match = any(t for t in expression.split() if t == target_os)
match = any(t for t in expand(expression).split() if t == target_os)
return not match if keyword == "%ifnos" else match
return False

Expand All @@ -78,6 +78,15 @@ def process_conditions(
Returns:
List of tuples in the form of (line, validity).
"""

def expand(s):
if not context:
return Macros.expand(s)
result = context.expand(s, skip_parsing=getattr(expand, "skip_parsing", False))
# parse only once
expand.skip_parsing = True
return result

excluded_lines = []
if macro_definitions:
for md in macro_definitions:
Expand Down Expand Up @@ -110,7 +119,12 @@ def process_conditions(
if any(index in r for r in excluded_lines):
result.append((line, branches[-1]))
continue
m = condition_regex.match(line)
try:
expanded_line = expand(line)
except RPMException:
# ignore failed expansion and use the original line
expanded_line = line
m = condition_regex.match(expanded_line)
if not m:
result.append((line, branches[-1]))
continue
Expand Down
38 changes: 26 additions & 12 deletions tests/unit/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
import pytest
from flexmock import flexmock

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


@pytest.mark.parametrize(
"lines, validity, resolve_func",
"lines, validity, expand_func",
[
(
["%ifarch %{power64}", "export ARCH=PPC64", "%endif"],
[True, True, True],
lambda kwd, exp: True,
lambda expr: (
"ppc64"
if expr == "%{_target_cpu}"
else "ppc64 ppc64p7 ppc64le" if expr == "%{power64}" else expr
),
),
(
["%ifarch %{power64}", "export ARCH=PPC64", "%endif"],
[True, False, True],
lambda kwd, exp: False,
lambda expr: (
"x86_64"
if expr == "%{_target_cpu}"
else "ppc64 ppc64p7 ppc64le" if expr == "%{power64}" else expr
),
),
(
[
Expand All @@ -33,7 +41,11 @@
"%endif",
],
[True, False, True, True, True, False, True],
lambda kwd, exp: "rhel" in exp,
lambda expr: (
"0"
if expr == "%{expr:0%{?fedora} > 38}"
else "1" if expr == "%{expr:0%{?rhel} > 8}" else expr
),
),
(
[
Expand Down Expand Up @@ -64,17 +76,19 @@
True,
True,
],
lambda kwd, exp: "fedora" in exp,
lambda expr: (
"0"
if expr.startswith("%{expr:%{with_")
else "1" if expr == "%{expr:0%{?fedora}}" else expr
),
),
],
)
def test_process_conditions(lines, validity, resolve_func):
def resolve_expression(kwd, exp, *_, **__):
return resolve_func(kwd, exp)
def test_process_conditions(lines, validity, expand_func):
def expand(expr):
return expand_func(expr)

flexmock(specfile.conditions).should_receive("resolve_expression").replace_with(
resolve_expression
)
flexmock(Macros).should_receive("expand").replace_with(expand)
processed_lines, processed_validity = zip(*process_conditions(lines))
assert list(processed_lines) == lines
assert list(processed_validity) == validity
Expand Down
Loading