From dca5a5db4f0143711d244d3c7ae472d442bd91c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Mon, 29 Jul 2024 09:14:51 +0200 Subject: [PATCH 1/2] Fix parsing of conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After 157a62b9 conditional expressions are parsed after macro expansion, which means that the actual expression can be empty. Account for that. Signed-off-by: Nikola Forró --- specfile/conditions.py | 19 ++++++++++--------- tests/unit/test_conditions.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/specfile/conditions.py b/specfile/conditions.py index 85b3285..3ae88d8 100644 --- a/specfile/conditions.py +++ b/specfile/conditions.py @@ -137,18 +137,19 @@ def expand(s): 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..4046c1e 100644 --- a/tests/unit/test_conditions.py +++ b/tests/unit/test_conditions.py @@ -82,6 +82,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): From 0d28c5fc19c0646768e6df2f48847942c8d5b849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Mon, 29 Jul 2024 10:20:14 +0200 Subject: [PATCH 2/2] Make sure every line in a false branch of a condition is invalid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nikola Forró --- specfile/conditions.py | 3 ++- tests/unit/test_conditions.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/specfile/conditions.py b/specfile/conditions.py index 3ae88d8..77f14ab 100644 --- a/specfile/conditions.py +++ b/specfile/conditions.py @@ -134,7 +134,8 @@ 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])) if keyword.startswith("%if") or keyword.startswith("%elif"): diff --git a/tests/unit/test_conditions.py b/tests/unit/test_conditions.py index 4046c1e..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,