From 921672a5d131d9a018744bfc51bdad570ae0c8ff Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Tue, 20 Aug 2024 18:35:02 +0200 Subject: [PATCH] generator: simplify cargo flag formatter and improve test coverage --- rust2rpm/generator.py | 9 ++------- rust2rpm/tests/test_generator.py | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/rust2rpm/generator.py b/rust2rpm/generator.py index 91e2197..600bad2 100644 --- a/rust2rpm/generator.py +++ b/rust2rpm/generator.py @@ -114,13 +114,8 @@ def cargo_args_from_flags( else: cargo_args_list = [] - if len(feature_flags.features) > 0: - cargo_args_list.append(f"-f {','.join(feature_flags.features)}") - - if cargo_args_list: - cargo_args = " " + " ".join(cargo_args_list) - else: - cargo_args = "" + cargo_args_list.append(f"-f {','.join(feature_flags.features)}") + cargo_args = " " + " ".join(cargo_args_list) elif feature_flags.no_default_features: cargo_args = " -n" diff --git a/rust2rpm/tests/test_generator.py b/rust2rpm/tests/test_generator.py index 26bc1fb..48f9eb4 100644 --- a/rust2rpm/tests/test_generator.py +++ b/rust2rpm/tests/test_generator.py @@ -5,11 +5,11 @@ import re import time from typing import Optional, cast -from cargo2rpm.metadata import Metadata +from cargo2rpm.metadata import Metadata, FeatureFlags import pytest from rust2rpm.conf import TomlConf -from rust2rpm.generator import spec_render_crate, spec_render_project, spec_render_workspace +from rust2rpm.generator import spec_render_crate, spec_render_project, spec_render_workspace, cargo_args_from_flags from rust2rpm.metadata import guess_main_package from rust2rpm.patching import preprocess_cargo_toml from rust2rpm.utils import package_name_suffixed @@ -283,3 +283,26 @@ def test_drop_foreign_dependencies(filename: str, features: set[str], expected: toml_after = after_path.read_text() assert patched == toml_after + + +@pytest.mark.parametrize( + "flags,required,enabled,expected", + [ + (FeatureFlags(), set(), set(), ""), + (FeatureFlags(), {"test"}, set(), " -f test"), + (FeatureFlags(features=["test"]), {"test"}, set(), " -f test"), + (FeatureFlags(features=["test"]), set(), {"test"}, " -f test"), + (FeatureFlags(), {"test"}, {"test"}, ""), + (FeatureFlags(), {"test1"}, {"test2"}, " -f test1"), + (FeatureFlags(no_default_features=True), {"test"}, set(), " -n -f test"), + (FeatureFlags(no_default_features=True, features=["test"]), {"test"}, set(), " -n -f test"), + (FeatureFlags(no_default_features=True, features=["test"]), set(), {"test"}, " -n -f test"), + (FeatureFlags(no_default_features=True), {"test"}, {"test"}, " -n"), + (FeatureFlags(all_features=True), set(), set(), " -a"), + (FeatureFlags(no_default_features=True), set(), set(), " -n"), + (FeatureFlags(no_default_features=True, features=["test1", "test2"]), set(), set(), " -n -f test1,test2"), + ], + ids=repr, +) +def test_cargo_args_from_flags(flags: FeatureFlags, required: set[str], enabled: set[str], expected: str): + assert cargo_args_from_flags(flags, required, enabled) == expected