diff --git a/specfile/conditions.py b/specfile/conditions.py index 85b3285..77f14ab 100644 --- a/specfile/conditions.py +++ b/specfile/conditions.py @@ -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 diff --git a/tests/unit/test_conditions.py b/tests/unit/test_conditions.py index 92dbeb9..68719e6 100644 --- a/tests/unit/test_conditions.py +++ b/tests/unit/test_conditions.py @@ -53,6 +53,8 @@ "BuildRequires: libX11-devel", "%if 0%{?fedora}", "Requires: desktop-file-utils", + "%else", + "Requires: gnome-desktop", "%endif", "BuildRequires: libXext-devel", "%else", @@ -69,6 +71,8 @@ False, False, False, + False, + False, True, True, False, @@ -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):