Skip to content

Commit

Permalink
test: Fix FileNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetrr committed Apr 19, 2024
1 parent 6d9d54a commit 6c21ed1
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions tests/e2e/test_utils/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shutil
from enum import StrEnum
from pathlib import Path
from tempfile import mkdtemp
from tempfile import TemporaryDirectory, mkdtemp
from typing import Any
from uuid import uuid4

Expand Down Expand Up @@ -36,17 +36,22 @@ class CommitMessages(StrEnum):

class TestRepo:
"""Wrapper around the ``git.Repo`` class that implements specific methods for unit testing."""
path: Path
tempdir: TemporaryDirectory
repo: Repo

def __init__(self) -> None:
def __init__(self, *, keep_repository_dir: bool = False) -> None:
"""
Wrapper around the ``git.Repo`` class that implements specific methods for unit testing.
This creates a new git repo in a temporary directory, makes an initial README commit to the ``main`` branch
and creates 3 branches called ``release``, ``release-beta`` and ``release-prod``.
"""
self.path = Path(mkdtemp(prefix='wemogy.get-release-version-action.tests'))
self.tempdir = TemporaryDirectory(
prefix='wemogy.get-release-version-action.tests',
ignore_cleanup_errors=True,
delete=not keep_repository_dir
)

logger.info('Creating git repository in directory %s', self.path)
os.chdir(self.path)

Expand All @@ -68,15 +73,14 @@ def __enter__(self) -> TestRepo:
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
self.close()

def close(self, keep_repository_dir: bool = False) -> None:
"""Close the ``git.Repo`` object and delete the temporary folder unless specified otherwise."""
self.repo.close()
@property
def path(self) -> Path:
"""Get the path of the temporary directory."""
return Path(self.tempdir.name)

if not keep_repository_dir:
logger.info('Removing repository %s', self.path)
shutil.rmtree(self.path)
else:
logger.warning('Keeping repository %s', self.path)
def close(self) -> None:
"""Close the ``git.Repo`` object."""
self.repo.close()

def create_branch(self, name: str, base_name: str | None) -> None:
"""Create a branch named ``name`` that is based on the branch with ``base_name`` and check it out."""
Expand Down

0 comments on commit 6c21ed1

Please sign in to comment.