nixos-rebuild-ng: fix test_upgrade_channels in aarch64-darwin (#512154)

This commit is contained in:
Thiago Kenji Okada
2026-04-22 08:20:58 +00:00
committed by GitHub
2 changed files with 49 additions and 28 deletions
@@ -726,7 +726,11 @@ def switch_to_configuration(
)
def upgrade_channels(all_channels: bool = False, sudo: bool = False) -> None:
def upgrade_channels(
all_channels: bool = False,
sudo: bool = False,
channels_dir: Path = Path("/nix/var/nix/profiles/per-user/root/channels/"),
) -> None:
"""Upgrade channels for classic Nix.
It will either upgrade just the `nixos` channel (including any channel
@@ -739,7 +743,7 @@ def upgrade_channels(all_channels: bool = False, sudo: bool = False) -> None:
)
channel_updated = False
for channel_path in Path("/nix/var/nix/profiles/per-user/root/channels/").glob("*"):
for channel_path in channels_dir.glob("*"):
if channel_path.is_dir() and (
all_channels
or channel_path.name == "nixos"
@@ -882,44 +882,61 @@ def test_switch_to_configuration_with_systemd_run(
)
@patch(
"pathlib.Path.glob",
autospec=True,
return_value=[
Path("/nix/var/nix/profiles/per-user/root/channels/nixos"),
Path("/nix/var/nix/profiles/per-user/root/channels/nixos-hardware"),
Path("/nix/var/nix/profiles/per-user/root/channels/home-manager"),
],
)
@patch("pathlib.Path.is_dir", autospec=True, return_value=True)
@patch("os.geteuid", autospec=True, return_value=1000)
@patch(get_qualified_name(n.run_wrapper, n), autospec=True)
def test_upgrade_channels(
mock_run: Mock,
mock_geteuid: Mock,
mock_is_dir: Mock,
mock_glob: Mock,
) -> None:
def test_upgrade_channels(mock_run: Mock, mock_geteuid: Mock, tmpdir: Path) -> None:
tmp_path = Path(tmpdir)
with pytest.raises(m.NixOSRebuildError) as e:
n.upgrade_channels(all_channels=False, sudo=False)
n.upgrade_channels(all_channels=False, sudo=False, channels_dir=tmp_path)
assert str(e.value) == (
"error: if you pass the '--upgrade' or '--upgrade-all' flag, you must "
"also pass '--sudo' or run the command as root (e.g., with sudo)"
)
n.upgrade_channels(all_channels=False, sudo=True)
mock_run.assert_called_once_with(
["nix-channel", "--update", "nixos"], check=False, sudo=True
)
(tmp_path / "nixos").mkdir()
(tmp_path / "nixos-hardware").mkdir()
(tmp_path / "nixos-hardware" / ".update-on-nixos-rebuild").touch()
(tmp_path / "home-manager").mkdir()
mock_geteuid.return_value = 0
n.upgrade_channels(all_channels=True, sudo=False)
# should work because we are passing sudo=True even with os.geteuid == 1000
n.upgrade_channels(all_channels=False, sudo=True, channels_dir=tmp_path)
mock_run.assert_has_calls(
[
call(["nix-channel", "--update", "nixos"], check=False, sudo=False),
call(
["nix-channel", "--update", "nixos-hardware"], check=False, sudo=False
["nix-channel", "--update", "nixos-hardware"],
check=False,
sudo=True,
),
call(
["nix-channel", "--update", "nixos"],
check=False,
sudo=True,
),
]
)
mock_run.reset_mock()
# root check
mock_geteuid.return_value = 0
n.upgrade_channels(all_channels=True, sudo=False, channels_dir=tmp_path)
mock_run.assert_has_calls(
[
call(
["nix-channel", "--update", "home-manager"],
check=False,
sudo=False,
),
call(
["nix-channel", "--update", "nixos-hardware"],
check=False,
sudo=False,
),
call(
["nix-channel", "--update", "nixos"],
check=False,
sudo=False,
),
call(["nix-channel", "--update", "home-manager"], check=False, sudo=False),
]
)