From f2df828138846ba168ce0ba2e7eecc9fcc0d6cba Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Thu, 13 Feb 2025 21:39:04 +0000 Subject: [PATCH] nixos-rebuild-ng: improve some tests --- .../nixos-rebuild-ng/src/tests/test_main.py | 60 +++++++++---------- .../nixos-rebuild-ng/src/tests/test_models.py | 1 + .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 2 +- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py index 249d7b059774..2e9363e2869a 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py @@ -136,34 +136,34 @@ def test_reexec(mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch) - mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": True}) - assert mock_build.call_args_list == [ - call( - "config.system.build.nixos-rebuild", - nr.models.BuildAttr(ANY, ANY), - {"build": True, "no_out_link": True}, - ) - ] + mock_build.assert_has_calls( + [ + call( + "config.system.build.nixos-rebuild", + nr.models.BuildAttr(ANY, ANY), + {"build": True, "no_out_link": True}, + ) + ] + ) # do not exec if there is no new version - assert mock_execve.call_args_list == [] + mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully - assert mock_execve.call_args_list == [ - call( - Path("/path/new/bin/nixos-rebuild-ng"), - ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"], - {"_NIXOS_REBUILD_REEXEC": "1"}, - ) - ] + mock_execve.assert_called_once_with( + Path("/path/new/bin/nixos-rebuild-ng"), + ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"], + {"_NIXOS_REBUILD_REEXEC": "1"}, + ) mock_execve.reset_mock() mock_execve.side_effect = [OSError("BOOM"), None] nr.reexec(argv, args, {}, {}) # exec in the previous version if the new version fails - assert mock_execve.call_args == call( + mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, @@ -182,34 +182,30 @@ def test_reexec_flake( mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": True}) - assert mock_build.call_args_list == [ - call( - "config.system.build.nixos-rebuild", - nr.models.Flake(ANY, ANY), - {"flake": True, "no_link": True}, - ) - ] + mock_build.assert_called_once_with( + "config.system.build.nixos-rebuild", + nr.models.Flake(ANY, ANY), + {"flake": True, "no_link": True}, + ) # do not exec if there is no new version - assert mock_execve.call_args_list == [] + mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully - assert mock_execve.call_args_list == [ - call( - Path("/path/new/bin/nixos-rebuild-ng"), - ["/path/bin/nixos-rebuild-ng", "switch", "--flake"], - {"_NIXOS_REBUILD_REEXEC": "1"}, - ) - ] + mock_execve.assert_called_once_with( + Path("/path/new/bin/nixos-rebuild-ng"), + ["/path/bin/nixos-rebuild-ng", "switch", "--flake"], + {"_NIXOS_REBUILD_REEXEC": "1"}, + ) mock_execve.reset_mock() mock_execve.side_effect = [OSError("BOOM"), None] nr.reexec(argv, args, {}, {}) # exec in the previous version if the new version fails - assert mock_execve.call_args == call( + mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_models.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_models.py index 3c6859015489..cb318192fc2e 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_models.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_models.py @@ -167,6 +167,7 @@ def test_profile_from_arg(mock_mkdir: Mock) -> None: "system", Path("/nix/var/nix/profiles/system"), ) + mock_mkdir.assert_not_called() assert m.Profile.from_arg("something") == m.Profile( "something", 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 ea937c06fcd3..af8c0bdfd6a7 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 @@ -762,7 +762,7 @@ def test_switch_to_configuration(mock_run: Mock, monkeypatch: MonkeyPatch) -> No def test_upgrade_channels(mock_is_dir: Mock, mock_glob: Mock) -> None: with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.upgrade_channels(False) - mock_run.assert_called_with(["nix-channel", "--update", "nixos"], check=False) + mock_run.assert_called_once_with(["nix-channel", "--update", "nixos"], check=False) with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.upgrade_channels(True)