From 2c7f0d79701976cc68481e6ed23e245e860da2b1 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 21 Apr 2026 19:08:54 +0100 Subject: [PATCH 1/2] nixos-rebuild-ng: mock pathlib.Path.exists in test_upgrade_channels This should avoid make it try to call the real path to see if it exists. Seems to be an issue in a particular setup in aarch64-darwin at least, but I can't reproduce. Fix: #511860. --- pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py index f8c6d75c5290..8fb4d5ef166e 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py @@ -892,11 +892,13 @@ def test_switch_to_configuration_with_systemd_run( ], ) @patch("pathlib.Path.is_dir", autospec=True, return_value=True) +@patch("pathlib.Path.exists", autospec=True, return_value=False) @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_exists: Mock, mock_is_dir: Mock, mock_glob: Mock, ) -> None: @@ -912,7 +914,11 @@ def test_upgrade_channels( ["nix-channel", "--update", "nixos"], check=False, sudo=True ) + # root check mock_geteuid.return_value = 0 + # (channel_path / ".update-on-nixos-rebuild").exists() + mock_exists.return_value = True + n.upgrade_channels(all_channels=True, sudo=False) mock_run.assert_has_calls( [ From 4cec3122850f3aa592b04648c1088268bc6144a6 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Tue, 21 Apr 2026 20:01:49 +0100 Subject: [PATCH 2/2] nixos-rebuild-ng: refactor test_upgrade_channels --- .../nixos-rebuild-ng/src/nixos_rebuild/nix.py | 8 ++- .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 67 +++++++++++-------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py index c825320eae74..c6b5b4d63c45 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py @@ -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" diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py index 8fb4d5ef166e..bccb2f196970 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py @@ -882,50 +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("pathlib.Path.exists", autospec=True, return_value=False) @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_exists: 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() + + # 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-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 - # (channel_path / ".update-on-nixos-rebuild").exists() - mock_exists.return_value = True - n.upgrade_channels(all_channels=True, sudo=False) + n.upgrade_channels(all_channels=True, sudo=False, 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", "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), ] )