diff --git a/pkgs/clan-cli/tests/fixtures_flakes.py b/pkgs/clan-cli/tests/fixtures_flakes.py index 983a1e321..4b999307b 100644 --- a/pkgs/clan-cli/tests/fixtures_flakes.py +++ b/pkgs/clan-cli/tests/fixtures_flakes.py @@ -59,9 +59,29 @@ def set_machine_settings( config_path.write_text(json.dumps(machine_settings, indent=2)) +def init_git(monkeypatch: pytest.MonkeyPatch, flake: Path) -> None: + monkeypatch.setenv("GIT_AUTHOR_NAME", "clan-tool") + monkeypatch.setenv("GIT_AUTHOR_EMAIL", "clan@example.com") + monkeypatch.setenv("GIT_COMMITTER_NAME", "clan-tool") + monkeypatch.setenv("GIT_COMMITTER_EMAIL", "clan@example.com") + + # TODO: Find out why test_vms_api.py fails in nix build + # but works in pytest when this bottom line is commented out + sp.run( + ["git", "config", "--global", "init.defaultBranch", "main"], + cwd=flake, + check=True, + ) + + sp.run(["git", "init"], cwd=flake, check=True) + sp.run(["git", "add", "."], cwd=flake, check=True) + sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True) + + def generate_flake( temporary_home: Path, flake_template: Path, + monkeypatch: pytest.MonkeyPatch, substitutions: dict[str, str] | None = None, # define the machines directly including their config machine_configs: dict[str, dict] | None = None, @@ -125,7 +145,7 @@ def generate_flake( configuration_nix.write_text(""" { imports = [ (builtins.fromJSON (builtins.readFile ./configuration.json)) ]; } """) - set_machine_config(flake, machine_name, machine_config) + set_machine_settings(flake, machine_name, machine_config) if "/tmp" not in str(os.environ.get("HOME")): log.warning( @@ -139,17 +159,15 @@ def generate_flake( cwd=flake, check=True, ) - sp.run(["git", "init"], cwd=flake, check=True) - sp.run(["git", "add", "."], cwd=flake, check=True) - sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True) - sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True) - sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True) + init_git(monkeypatch, flake) + return FlakeForTest(flake) def create_flake( temporary_home: Path, flake_template: str | Path, + monkeypatch: pytest.MonkeyPatch, clan_core_flake: Path | None = None, # names referring to pre-defined machines from ../machines machines: list[str] | None = None, @@ -202,18 +220,7 @@ def create_flake( f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}" ) - # TODO: Find out why test_vms_api.py fails in nix build - # but works in pytest when this bottom line is commented out - sp.run( - ["git", "config", "--global", "init.defaultBranch", "main"], - cwd=flake, - check=True, - ) - sp.run(["git", "init"], cwd=flake, check=True) - sp.run(["git", "add", "."], cwd=flake, check=True) - sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True) - sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True) - sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True) + init_git(monkeypatch, flake) if remote: with tempfile.TemporaryDirectory(prefix="flake-"): @@ -226,7 +233,11 @@ def create_flake( def test_flake( monkeypatch: pytest.MonkeyPatch, temporary_home: Path ) -> Iterator[FlakeForTest]: - yield from create_flake(temporary_home, "test_flake") + yield from create_flake( + temporary_home=temporary_home, + flake_template="test_flake", + monkeypatch=monkeypatch, + ) # check that git diff on ./sops is empty if (temporary_home / "test_flake" / "sops").exists(): git_proc = sp.run( @@ -248,9 +259,10 @@ def test_flake_with_core( msg = "clan-core flake not found. This test requires the clan-core flake to be present" raise FixtureError(msg) yield from create_flake( - temporary_home, - "test_flake_with_core", - CLAN_CORE, + temporary_home=temporary_home, + flake_template="test_flake_with_core", + clan_core_flake=CLAN_CORE, + monkeypatch=monkeypatch, ) @@ -280,9 +292,10 @@ def test_flake_with_core_and_pass( msg = "clan-core flake not found. This test requires the clan-core flake to be present" raise FixtureError(msg) yield from create_flake( - temporary_home, - "test_flake_with_core_and_pass", - CLAN_CORE, + temporary_home=temporary_home, + flake_template="test_flake_with_core_and_pass", + clan_core_flake=CLAN_CORE, + monkeypatch=monkeypatch, ) @@ -294,7 +307,8 @@ def test_flake_minimal( msg = "clan-core flake not found. This test requires the clan-core flake to be present" raise FixtureError(msg) yield from create_flake( - temporary_home, - CLAN_CORE / "templates" / "minimal", - CLAN_CORE, + temporary_home=temporary_home, + flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, + clan_core_flake=CLAN_CORE, ) diff --git a/pkgs/clan-cli/tests/test_vars.py b/pkgs/clan-cli/tests/test_vars.py index 46d80c448..5a7355acd 100644 --- a/pkgs/clan-cli/tests/test_vars.py +++ b/pkgs/clan-cli/tests/test_vars.py @@ -90,6 +90,7 @@ def test_generate_public_var( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -130,6 +131,7 @@ def test_generate_secret_var_sops( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -172,6 +174,7 @@ def test_generate_secret_var_sops_with_default_group( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -211,6 +214,7 @@ def test_generated_shared_secret_sops( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"machine1": m1_config, "machine2": m2_config}, ) monkeypatch.chdir(flake.path) @@ -255,6 +259,7 @@ def test_generate_secret_var_password_store( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -309,6 +314,7 @@ def test_generate_secret_for_multiple_machines( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"machine1": machine1_config, "machine2": machine2_config}, ) monkeypatch.chdir(flake.path) @@ -354,6 +360,7 @@ def test_dependant_generators( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -393,6 +400,7 @@ def test_prompt( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -432,6 +440,7 @@ def test_share_flag( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -482,6 +491,7 @@ def test_prompt_create_file( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -510,6 +520,7 @@ def test_api_get_prompts( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -538,6 +549,7 @@ def test_api_set_prompts( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -584,6 +596,7 @@ def test_commit_message( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -643,6 +656,7 @@ def test_default_value( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -688,6 +702,7 @@ def test_stdout_of_generate( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -767,6 +782,7 @@ def test_migration_skip( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -798,6 +814,7 @@ def test_migration( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) @@ -832,6 +849,7 @@ def test_fails_when_files_are_left_from_other_backend( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"my_machine": config}, ) monkeypatch.chdir(flake.path) diff --git a/pkgs/clan-cli/tests/test_vars_deployment.py b/pkgs/clan-cli/tests/test_vars_deployment.py index 90e991c61..2a9bcc945 100644 --- a/pkgs/clan-cli/tests/test_vars_deployment.py +++ b/pkgs/clan-cli/tests/test_vars_deployment.py @@ -63,6 +63,7 @@ def test_vm_deployment( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs={"m1_machine": machine1_config, "m2_machine": machine2_config}, ) diff --git a/pkgs/clan-cli/tests/test_vms_cli.py b/pkgs/clan-cli/tests/test_vms_cli.py index 7957add0a..fc07c15e9 100644 --- a/pkgs/clan-cli/tests/test_vms_cli.py +++ b/pkgs/clan-cli/tests/test_vms_cli.py @@ -83,6 +83,7 @@ def test_vm_persistence( flake = generate_flake( temporary_home, flake_template=CLAN_CORE / "templates" / "minimal", + monkeypatch=monkeypatch, machine_configs=config, )