From 004a505dc66edcbb296b0654484380f2c8077637 Mon Sep 17 00:00:00 2001 From: "Commentator2.0" Date: Tue, 3 Jun 2025 10:42:59 +0200 Subject: [PATCH] tests/functional2/nix: fix config serialization Currently, the typecheck for the config values is only done half-heartedly only checking if something is either a list or non-list item, but not checking what type the list items are this commit fixes the typecheck and adds test for proper serialization Change-Id: Ifd93842b19b1dd870bdb3af0c000243b4380e7aa --- tests/functional2/testlib/fixtures/nix.py | 11 +-- .../functional2/testlib/fixtures/test_nix.py | 72 +++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tests/functional2/testlib/fixtures/test_nix.py diff --git a/tests/functional2/testlib/fixtures/nix.py b/tests/functional2/testlib/fixtures/nix.py index 2ebe90df3..e0609f1d4 100644 --- a/tests/functional2/testlib/fixtures/nix.py +++ b/tests/functional2/testlib/fixtures/nix.py @@ -8,6 +8,7 @@ from collections.abc import Callable, Generator import pytest from functional2.testlib.commands import CommandResult, Command +from functional2.testlib.utils import is_value_of_type @dataclasses.dataclass @@ -43,11 +44,13 @@ class NixSettings: config = "" def serialise(value: Any) -> str: - if type(value) in {str, int}: + # TODO(Commentator2.0): why exactly are ints supported? + if is_value_of_type(value, set[str | int]): + return " ".join(serialise(e) for e in value) + if is_value_of_type(value, str | int): return str(value) - if type(value) in {list, set}: - return " ".join(str(e) for e in value) - msg = f"Value is unsupported in nix config: {value!r}" + + msg = f"Value is unsupported in nix config: {value!r}, must bei either `str|int` or `set[str|int]`" raise ValueError(msg) def field_may(name: str, value: Any, serializer: Callable[[Any], str] = serialise): diff --git a/tests/functional2/testlib/fixtures/test_nix.py b/tests/functional2/testlib/fixtures/test_nix.py new file mode 100644 index 000000000..f4aa36f0b --- /dev/null +++ b/tests/functional2/testlib/fixtures/test_nix.py @@ -0,0 +1,72 @@ +from pathlib import Path + +import pytest + +from functional2.testlib.fixtures.nix import NixSettings + + +def test_nix_settings_serializes_xf(): + settings = NixSettings(nix_store_dir=Path("/store/nix")) + settings.feature("a", "b") + + expected = "experimental-features = a b\n" + assert settings.to_config() == expected + + +def test_nix_settings_serializes_store(): + settings = NixSettings(nix_store_dir=Path("/store/nix")) + settings.store = "some/path" + + expected = "store = some/path\n" + assert settings.to_config() == expected + + +def test_nix_settings_serializes_both(): + settings = NixSettings(nix_store_dir=Path("/store/nix")) + settings.feature("a", "b") + settings.store = "some/path" + + expected = "experimental-features = a b\nstore = some/path\n" + assert settings.to_config() == expected + + +def test_nix_settings_ser_fails_bad_top_level_type(): + settings = NixSettings(nix_store_dir=Path("/store/nix")) + settings.experimental_features = {"a": "b"} # type: ignore we are testing the types here + + with pytest.raises(ValueError, match="Value is unsupported in nix config: {'a': 'b'}"): + settings.to_config() + + +def test_nix_settings_ser_fails_bad_sub_type(): + settings = NixSettings(nix_store_dir=Path("/store/nix")) + settings.experimental_features = [["a", "b"], "c"] # type: ignore we are testing the types here + + with pytest.raises(ValueError, match="Value is unsupported in nix config: .+"): + settings.to_config() + + +def test_nix_settings_fails_without_store_and_store_dir(): + settings = NixSettings() + + with pytest.raises( + AssertionError, + match="Failing to set either nix_store_dir or store will cause accidental use of the system store.", + ): + settings.to_config() + + +def test_nix_settings_to_env_overlay_no_store_dir(): + settings = NixSettings() + settings.store = "some/path" + + expected = {"NIX_CONFIG": "store = some/path\n"} + assert settings.to_env_overlay() == expected + + +def test_nix_settings_to_env_overlay_store_dir(): + settings = NixSettings() + settings.nix_store_dir = Path("/some/path") + + expected = {"NIX_CONFIG": "", "NIX_STORE_DIR": "/some/path"} + assert settings.to_env_overlay() == expected