nixos-rebuild-ng: fix -I flag passed multiple times (#372919)

This commit is contained in:
Thiago Kenji Okada
2025-01-12 16:48:30 +00:00
committed by GitHub
4 changed files with 52 additions and 2 deletions
@@ -38,7 +38,7 @@ def get_parser() -> tuple[argparse.ArgumentParser, dict[str, argparse.ArgumentPa
common_build_flags = argparse.ArgumentParser(add_help=False)
common_build_flags.add_argument("--builders")
common_build_flags.add_argument("--include", "-I")
common_build_flags.add_argument("--include", "-I", action="append")
common_build_flags.add_argument("--quiet", action="store_true")
common_build_flags.add_argument("--print-build-logs", "-L", action="store_true")
common_build_flags.add_argument("--show-trace", action="store_true")
@@ -43,8 +43,8 @@ def dict_to_flags(d: Args | None) -> list[str]:
flags.append(flag)
flags.append(value)
case list():
flags.append(flag)
for v in value:
flags.append(flag)
flags.append(v)
case _:
assert_never(value)
@@ -150,6 +150,55 @@ def test_execute_nix_boot(mock_run: Any, tmp_path: Path) -> None:
)
@patch.dict(nr.process.os.environ, {}, clear=True)
@patch(get_qualified_name(nr.process.subprocess.run), autospec=True)
def test_execute_nix_build_vm(mock_run: Any, tmp_path: Path) -> None:
config_path = tmp_path / "test"
config_path.touch()
def run_side_effect(args: list[str], **kwargs: Any) -> CompletedProcess[str]:
if args[0] == "nix-build":
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)
mock_run.side_effect = run_side_effect
nr.execute(
[
"nixos-rebuild",
"build-vm",
"--no-flake",
"-I",
"nixos-config=./configuration.nix",
"-I",
"nixpkgs=$HOME/.nix-defexpr/channels/pinned_nixpkgs",
"--fast",
]
)
assert mock_run.call_count == 1
mock_run.assert_has_calls(
[
call(
[
"nix-build",
"<nixpkgs/nixos>",
"--attr",
"config.system.build.vm",
"--include",
"nixos-config=./configuration.nix",
"--include",
"nixpkgs=$HOME/.nix-defexpr/channels/pinned_nixpkgs",
],
check=True,
stdout=PIPE,
**DEFAULT_RUN_KWARGS,
)
]
)
@patch.dict(nr.process.os.environ, {}, clear=True)
@patch(get_qualified_name(nr.process.subprocess.run), autospec=True)
def test_execute_nix_build_image_flake(mock_run: Any, tmp_path: Path) -> None:
@@ -23,6 +23,7 @@ def test_dict_to_flags() -> None:
"value",
"--test-flag-4",
"v1",
"--test-flag-4",
"v2",
"-t",
"-vvvvv",