From ebc9ef0eee6944cc74c62f9e9aee26f5120ebd6f Mon Sep 17 00:00:00 2001 From: Dariusz Duda Date: Fri, 8 Nov 2024 12:13:52 -0500 Subject: [PATCH] test: extract helper fixtures to the conftest.py Signed-off-by: Dariusz Duda --- tests/conftest.py | 77 +++++++++++++++++++++++++++++++++++++- tests/unit/git/test_git.py | 74 +----------------------------------- 2 files changed, 77 insertions(+), 74 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ac105b72..6816532c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,8 @@ import os import pathlib import shutil +import subprocess +from dataclasses import dataclass from importlib import metadata from typing import TYPE_CHECKING, Any @@ -27,7 +29,7 @@ import jinja2 import pydantic import pytest -from craft_application import application, launchpad, models, services, util +from craft_application import application, git, launchpad, models, services, util from craft_cli import EmitterMode, emit from craft_providers import bases from jinja2 import FileSystemLoader @@ -366,3 +368,76 @@ def new_dir(tmp_path): yield tmp_path os.chdir(cwd) + + +@pytest.fixture +def empty_working_directory( + tmp_path: pathlib.Path, + monkeypatch: pytest.MonkeyPatch, +) -> pathlib.Path: + repo_dir = pathlib.Path(tmp_path, "test-repo") + repo_dir.mkdir() + monkeypatch.chdir(repo_dir) + return repo_dir + + +@pytest.fixture +def empty_repository(empty_working_directory: pathlib.Path) -> pathlib.Path: + subprocess.run(["git", "init"], check=True) + return empty_working_directory + + +@dataclass +class RepositoryDefinition: + repository_path: pathlib.Path + commit: str + tag: str | None = None + + @property + def short_commit(self) -> str: + """Return abbreviated commit.""" + return git.short_commit_sha(self.commit) + + +@pytest.fixture +def repository_with_commit(empty_repository: pathlib.Path) -> RepositoryDefinition: + repo = git.GitRepo(empty_repository) + (empty_repository / "Some file").touch() + repo.add_all() + commit_sha = repo.commit("1") + return RepositoryDefinition( + repository_path=empty_repository, + commit=commit_sha, + ) + + +@pytest.fixture +def repository_with_annotated_tag( + repository_with_commit: RepositoryDefinition, +) -> RepositoryDefinition: + test_tag = "v3.2.1" + subprocess.run( + ["git", "config", "--local", "user.name", "Testcraft", test_tag], check=True + ) + subprocess.run( + ["git", "config", "--local", "user.email", "testcraft@canonical.com", test_tag], + check=True, + ) + subprocess.run(["git", "tag", "-a", "-m", "testcraft tag", test_tag], check=True) + repository_with_commit.tag = test_tag + return repository_with_commit + + +@pytest.fixture +def repository_with_unannotated_tag( + repository_with_commit: RepositoryDefinition, +) -> RepositoryDefinition: + subprocess.run(["git", "config", "--local", "user.name", "Testcraft"], check=True) + subprocess.run( + ["git", "config", "--local", "user.email", "testcraft@canonical.com"], + check=True, + ) + test_tag = "non-annotated" + subprocess.run(["git", "tag", test_tag], check=True) + repository_with_commit.tag = test_tag + return repository_with_commit diff --git a/tests/unit/git/test_git.py b/tests/unit/git/test_git.py index 6a628d8c..cec290c7 100644 --- a/tests/unit/git/test_git.py +++ b/tests/unit/git/test_git.py @@ -17,7 +17,6 @@ import re import subprocess -from dataclasses import dataclass from pathlib import Path from typing import cast from unittest.mock import ANY @@ -39,78 +38,7 @@ check_git_repo_for_remote_build, ) - -@pytest.fixture -def empty_working_directory( - tmp_path: Path, - monkeypatch: pytest.MonkeyPatch, -) -> Path: - repo_dir = Path(tmp_path, "test-repo") - repo_dir.mkdir() - monkeypatch.chdir(repo_dir) - return repo_dir - - -@pytest.fixture -def empty_repository(empty_working_directory: Path) -> Path: - subprocess.run(["git", "init"], check=True) - return empty_working_directory - - -@dataclass -class RepositoryDefinition: - repository_path: Path - commit: str - tag: str | None = None - - @property - def short_commit(self) -> str: - """Return abbreviated commit.""" - return short_commit_sha(self.commit) - - -@pytest.fixture -def repository_with_commit(empty_repository: Path) -> RepositoryDefinition: - repo = GitRepo(empty_repository) - (empty_repository / "Some file").touch() - repo.add_all() - commit_sha = repo.commit("1") - return RepositoryDefinition( - repository_path=empty_repository, - commit=commit_sha, - ) - - -@pytest.fixture -def repository_with_annotated_tag( - repository_with_commit: RepositoryDefinition, -) -> RepositoryDefinition: - test_tag = "v3.2.1" - subprocess.run( - ["git", "config", "--local", "user.name", "Testcraft", test_tag], check=True - ) - subprocess.run( - ["git", "config", "--local", "user.email", "testcraft@canonical.com", test_tag], - check=True, - ) - subprocess.run(["git", "tag", "-a", "-m", "testcraft tag", test_tag], check=True) - repository_with_commit.tag = test_tag - return repository_with_commit - - -@pytest.fixture -def repository_with_unannotated_tag( - repository_with_commit: RepositoryDefinition, -) -> RepositoryDefinition: - subprocess.run(["git", "config", "--local", "user.name", "Testcraft"], check=True) - subprocess.run( - ["git", "config", "--local", "user.email", "testcraft@canonical.com"], - check=True, - ) - test_tag = "non-annotated" - subprocess.run(["git", "tag", test_tag], check=True) - repository_with_commit.tag = test_tag - return repository_with_commit +from tests.conftest import RepositoryDefinition def test_is_repo(empty_working_directory):