generator: simplify cargo flag formatter and improve test coverage

This commit is contained in:
Fabio Valentini 2024-08-20 18:35:02 +02:00
parent 939ab5eb55
commit 921672a5d1
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
2 changed files with 27 additions and 9 deletions

View file

@ -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"

View file

@ -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