103 lines
3.7 KiB
Python
103 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from cf_temp_email_deploy.models import DeploymentConfig, DeploymentState
|
|
from cf_temp_email_deploy.source import prepare_source
|
|
from cf_temp_email_deploy.subprocess_runner import CommandRunner
|
|
|
|
|
|
def run_git(args: tuple[str, ...], cwd: Path) -> str:
|
|
completed = subprocess.run(args, cwd=cwd, check=True, capture_output=True, text=True)
|
|
return completed.stdout.strip()
|
|
|
|
|
|
def create_git_repository(path: Path) -> str:
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
run_git(("git", "init", "-b", "main"), path)
|
|
run_git(("git", "config", "user.name", "Codex"), path)
|
|
run_git(("git", "config", "user.email", "codex@example.com"), path)
|
|
(path / "README.md").write_text("demo\n", encoding="utf-8")
|
|
run_git(("git", "add", "README.md"), path)
|
|
run_git(("git", "commit", "-m", "initial"), path)
|
|
return run_git(("git", "rev-parse", "HEAD"), path)
|
|
|
|
|
|
def test_prepare_local_source_records_commit(tmp_path: Path) -> None:
|
|
repo_dir = tmp_path / "local-repo"
|
|
expected_sha = create_git_repository(repo_dir)
|
|
|
|
config = DeploymentConfig.model_validate(
|
|
{
|
|
"source": {
|
|
"mode": "local",
|
|
"local_path": str(repo_dir),
|
|
},
|
|
"cloudflare": {"zone_name": "example.com"},
|
|
"mail": {"domains": ["mail.example.com"], "verified_destination_address": "target@example.com"},
|
|
"pages": {"custom_domain": "mail.example.com"},
|
|
}
|
|
)
|
|
|
|
prepared = prepare_source(config, CommandRunner())
|
|
|
|
assert prepared.source_dir == repo_dir.resolve()
|
|
assert prepared.commit_sha == expected_sha
|
|
|
|
state = DeploymentState()
|
|
prepared.apply_to_state(state)
|
|
assert state.source.source_dir == str(repo_dir.resolve())
|
|
assert state.source.commit_sha == expected_sha
|
|
|
|
|
|
def test_prepare_clone_source_clones_and_checks_out_ref(tmp_path: Path) -> None:
|
|
origin_dir = tmp_path / "origin-repo"
|
|
expected_sha = create_git_repository(origin_dir)
|
|
workspace_dir = tmp_path / "workspace"
|
|
|
|
config = DeploymentConfig.model_validate(
|
|
{
|
|
"source": {
|
|
"mode": "clone",
|
|
"repo_url": str(origin_dir),
|
|
"repo_ref": "main",
|
|
"workspace_dir": str(workspace_dir),
|
|
},
|
|
"cloudflare": {"zone_name": "example.com"},
|
|
"mail": {"domains": ["mail.example.com"], "verified_destination_address": "target@example.com"},
|
|
"pages": {"custom_domain": "mail.example.com"},
|
|
}
|
|
)
|
|
|
|
prepared = prepare_source(config, CommandRunner())
|
|
|
|
assert prepared.source_dir == (workspace_dir / "source").resolve()
|
|
assert prepared.commit_sha == expected_sha
|
|
assert (prepared.source_dir / "README.md").read_text(encoding="utf-8") == "demo\n"
|
|
|
|
|
|
def test_prepare_clone_source_uses_latest_default_branch_when_repo_ref_empty(tmp_path: Path) -> None:
|
|
origin_dir = tmp_path / "origin-repo"
|
|
expected_sha = create_git_repository(origin_dir)
|
|
workspace_dir = tmp_path / "workspace"
|
|
|
|
config = DeploymentConfig.model_validate(
|
|
{
|
|
"source": {
|
|
"mode": "clone",
|
|
"repo_url": str(origin_dir),
|
|
"repo_ref": "",
|
|
"workspace_dir": str(workspace_dir),
|
|
},
|
|
"cloudflare": {"zone_name": "example.com"},
|
|
"mail": {"domains": ["example.com"], "verified_destination_address": "target@example.com"},
|
|
"pages": {"custom_domain": "email.example.com"},
|
|
}
|
|
)
|
|
|
|
prepared = prepare_source(config, CommandRunner())
|
|
|
|
assert prepared.source_dir == (workspace_dir / "source").resolve()
|
|
assert prepared.commit_sha == expected_sha
|