/ tests / e2e / conftest.py
conftest.py
  1  """Shared fixtures for Auths E2E tests."""
  2  
  3  import os
  4  import shutil
  5  import subprocess
  6  from pathlib import Path
  7  
  8  import pytest
  9  
 10  from helpers.cli import run_auths
 11  from helpers.git import init_git_repo
 12  
 13  
 14  def _find_binary(env_var: str, name: str) -> Path | None:
 15      """Resolve a binary from env var, PATH, or target/debug."""
 16      if path := os.environ.get(env_var):
 17          p = Path(path)
 18          if p.exists():
 19              return p
 20  
 21      if found := shutil.which(name):
 22          return Path(found)
 23  
 24      workspace_root = Path(__file__).resolve().parent.parent.parent
 25      debug_path = workspace_root / "target" / "debug" / name
 26      if debug_path.exists():
 27          return debug_path
 28  
 29      return None
 30  
 31  
 32  @pytest.fixture(scope="session")
 33  def auths_bin():
 34      """Path to the `auths` binary."""
 35      path = _find_binary("AUTHS_BIN", "auths")
 36      if path is None:
 37          pytest.skip("auths binary not found (set AUTHS_BIN or build with cargo)")
 38      return path
 39  
 40  
 41  @pytest.fixture(scope="session")
 42  def auths_sign_bin():
 43      """Path to the `auths-sign` binary."""
 44      path = _find_binary("AUTHS_SIGN_BIN", "auths-sign")
 45      if path is None:
 46          pytest.skip("auths-sign binary not found (set AUTHS_SIGN_BIN or build with cargo)")
 47      return path
 48  
 49  
 50  @pytest.fixture(scope="session")
 51  def auths_verify_bin():
 52      """Path to the `auths-verify` binary."""
 53      path = _find_binary("AUTHS_VERIFY_BIN", "auths-verify")
 54      if path is None:
 55          pytest.skip("auths-verify binary not found (set AUTHS_VERIFY_BIN or build with cargo)")
 56      return path
 57  
 58  
 59  @pytest.fixture
 60  def isolated_env(tmp_path, auths_bin):
 61      """Fully isolated environment for CLI tests."""
 62      auths_home = tmp_path / ".auths"
 63      auths_home.mkdir()
 64      keychain_file = auths_home / "keys.enc"
 65  
 66      bin_dir = str(auths_bin.parent)
 67      path = f"{bin_dir}:{os.environ.get('PATH', '/usr/bin:/bin')}"
 68  
 69      return {
 70          "PATH": path,
 71          "HOME": str(tmp_path),
 72          "AUTHS_HOME": str(auths_home),
 73          "AUTHS_KEYCHAIN_BACKEND": "file",
 74          "AUTHS_KEYCHAIN_FILE": str(keychain_file),
 75          "AUTHS_PASSPHRASE": "TestPassphrase!42",
 76          "GIT_CONFIG_NOSYSTEM": "1",
 77          "GIT_AUTHOR_NAME": "Test User",
 78          "GIT_COMMITTER_NAME": "Test User",
 79          "GIT_AUTHOR_EMAIL": "test@auths.dev",
 80          "GIT_COMMITTER_EMAIL": "test@auths.dev",
 81          "NO_COLOR": "1",
 82      }
 83  
 84  
 85  @pytest.fixture
 86  def git_repo(tmp_path, isolated_env):
 87      """Temporary git repository with initial commit."""
 88      repo_path = tmp_path / "repo"
 89      repo_path.mkdir()
 90      init_git_repo(repo_path, isolated_env)
 91      return repo_path
 92  
 93  
 94  @pytest.fixture
 95  def init_identity(auths_bin, isolated_env):
 96      """Pre-initialized Auths identity."""
 97      result = run_auths(
 98          auths_bin,
 99          ["init", "--profile", "developer", "--non-interactive", "--skip-registration"],
100          env=isolated_env,
101      )
102      result.assert_success()
103      return isolated_env