Loading pkgs/by-name/ni/nixos-rebuild-ng/package.nix +3 −0 Original line number Diff line number Diff line Loading @@ -89,6 +89,9 @@ python3Packages.buildPythonApplication rec { ps: with ps; [ mypy pytest # this is to help development (e.g.: better diffs) inside devShell # only, do not use its helpers like `mocker` pytest-mock ruff ] ); Loading pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py +601 −481 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ from typing import Any from unittest.mock import ANY, Mock, call, patch import pytest from pytest import MonkeyPatch import nixos_rebuild as nr Loading Loading @@ -125,6 +126,92 @@ def test_parse_args() -> None: ] @patch.dict(nr.os.environ, {}, clear=True) @patch(get_qualified_name(nr.os.execve, nr.os), autospec=True) @patch(get_qualified_name(nr.nix.build), autospec=True) def test_reexec(mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(nr, "EXECUTABLE", "nixos-rebuild-ng") argv = ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"] args, _ = nr.parse_args(argv) mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": 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 mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully 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 mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, ) @patch.dict(nr.os.environ, {}, clear=True) @patch(get_qualified_name(nr.os.execve, nr.os), autospec=True) @patch(get_qualified_name(nr.nix.build_flake), autospec=True) def test_reexec_flake( mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch ) -> None: monkeypatch.setattr(nr, "EXECUTABLE", "nixos-rebuild-ng") argv = ["/path/bin/nixos-rebuild-ng", "switch", "--flake"] args, _ = nr.parse_args(argv) mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": 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 mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully 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 mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, ) @patch.dict(nr.process.os.environ, {}, clear=True) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: Loading @@ -147,7 +234,9 @@ def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: nr.execute(["nixos-rebuild", "boot", "--no-flake", "-vvv", "--no-reexec"]) assert mock_run.call_args_list == [ assert mock_run.call_count == 6 mock_run.assert_has_calls( [ call( ["nix-instantiate", "--find-file", "nixpkgs", "-vvv"], stdout=PIPE, Loading Loading @@ -195,6 +284,7 @@ def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: **(DEFAULT_RUN_KWARGS | {"env": {"NIXOS_INSTALL_BOOTLOADER": "0"}}), ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -224,7 +314,9 @@ def test_execute_nix_build_vm(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 1 mock_run.assert_has_calls( [ call( [ "nix-build", Loading @@ -241,6 +333,7 @@ def test_execute_nix_build_vm(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ) ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -279,7 +372,9 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -307,6 +402,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -340,7 +436,9 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 3 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -377,6 +475,7 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None: **(DEFAULT_RUN_KWARGS | {"env": {"NIXOS_INSTALL_BOOTLOADER": "1"}}), ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -430,7 +529,9 @@ def test_execute_nix_switch_build_target_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 10 mock_run.assert_has_calls( [ call( [ "nix-instantiate", Loading Loading @@ -570,6 +671,7 @@ def test_execute_nix_switch_build_target_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -604,7 +706,9 @@ def test_execute_nix_switch_flake_target_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 4 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -656,6 +760,7 @@ def test_execute_nix_switch_flake_target_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -691,7 +796,9 @@ def test_execute_nix_switch_flake_build_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 6 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -755,6 +862,7 @@ def test_execute_nix_switch_flake_build_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading Loading @@ -783,7 +891,9 @@ def test_execute_switch_rollback(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 4 mock_run.assert_has_calls( [ call( ["nix-instantiate", "--find-file", "nixpkgs"], check=False, Loading Loading @@ -822,6 +932,7 @@ def test_execute_switch_rollback(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading @@ -835,7 +946,9 @@ def test_execute_build(mock_run: Mock, tmp_path: Path) -> None: nr.execute(["nixos-rebuild", "build", "--no-flake", "--no-reexec"]) assert mock_run.call_args_list == [ assert mock_run.call_count == 1 mock_run.assert_has_calls( [ call( [ "nix-build", Loading @@ -848,6 +961,7 @@ def test_execute_build(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ) ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading @@ -867,7 +981,9 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None: ["nixos-rebuild", "test", "--flake", "github:user/repo#hostname", "--no-reexec"] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix", Loading @@ -887,6 +1003,7 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading Loading @@ -917,7 +1034,9 @@ def test_execute_test_rollback( ["nixos-rebuild", "test", "--rollback", "--profile-name", "foo", "--no-reexec"] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix-env", Loading @@ -940,3 +1059,4 @@ def test_execute_test_rollback( **DEFAULT_RUN_KWARGS, ), ] ) pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +146 −149 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ def test_build(mock_run: Mock) -> None: m.BuildAttr("<nixpkgs/nixos>", None), {"nix_flag": "foo"}, ) == Path("/path/to/file") assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-build", "<nixpkgs/nixos>", Loading @@ -38,17 +38,13 @@ def test_build(mock_run: Mock) -> None: stdout=PIPE, ) mock_run.reset_mock() assert n.build( "config.system.build.attr", m.BuildAttr(Path("file"), "preAttr") ) == Path("/path/to/file") assert mock_run.call_args_list == [ call( mock_run.assert_called_with( ["nix-build", Path("file"), "--attr", "preAttr.config.system.build.attr"], stdout=PIPE, ) ] @patch( Loading @@ -65,7 +61,7 @@ def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> flake, {"no_link": True, "nix_flag": "foo"}, ) == Path("/path/to/file") assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "--extra-experimental-features", Loading Loading @@ -114,7 +110,9 @@ def test_build_remote( instantiate_flags={"inst": True}, copy_flags={"copy": True}, ) == Path("/path/to/config") assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( [ "nix-instantiate", Loading @@ -135,7 +133,9 @@ def test_build_remote( "user@host", Path("/path/to/file"), ], extra_env={"NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"])}, extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"]) }, ), call( ["mktemp", "-d", "-t", "nixos-rebuild.XXXXX"], Loading @@ -161,6 +161,7 @@ def test_build_remote( ), call(["rm", "-rf", Path("/tmp/tmpdir")], remote=build_host, check=False), ] ) @patch( Loading @@ -184,7 +185,8 @@ def test_build_remote_flake( copy_flags={"copy": True}, flake_build_flags={"build": True}, ) == Path("/path/to/file") assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( [ "nix", Loading @@ -205,7 +207,9 @@ def test_build_remote_flake( "user@host", Path("/path/to/file"), ], extra_env={"NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"])}, extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"]) }, ), call( [ Loading @@ -221,6 +225,7 @@ def test_build_remote_flake( stdout=PIPE, ), ] ) def test_copy_closure(monkeypatch: MonkeyPatch) -> None: Loading @@ -233,7 +238,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: build_host = m.Remote("user@build.host", [], None) with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-copy-closure", "--to", "user@target.host", closure], extra_env={"NIX_SSHOPTS": " ".join(p.SSH_DEFAULT_OPTS)}, ) Loading @@ -241,7 +246,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("NIX_SSHOPTS", "--ssh build-opt") with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, None, build_host, {"copy_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-copy-closure", "--copy-flag", "--from", "user@build.host", closure], extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh build-opt"]) Loading @@ -255,7 +260,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: } with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host, build_host, {"copy_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "copy", Loading @@ -272,7 +277,8 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(n, "WITH_NIX_2_18", False) with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host, build_host) assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( ["nix-copy-closure", "--from", "user@build.host", closure], extra_env=extra_env, Loading @@ -282,6 +288,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: extra_env=extra_env, ), ] ) @patch(get_qualified_name(n.run_wrapper, n), autospec=True) Loading @@ -289,7 +296,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None: # Flake flake = m.Flake.parse(f"{tmpdir}#attr") n.edit(flake, {"commit_lock_file": True}) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "--extra-experimental-features", Loading @@ -311,7 +318,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None: mp.setenv("EDITOR", "editor") n.edit(None) assert mock_run.call_args == call(["editor", default_nix], check=False) mock_run.assert_called_with(["editor", default_nix], check=False) @patch( Loading @@ -334,7 +341,7 @@ def test_get_build_image_variants(mock_run: Mock, tmp_path: Path) -> None: "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-instantiate", "--eval", Loading @@ -352,14 +359,12 @@ def test_get_build_image_variants(mock_run: Mock, tmp_path: Path) -> None: stdout=PIPE, ) mock_run.reset_mock() build_attr = m.BuildAttr(Path(tmp_path), "preAttr") assert n.get_build_image_variants(build_attr, {"inst_flag": True}) == { "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-instantiate", "--eval", Loading Loading @@ -399,7 +404,7 @@ def test_get_build_image_variants_flake(mock_run: Mock) -> None: "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "eval", Loading @@ -424,7 +429,7 @@ def test_get_nixpkgs_rev() -> None: side_effect=[CompletedProcess([], 0, "")], ) as mock_run: assert n.get_nixpkgs_rev(path) is None assert mock_run.call_args == call( mock_run.assert_called_with( ["git", "-C", path, "rev-parse", "--short", "HEAD"], check=False, capture_output=True, Loading @@ -451,7 +456,7 @@ def test_get_nixpkgs_rev() -> None: ], ) as mock_run: assert n.get_nixpkgs_rev(path) == ".git.0f7c82403fd6" assert mock_run.call_args_list == expected_calls mock_run.assert_has_calls(expected_calls) with patch( get_qualified_name(n.run_wrapper, n), Loading @@ -462,7 +467,7 @@ def test_get_nixpkgs_rev() -> None: ], ) as mock_run: assert n.get_nixpkgs_rev(path) == ".git.0f7c82403fd6M" assert mock_run.call_args_list == expected_calls mock_run.assert_has_calls(expected_calls) def test_get_generations(tmp_path: Path) -> None: Loading Loading @@ -503,7 +508,7 @@ def test_get_generations_from_nix_env(tmp_path: Path) -> None: m.Generation(id=2083, current=False, timestamp="2024-11-07 22:59:41"), m.Generation(id=2084, current=True, timestamp="2024-11-07 23:54:17"), ] assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", path, "--list-generations"], stdout=PIPE, remote=None, Loading @@ -521,7 +526,7 @@ def test_get_generations_from_nix_env(tmp_path: Path) -> None: m.Generation(id=2083, current=False, timestamp="2024-11-07 22:59:41"), m.Generation(id=2084, current=True, timestamp="2024-11-07 23:54:17"), ] assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", path, "--list-generations"], stdout=PIPE, remote=remote, Loading Loading @@ -573,14 +578,12 @@ def test_list_generations(mock_get_generations: Mock, tmp_path: Path) -> None: @patch(get_qualified_name(n.run_wrapper, n), autospec=True) def test_repl(mock_run: Mock) -> None: n.repl("attr", m.BuildAttr("<nixpkgs/nixos>", None), {"nix_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix", "repl", "--file", "<nixpkgs/nixos>", "--nix-flag"] ) n.repl("attr", m.BuildAttr(Path("file.nix"), "myAttr")) assert mock_run.call_args == call( ["nix", "repl", "--file", Path("file.nix"), "myAttr"] ) mock_run.assert_called_with(["nix", "repl", "--file", Path("file.nix"), "myAttr"]) @patch(get_qualified_name(n.run_wrapper, n), autospec=True) Loading @@ -599,7 +602,7 @@ def test_rollback(mock_run: Mock, tmp_path: Path) -> None: profile = m.Profile("system", path) assert n.rollback(profile, None, False) == profile.path assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "--rollback", "-p", path], remote=None, sudo=False, Loading @@ -607,7 +610,7 @@ def test_rollback(mock_run: Mock, tmp_path: Path) -> None: target_host = m.Remote("user@localhost", [], None) assert n.rollback(profile, target_host, True) == profile.path assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "--rollback", "-p", path], remote=target_host, sudo=True, Loading @@ -619,10 +622,8 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: path.touch() profile = m.Profile("system", path) with patch( get_qualified_name(n.run_wrapper, n), autospec=True, return_value=CompletedProcess( with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: mock_run.return_value = CompletedProcess( [], 0, stdout=textwrap.dedent("""\ Loading @@ -630,13 +631,12 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: 2083 2024-11-07 22:59:41 2084 2024-11-07 23:54:17 (current) """), ), ) as mock_run: ) assert ( n.rollback_temporary_profile(m.Profile("system", path), None, False) == path.parent / "system-2083-link" ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-env", "-p", Loading @@ -653,7 +653,7 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: n.rollback_temporary_profile(m.Profile("foo", path), target_host, True) == path.parent / "foo-2083-link" ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-env", "-p", Loading @@ -665,11 +665,8 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: sudo=True, ) with patch( get_qualified_name(n.run_wrapper, n), autospec=True, return_value=CompletedProcess([], 0, stdout=""), ) as mock_run: with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: mock_run.return_value = CompletedProcess([], 0, stdout="") assert n.rollback_temporary_profile(profile, None, False) is None Loading @@ -684,7 +681,7 @@ def test_set_profile(mock_run: Mock) -> None: sudo=False, ) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", profile_path, "--set", config_path], remote=None, sudo=False, Loading @@ -707,7 +704,7 @@ def test_switch_to_configuration(mock_run: Mock, monkeypatch: MonkeyPatch) -> No specialisation=None, install_bootloader=False, ) assert mock_run.call_args == call( mock_run.assert_called_with( [profile_path / "bin/switch-to-configuration", "switch"], extra_env={"NIXOS_INSTALL_BOOTLOADER": "0"}, sudo=False, Loading Loading @@ -741,7 +738,7 @@ def test_switch_to_configuration(mock_run: Mock, monkeypatch: MonkeyPatch) -> No install_bootloader=True, specialisation="special", ) assert mock_run.call_args == call( mock_run.assert_called_with( [ config_path / "specialisation/special/bin/switch-to-configuration", "test", Loading @@ -765,14 +762,14 @@ 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) assert mock_run.call_args == call(["nix-channel", "--update", "nixos"], check=False) mock_run.reset_mock() 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) assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call(["nix-channel", "--update", "nixos"], check=False), call(["nix-channel", "--update", "nixos-hardware"], check=False), call(["nix-channel", "--update", "home-manager"], check=False), ] ) pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_process.py +7 −6 Original line number Diff line number Diff line from unittest.mock import Mock, call, patch from typing import Any from unittest.mock import patch from pytest import MonkeyPatch Loading @@ -9,9 +10,9 @@ from .helpers import get_qualified_name @patch(get_qualified_name(p.subprocess.run), autospec=True) def test_run(mock_run: Mock) -> None: def test_run(mock_run: Any) -> None: p.run_wrapper(["test", "--with", "flags"], check=True) assert mock_run.call_args == call( mock_run.assert_called_with( ["test", "--with", "flags"], check=True, text=True, Loading @@ -27,7 +28,7 @@ def test_run(mock_run: Mock) -> None: sudo=True, extra_env={"FOO": "bar"}, ) assert mock_run.call_args == call( mock_run.assert_called_with( ["sudo", "test", "--with", "flags"], check=False, text=True, Loading @@ -44,7 +45,7 @@ def test_run(mock_run: Mock) -> None: check=True, remote=m.Remote("user@localhost", ["--ssh", "opt"], "password"), ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "ssh", "--ssh", Loading @@ -70,7 +71,7 @@ def test_run(mock_run: Mock) -> None: extra_env={"FOO": "bar"}, remote=m.Remote("user@localhost", ["--ssh", "opt"], "password"), ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "ssh", "--ssh", Loading pkgs/by-name/ni/nixos-rebuild-ng/tests/repl.nix +4 −4 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ let escapeExpect = lib.strings.escapeNixString; expectSetup = '' set timeout 180 set timeout 300 proc expect_simple { pattern } { puts "Expecting: $pattern" expect { Loading Loading @@ -76,7 +76,7 @@ runCommand "test-nixos-rebuild-repl" expect ${writeText "test-nixos-rebuild-repl-expect" '' ${expectSetup} spawn nixos-rebuild repl --fast spawn nixos-rebuild repl --no-reexec expect "nix-repl> " Loading Loading @@ -116,7 +116,7 @@ runCommand "test-nixos-rebuild-repl" expect ${writeText "test-nixos-rebuild-repl-absolute-path-expect" '' ${expectSetup} spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf" spawn sh -c "nixos-rebuild repl --no-reexec --flake path:\$HOME#testconf" expect_simple "nix-repl>" Loading Loading @@ -146,7 +146,7 @@ runCommand "test-nixos-rebuild-repl" pushd "$HOME" expect ${writeText "test-nixos-rebuild-repl-relative-path-expect" '' ${expectSetup} spawn sh -c "nixos-rebuild repl --fast --flake .#testconf" spawn sh -c "nixos-rebuild repl --no-reexec --flake .#testconf" expect_simple "nix-repl>" Loading Loading
pkgs/by-name/ni/nixos-rebuild-ng/package.nix +3 −0 Original line number Diff line number Diff line Loading @@ -89,6 +89,9 @@ python3Packages.buildPythonApplication rec { ps: with ps; [ mypy pytest # this is to help development (e.g.: better diffs) inside devShell # only, do not use its helpers like `mocker` pytest-mock ruff ] ); Loading
pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py +601 −481 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ from typing import Any from unittest.mock import ANY, Mock, call, patch import pytest from pytest import MonkeyPatch import nixos_rebuild as nr Loading Loading @@ -125,6 +126,92 @@ def test_parse_args() -> None: ] @patch.dict(nr.os.environ, {}, clear=True) @patch(get_qualified_name(nr.os.execve, nr.os), autospec=True) @patch(get_qualified_name(nr.nix.build), autospec=True) def test_reexec(mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(nr, "EXECUTABLE", "nixos-rebuild-ng") argv = ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"] args, _ = nr.parse_args(argv) mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": 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 mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully 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 mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--no-flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, ) @patch.dict(nr.os.environ, {}, clear=True) @patch(get_qualified_name(nr.os.execve, nr.os), autospec=True) @patch(get_qualified_name(nr.nix.build_flake), autospec=True) def test_reexec_flake( mock_build: Mock, mock_execve: Mock, monkeypatch: MonkeyPatch ) -> None: monkeypatch.setattr(nr, "EXECUTABLE", "nixos-rebuild-ng") argv = ["/path/bin/nixos-rebuild-ng", "switch", "--flake"] args, _ = nr.parse_args(argv) mock_build.return_value = Path("/path") nr.reexec(argv, args, {"build": True}, {"flake": 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 mock_execve.assert_not_called() mock_build.return_value = Path("/path/new") nr.reexec(argv, args, {}, {}) # exec in the new version successfully 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 mock_execve.assert_any_call( Path("/path/bin/nixos-rebuild-ng"), ["/path/bin/nixos-rebuild-ng", "switch", "--flake"], {"_NIXOS_REBUILD_REEXEC": "1"}, ) @patch.dict(nr.process.os.environ, {}, clear=True) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: Loading @@ -147,7 +234,9 @@ def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: nr.execute(["nixos-rebuild", "boot", "--no-flake", "-vvv", "--no-reexec"]) assert mock_run.call_args_list == [ assert mock_run.call_count == 6 mock_run.assert_has_calls( [ call( ["nix-instantiate", "--find-file", "nixpkgs", "-vvv"], stdout=PIPE, Loading Loading @@ -195,6 +284,7 @@ def test_execute_nix_boot(mock_run: Mock, tmp_path: Path) -> None: **(DEFAULT_RUN_KWARGS | {"env": {"NIXOS_INSTALL_BOOTLOADER": "0"}}), ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -224,7 +314,9 @@ def test_execute_nix_build_vm(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 1 mock_run.assert_has_calls( [ call( [ "nix-build", Loading @@ -241,6 +333,7 @@ def test_execute_nix_build_vm(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ) ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -279,7 +372,9 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -307,6 +402,7 @@ def test_execute_nix_build_image_flake(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -340,7 +436,9 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 3 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -377,6 +475,7 @@ def test_execute_nix_switch_flake(mock_run: Mock, tmp_path: Path) -> None: **(DEFAULT_RUN_KWARGS | {"env": {"NIXOS_INSTALL_BOOTLOADER": "1"}}), ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -430,7 +529,9 @@ def test_execute_nix_switch_build_target_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 10 mock_run.assert_has_calls( [ call( [ "nix-instantiate", Loading Loading @@ -570,6 +671,7 @@ def test_execute_nix_switch_build_target_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -604,7 +706,9 @@ def test_execute_nix_switch_flake_target_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 4 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -656,6 +760,7 @@ def test_execute_nix_switch_flake_target_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch.dict(nr.process.os.environ, {}, clear=True) Loading Loading @@ -691,7 +796,9 @@ def test_execute_nix_switch_flake_build_host( ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 6 mock_run.assert_has_calls( [ call( [ "nix", Loading Loading @@ -755,6 +862,7 @@ def test_execute_nix_switch_flake_build_host( **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading Loading @@ -783,7 +891,9 @@ def test_execute_switch_rollback(mock_run: Mock, tmp_path: Path) -> None: ] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 4 mock_run.assert_has_calls( [ call( ["nix-instantiate", "--find-file", "nixpkgs"], check=False, Loading Loading @@ -822,6 +932,7 @@ def test_execute_switch_rollback(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading @@ -835,7 +946,9 @@ def test_execute_build(mock_run: Mock, tmp_path: Path) -> None: nr.execute(["nixos-rebuild", "build", "--no-flake", "--no-reexec"]) assert mock_run.call_args_list == [ assert mock_run.call_count == 1 mock_run.assert_has_calls( [ call( [ "nix-build", Loading @@ -848,6 +961,7 @@ def test_execute_build(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ) ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading @@ -867,7 +981,9 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None: ["nixos-rebuild", "test", "--flake", "github:user/repo#hostname", "--no-reexec"] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix", Loading @@ -887,6 +1003,7 @@ def test_execute_test_flake(mock_run: Mock, tmp_path: Path) -> None: **DEFAULT_RUN_KWARGS, ), ] ) @patch(get_qualified_name(nr.process.subprocess.run), autospec=True) Loading Loading @@ -917,7 +1034,9 @@ def test_execute_test_rollback( ["nixos-rebuild", "test", "--rollback", "--profile-name", "foo", "--no-reexec"] ) assert mock_run.call_args_list == [ assert mock_run.call_count == 2 mock_run.assert_has_calls( [ call( [ "nix-env", Loading @@ -940,3 +1059,4 @@ def test_execute_test_rollback( **DEFAULT_RUN_KWARGS, ), ] )
pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +146 −149 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ def test_build(mock_run: Mock) -> None: m.BuildAttr("<nixpkgs/nixos>", None), {"nix_flag": "foo"}, ) == Path("/path/to/file") assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-build", "<nixpkgs/nixos>", Loading @@ -38,17 +38,13 @@ def test_build(mock_run: Mock) -> None: stdout=PIPE, ) mock_run.reset_mock() assert n.build( "config.system.build.attr", m.BuildAttr(Path("file"), "preAttr") ) == Path("/path/to/file") assert mock_run.call_args_list == [ call( mock_run.assert_called_with( ["nix-build", Path("file"), "--attr", "preAttr.config.system.build.attr"], stdout=PIPE, ) ] @patch( Loading @@ -65,7 +61,7 @@ def test_build_flake(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> flake, {"no_link": True, "nix_flag": "foo"}, ) == Path("/path/to/file") assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "--extra-experimental-features", Loading Loading @@ -114,7 +110,9 @@ def test_build_remote( instantiate_flags={"inst": True}, copy_flags={"copy": True}, ) == Path("/path/to/config") assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( [ "nix-instantiate", Loading @@ -135,7 +133,9 @@ def test_build_remote( "user@host", Path("/path/to/file"), ], extra_env={"NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"])}, extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"]) }, ), call( ["mktemp", "-d", "-t", "nixos-rebuild.XXXXX"], Loading @@ -161,6 +161,7 @@ def test_build_remote( ), call(["rm", "-rf", Path("/tmp/tmpdir")], remote=build_host, check=False), ] ) @patch( Loading @@ -184,7 +185,8 @@ def test_build_remote_flake( copy_flags={"copy": True}, flake_build_flags={"build": True}, ) == Path("/path/to/file") assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( [ "nix", Loading @@ -205,7 +207,9 @@ def test_build_remote_flake( "user@host", Path("/path/to/file"), ], extra_env={"NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"])}, extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh opts"]) }, ), call( [ Loading @@ -221,6 +225,7 @@ def test_build_remote_flake( stdout=PIPE, ), ] ) def test_copy_closure(monkeypatch: MonkeyPatch) -> None: Loading @@ -233,7 +238,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: build_host = m.Remote("user@build.host", [], None) with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-copy-closure", "--to", "user@target.host", closure], extra_env={"NIX_SSHOPTS": " ".join(p.SSH_DEFAULT_OPTS)}, ) Loading @@ -241,7 +246,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: monkeypatch.setenv("NIX_SSHOPTS", "--ssh build-opt") with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, None, build_host, {"copy_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-copy-closure", "--copy-flag", "--from", "user@build.host", closure], extra_env={ "NIX_SSHOPTS": " ".join([*p.SSH_DEFAULT_OPTS, "--ssh build-opt"]) Loading @@ -255,7 +260,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: } with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host, build_host, {"copy_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "copy", Loading @@ -272,7 +277,8 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(n, "WITH_NIX_2_18", False) with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: n.copy_closure(closure, target_host, build_host) assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call( ["nix-copy-closure", "--from", "user@build.host", closure], extra_env=extra_env, Loading @@ -282,6 +288,7 @@ def test_copy_closure(monkeypatch: MonkeyPatch) -> None: extra_env=extra_env, ), ] ) @patch(get_qualified_name(n.run_wrapper, n), autospec=True) Loading @@ -289,7 +296,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None: # Flake flake = m.Flake.parse(f"{tmpdir}#attr") n.edit(flake, {"commit_lock_file": True}) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "--extra-experimental-features", Loading @@ -311,7 +318,7 @@ def test_edit(mock_run: Mock, monkeypatch: MonkeyPatch, tmpdir: Path) -> None: mp.setenv("EDITOR", "editor") n.edit(None) assert mock_run.call_args == call(["editor", default_nix], check=False) mock_run.assert_called_with(["editor", default_nix], check=False) @patch( Loading @@ -334,7 +341,7 @@ def test_get_build_image_variants(mock_run: Mock, tmp_path: Path) -> None: "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-instantiate", "--eval", Loading @@ -352,14 +359,12 @@ def test_get_build_image_variants(mock_run: Mock, tmp_path: Path) -> None: stdout=PIPE, ) mock_run.reset_mock() build_attr = m.BuildAttr(Path(tmp_path), "preAttr") assert n.get_build_image_variants(build_attr, {"inst_flag": True}) == { "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-instantiate", "--eval", Loading Loading @@ -399,7 +404,7 @@ def test_get_build_image_variants_flake(mock_run: Mock) -> None: "azure": "nixos-image-azure-25.05.20250102.6df2492-x86_64-linux.vhd", "vmware": "nixos-image-vmware-25.05.20250102.6df2492-x86_64-linux.vmdk", } assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix", "eval", Loading @@ -424,7 +429,7 @@ def test_get_nixpkgs_rev() -> None: side_effect=[CompletedProcess([], 0, "")], ) as mock_run: assert n.get_nixpkgs_rev(path) is None assert mock_run.call_args == call( mock_run.assert_called_with( ["git", "-C", path, "rev-parse", "--short", "HEAD"], check=False, capture_output=True, Loading @@ -451,7 +456,7 @@ def test_get_nixpkgs_rev() -> None: ], ) as mock_run: assert n.get_nixpkgs_rev(path) == ".git.0f7c82403fd6" assert mock_run.call_args_list == expected_calls mock_run.assert_has_calls(expected_calls) with patch( get_qualified_name(n.run_wrapper, n), Loading @@ -462,7 +467,7 @@ def test_get_nixpkgs_rev() -> None: ], ) as mock_run: assert n.get_nixpkgs_rev(path) == ".git.0f7c82403fd6M" assert mock_run.call_args_list == expected_calls mock_run.assert_has_calls(expected_calls) def test_get_generations(tmp_path: Path) -> None: Loading Loading @@ -503,7 +508,7 @@ def test_get_generations_from_nix_env(tmp_path: Path) -> None: m.Generation(id=2083, current=False, timestamp="2024-11-07 22:59:41"), m.Generation(id=2084, current=True, timestamp="2024-11-07 23:54:17"), ] assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", path, "--list-generations"], stdout=PIPE, remote=None, Loading @@ -521,7 +526,7 @@ def test_get_generations_from_nix_env(tmp_path: Path) -> None: m.Generation(id=2083, current=False, timestamp="2024-11-07 22:59:41"), m.Generation(id=2084, current=True, timestamp="2024-11-07 23:54:17"), ] assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", path, "--list-generations"], stdout=PIPE, remote=remote, Loading Loading @@ -573,14 +578,12 @@ def test_list_generations(mock_get_generations: Mock, tmp_path: Path) -> None: @patch(get_qualified_name(n.run_wrapper, n), autospec=True) def test_repl(mock_run: Mock) -> None: n.repl("attr", m.BuildAttr("<nixpkgs/nixos>", None), {"nix_flag": True}) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix", "repl", "--file", "<nixpkgs/nixos>", "--nix-flag"] ) n.repl("attr", m.BuildAttr(Path("file.nix"), "myAttr")) assert mock_run.call_args == call( ["nix", "repl", "--file", Path("file.nix"), "myAttr"] ) mock_run.assert_called_with(["nix", "repl", "--file", Path("file.nix"), "myAttr"]) @patch(get_qualified_name(n.run_wrapper, n), autospec=True) Loading @@ -599,7 +602,7 @@ def test_rollback(mock_run: Mock, tmp_path: Path) -> None: profile = m.Profile("system", path) assert n.rollback(profile, None, False) == profile.path assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "--rollback", "-p", path], remote=None, sudo=False, Loading @@ -607,7 +610,7 @@ def test_rollback(mock_run: Mock, tmp_path: Path) -> None: target_host = m.Remote("user@localhost", [], None) assert n.rollback(profile, target_host, True) == profile.path assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "--rollback", "-p", path], remote=target_host, sudo=True, Loading @@ -619,10 +622,8 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: path.touch() profile = m.Profile("system", path) with patch( get_qualified_name(n.run_wrapper, n), autospec=True, return_value=CompletedProcess( with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: mock_run.return_value = CompletedProcess( [], 0, stdout=textwrap.dedent("""\ Loading @@ -630,13 +631,12 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: 2083 2024-11-07 22:59:41 2084 2024-11-07 23:54:17 (current) """), ), ) as mock_run: ) assert ( n.rollback_temporary_profile(m.Profile("system", path), None, False) == path.parent / "system-2083-link" ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-env", "-p", Loading @@ -653,7 +653,7 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: n.rollback_temporary_profile(m.Profile("foo", path), target_host, True) == path.parent / "foo-2083-link" ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "nix-env", "-p", Loading @@ -665,11 +665,8 @@ def test_rollback_temporary_profile(tmp_path: Path) -> None: sudo=True, ) with patch( get_qualified_name(n.run_wrapper, n), autospec=True, return_value=CompletedProcess([], 0, stdout=""), ) as mock_run: with patch(get_qualified_name(n.run_wrapper, n), autospec=True) as mock_run: mock_run.return_value = CompletedProcess([], 0, stdout="") assert n.rollback_temporary_profile(profile, None, False) is None Loading @@ -684,7 +681,7 @@ def test_set_profile(mock_run: Mock) -> None: sudo=False, ) assert mock_run.call_args == call( mock_run.assert_called_with( ["nix-env", "-p", profile_path, "--set", config_path], remote=None, sudo=False, Loading @@ -707,7 +704,7 @@ def test_switch_to_configuration(mock_run: Mock, monkeypatch: MonkeyPatch) -> No specialisation=None, install_bootloader=False, ) assert mock_run.call_args == call( mock_run.assert_called_with( [profile_path / "bin/switch-to-configuration", "switch"], extra_env={"NIXOS_INSTALL_BOOTLOADER": "0"}, sudo=False, Loading Loading @@ -741,7 +738,7 @@ def test_switch_to_configuration(mock_run: Mock, monkeypatch: MonkeyPatch) -> No install_bootloader=True, specialisation="special", ) assert mock_run.call_args == call( mock_run.assert_called_with( [ config_path / "specialisation/special/bin/switch-to-configuration", "test", Loading @@ -765,14 +762,14 @@ 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) assert mock_run.call_args == call(["nix-channel", "--update", "nixos"], check=False) mock_run.reset_mock() 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) assert mock_run.call_args_list == [ mock_run.assert_has_calls( [ call(["nix-channel", "--update", "nixos"], check=False), call(["nix-channel", "--update", "nixos-hardware"], check=False), call(["nix-channel", "--update", "home-manager"], check=False), ] )
pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_process.py +7 −6 Original line number Diff line number Diff line from unittest.mock import Mock, call, patch from typing import Any from unittest.mock import patch from pytest import MonkeyPatch Loading @@ -9,9 +10,9 @@ from .helpers import get_qualified_name @patch(get_qualified_name(p.subprocess.run), autospec=True) def test_run(mock_run: Mock) -> None: def test_run(mock_run: Any) -> None: p.run_wrapper(["test", "--with", "flags"], check=True) assert mock_run.call_args == call( mock_run.assert_called_with( ["test", "--with", "flags"], check=True, text=True, Loading @@ -27,7 +28,7 @@ def test_run(mock_run: Mock) -> None: sudo=True, extra_env={"FOO": "bar"}, ) assert mock_run.call_args == call( mock_run.assert_called_with( ["sudo", "test", "--with", "flags"], check=False, text=True, Loading @@ -44,7 +45,7 @@ def test_run(mock_run: Mock) -> None: check=True, remote=m.Remote("user@localhost", ["--ssh", "opt"], "password"), ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "ssh", "--ssh", Loading @@ -70,7 +71,7 @@ def test_run(mock_run: Mock) -> None: extra_env={"FOO": "bar"}, remote=m.Remote("user@localhost", ["--ssh", "opt"], "password"), ) assert mock_run.call_args == call( mock_run.assert_called_with( [ "ssh", "--ssh", Loading
pkgs/by-name/ni/nixos-rebuild-ng/tests/repl.nix +4 −4 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ let escapeExpect = lib.strings.escapeNixString; expectSetup = '' set timeout 180 set timeout 300 proc expect_simple { pattern } { puts "Expecting: $pattern" expect { Loading Loading @@ -76,7 +76,7 @@ runCommand "test-nixos-rebuild-repl" expect ${writeText "test-nixos-rebuild-repl-expect" '' ${expectSetup} spawn nixos-rebuild repl --fast spawn nixos-rebuild repl --no-reexec expect "nix-repl> " Loading Loading @@ -116,7 +116,7 @@ runCommand "test-nixos-rebuild-repl" expect ${writeText "test-nixos-rebuild-repl-absolute-path-expect" '' ${expectSetup} spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf" spawn sh -c "nixos-rebuild repl --no-reexec --flake path:\$HOME#testconf" expect_simple "nix-repl>" Loading Loading @@ -146,7 +146,7 @@ runCommand "test-nixos-rebuild-repl" pushd "$HOME" expect ${writeText "test-nixos-rebuild-repl-relative-path-expect" '' ${expectSetup} spawn sh -c "nixos-rebuild repl --fast --flake .#testconf" spawn sh -c "nixos-rebuild repl --no-reexec --flake .#testconf" expect_simple "nix-repl>" Loading