From 25f53b755caf6e3fdd2b2ce44bdf2f187804dd4d Mon Sep 17 00:00:00 2001 From: rootile Date: Tue, 16 Jun 2026 19:38:30 +0200 Subject: [PATCH] tests: migrate config.sh Change-Id: Ia37a84e0036dd52af112a359817c709a15aa8c90 --- tests/functional/config.sh | 65 ----------------- tests/functional/meson.build | 1 - tests/functional2/config/__init__.py | 0 tests/functional2/config/test_config_dir.py | 74 ++++++++++++++++++++ tests/functional2/config/test_config_show.py | 31 ++++++++ tests/functional2/testlib/fixtures/nix.py | 12 +++- 6 files changed, 115 insertions(+), 68 deletions(-) delete mode 100644 tests/functional/config.sh create mode 100644 tests/functional2/config/__init__.py create mode 100644 tests/functional2/config/test_config_dir.py create mode 100644 tests/functional2/config/test_config_show.py diff --git a/tests/functional/config.sh b/tests/functional/config.sh deleted file mode 100644 index 1feb381f7..000000000 --- a/tests/functional/config.sh +++ /dev/null @@ -1,65 +0,0 @@ -source common.sh - -# Isolate the home for this test. -# Other tests (e.g. flake registry tests) could be writing to $HOME in parallel. -export HOME=$TEST_ROOT/userhome - -# Test that using XDG_CONFIG_HOME works -# Assert the config folder didn't exist initially. -[ ! -e "$HOME/.config" ] -# Without XDG_CONFIG_HOME, creates $HOME/.config -unset XDG_CONFIG_HOME -# Run against the nix registry to create the config dir -# (Tip: this relies on removing non-existent entries being a no-op!) -nix registry remove userhome-without-xdg -# Verifies it created it -[ -e "$HOME/.config" ] -# Remove the directory it created -rm -rf "$HOME/.config" -# Run the same test, but with XDG_CONFIG_HOME -export XDG_CONFIG_HOME=$TEST_ROOT/confighome -# Assert the XDG_CONFIG_HOME/nix path does not exist yet. -[ ! -e "$TEST_ROOT/confighome/nix" ] -nix registry remove userhome-with-xdg -# Verifies the confighome path has been created -[ -e "$TEST_ROOT/confighome/nix" ] -# Assert the .config folder hasn't been created. -[ ! -e "$HOME/.config" ] - -# Test that files are loaded from XDG by default -export XDG_CONFIG_HOME=$TEST_ROOT/confighome -export XDG_CONFIG_DIRS=$TEST_ROOT/dir1:$TEST_ROOT/dir2 -files=$(nix-build --verbose --version | grep "User config" | cut -d ':' -f2- | xargs) -[[ $files == "$TEST_ROOT/confighome/nix/nix.conf:$TEST_ROOT/dir1/nix/nix.conf:$TEST_ROOT/dir2/nix/nix.conf" ]] - -# Test that setting NIX_USER_CONF_FILES overrides all the default user config files -export NIX_USER_CONF_FILES=$TEST_ROOT/file1.conf:$TEST_ROOT/file2.conf -files=$(nix-build --verbose --version | grep "User config" | cut -d ':' -f2- | xargs) -[[ $files == "$TEST_ROOT/file1.conf:$TEST_ROOT/file2.conf" ]] - -# Test that it's possible to load the config from a custom location -here=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")") -export NIX_USER_CONF_FILES=$here/config/nix-with-substituters.conf -var=$(nix config show | grep '^substituters =' | cut -d '=' -f 2 | xargs) -[[ $var == https://example.com ]] - -# Test that it's possible to load config from the environment -prev=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs) -export NIX_CONFIG="cores = 4242"$'\n'"experimental-features = nix-command flakes" -exp_cores=$(nix config show | grep '^cores' | cut -d '=' -f 2 | xargs) -exp_features=$(nix config show | grep '^experimental-features' | cut -d '=' -f 2 | xargs) -[[ $prev != $exp_cores ]] -[[ $exp_cores == "4242" ]] -[[ $exp_features == "flakes nix-command" ]] - -# Test that it's possible to retrieve a single setting's value -val=$(nix config show | grep '^warn-dirty' | cut -d '=' -f 2 | xargs) -val2=$(nix config show warn-dirty) -[[ $val == $val2 ]] - -# Regression test for `[extra-]trusted-users` (daemonAuthorizationSettings) being ignored. -# https://git.lix.systems/lix-project/lix/issues/1183 -val="$(NIX_CONFIG=$'trusted-users = as-configured' nix config show trusted-users)" -[[ "$val" == "as-configured" ]] -val="$(NIX_CONFIG=$'trusted-users = as-configured\nextra-trusted-users = extra' nix config show trusted-users)" -[[ "$val" == "as-configured extra" ]] diff --git a/tests/functional/meson.build b/tests/functional/meson.build index 14ca375fc..0a3d53a7f 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -75,7 +75,6 @@ functional_tests_scripts = [ 'nar-access.sh', 'repl.sh', 'logging.sh', - 'config.sh', 'filter-source.sh', 'linux-sandbox.sh', 'supplementary-groups.sh', diff --git a/tests/functional2/config/__init__.py b/tests/functional2/config/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/functional2/config/test_config_dir.py b/tests/functional2/config/test_config_dir.py new file mode 100644 index 000000000..e32dfa71e --- /dev/null +++ b/tests/functional2/config/test_config_dir.py @@ -0,0 +1,74 @@ +from testlib.fixtures.file_helper import File +from testlib.fixtures.file_helper import with_files +from testlib.fixtures.nix import Nix +from pathlib import Path +import pytest + +pytestmark = pytest.mark.no_daemon + + +def test_home_dot_config_no_xdg(nix: Nix, files: Path): + # Test that using XDG_CONFIG_HOME works + # Assert the config folder didn't exist initially. + config_path = files / ".config" + assert not config_path.exists() + + # Without XDG_CONFIG_HOME, creates $HOME/.config + # Run against the nix registry to create the config dir + # (Tip: this relies on removing non-existent entries being a no-op!) + nix.nix(["registry", "remove", "userhome-without-xdg"], flake=True).run().ok() + assert config_path.exists() + + +def test_config_xdg(nix: Nix, files: Path): + config_path = files / ".config" + xdg_config_path = files / ".xdg-config" + + nix.env["XDG_CONFIG_HOME"] = str(xdg_config_path) + + assert not config_path.exists() + assert not xdg_config_path.exists() + + nix.nix(["registry", "remove", "userhome-without-xdg"], flake=True).run().ok() + + assert not config_path.exists() + assert xdg_config_path.exists() + assert (xdg_config_path / "nix").exists() + + +def test_load_files_xdg(nix: Nix, files: Path): + # Test that files are loaded from XDG by default + xdg_config_path = files / ".xdg-config" + nix.env["XDG_CONFIG_HOME"] = str(xdg_config_path) + nix.env["XDG_CONFIG_DIRS"] = f"{files}/dir1:{files}/dir2" + + res = nix.nix_build(["-v", "--version"]).run().ok() + clean = res.stdout_plain.replace(str(files.parent), "/PWD") + assert ( + "User configuration files: /PWD/test-home/.xdg-config/nix/nix.conf:/PWD/test-home/dir1/nix/nix.conf:/PWD/test-home/dir2/nix/nix.conf" + in clean + ) + + +def test_user_conf_overrides(nix: Nix, files: Path): + conf_files = f"{files}/file1.conf:{files}/file2.conf" + nix.env["NIX_USER_CONF_FILES"] = conf_files + res = nix.nix_build(["-v", "--version"]).run().ok() + clean = res.stdout_plain.replace(str(files.parent), "/PWD") + assert "User configuration files: /PWD/test-home/file1.conf:/PWD/test-home/file2.conf" in clean + + +@with_files( + { + "my-config": { + "my-nix.conf": File( + "experimental-features = nix-command\nsubstituters = https://example.com" + ) + } + } +) +def test_conf_load_custom_location(nix: Nix, files: Path): + nix.env["NIX_USER_CONF_FILES"] = f"{files}/my-config/my-nix.conf" + nix.settings.disabled = True + res = nix.nix(["config", "show"]).run().ok() + assert "substituters = https://example.com" in res.stdout_plain diff --git a/tests/functional2/config/test_config_show.py b/tests/functional2/config/test_config_show.py new file mode 100644 index 000000000..a0db6e78b --- /dev/null +++ b/tests/functional2/config/test_config_show.py @@ -0,0 +1,31 @@ +from testlib.fixtures.nix import Nix +import pytest + +pytestmark = pytest.mark.no_daemon + + +def test_config_env_var(nix: Nix): + nix.settings.disabled = True + nix.env["NIX_CONFIG"] = "cores = 4242\nexperimental-features = nix-command flakes" + res = nix.nix(["config", "show"]).run().ok() + assert "cores = 4242" in res.stdout_plain + assert "experimental-features = flakes nix-command" in res.stdout_plain + + +def test_config_show_single_value(nix: Nix): + res = nix.nix(["config", "show", "warn-dirty"], flake=True).run().ok() + assert res.stdout_plain == "true" + + +def test_trusted_user_ignored(nix: Nix): + """ + regression test for https://git.lix.systems/lix-project/lix/issues/1183 + """ + nix.settings.disabled = True + nix.env["NIX_CONFIG"] = "experimental-features = nix-command\ntrusted-users = as-configured" + res = nix.nix(["config", "show", "trusted-users"]).run().ok() + assert res.stdout_plain == "as-configured" + + nix.env["NIX_CONFIG"] += "\nextra-trusted-users = extra" + res = nix.nix(["config", "show", "trusted-users"]).run().ok() + assert res.stdout_plain == "as-configured extra" diff --git a/tests/functional2/testlib/fixtures/nix.py b/tests/functional2/testlib/fixtures/nix.py index fead593c4..3192838ec 100644 --- a/tests/functional2/testlib/fixtures/nix.py +++ b/tests/functional2/testlib/fixtures/nix.py @@ -87,14 +87,19 @@ class NixSettings: "extra-experimental-features": [], "extra-deprecated-features": [], } + self.disabled = False + """ + This is *not* a nix specific setting, but disables the application of the configured settings. + Use this, if you need to test config file discovery or similar. + """ def __getattr__(self, attr: str) -> _NixSettingValue: - if attr.startswith("__"): + if attr.startswith("__") or attr == "disabled": return super().__getattr__(attr) return self._settings[attr.replace("_", "-")] def __setattr__(self, attr: str, value: _NixSettingValue): - if attr == "_settings": + if attr in ("_settings", "disabled"): super().__setattr__(attr, value) else: self._settings[attr.replace("_", "-")] = value @@ -136,6 +141,7 @@ class NixSettings: """ new_settings = NixSettings() new_settings._settings = copy.deepcopy(self._settings) + new_settings.disabled = self.disabled new_settings.update(args, **kwargs) return new_settings @@ -155,6 +161,8 @@ class NixSettings: return config def to_env_overlay(self, env: ManagedEnv) -> None: + if self.disabled: + return cfg = self.to_config(env) (env.dirs.nix_conf_dir / "nix.conf").write_text(cfg) env.set_env("NIX_CONFIG", cfg)