Skip to content
Snippets Groups Projects
Commit db90846f authored by Georges Racinet's avatar Georges Racinet
Browse files

New fixture base class for service tests

This will help make common patterns more uniform.
The `__init__` super dance is not the most elegant code
ever written, but using classmethods wouldn't be so much of
an improvement.
parent 26b06812
No related branches found
No related tags found
1 merge request!100RepositoryService: implement RemoveRepository
# Copyright 2022 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import attr
import shutil
from hgitaly.tests.common import (
make_empty_repo,
)
@attr.s
class ServiceFixture:
"""Fixture base class to absorb some of the boilerplate.
See test_ref for example usage.
"""
stub_cls = attr.ib()
grpc_channel = attr.ib()
server_repos_root = attr.ib()
with_repo = attr.ib(default=True)
"""If ``True``, create a repository in the default storage upon setup.
The repository details are then accessible in :attr:`grpc_repo`
and :attr:`repo_wrapper`. If :attr:`with_repo` is ``False``, these
attributes are initialised to None
"""
auto_cleanup = attr.ib(default=False)
"""If ``True``, remove repositories tracked by this class upon tear down.
For other repositories than the one created if :attr:`with_repo` is
``True``, this relies on downstream code adding them to
attr:`repos_to_cleanup`.
This can be useful for certain services because the
``server_repos_root`` fixture has the module scope, but most
services won't need it because the path of the repository provided by
this class is unique.
"""
def __enter__(self):
self.stub = self.stub_cls(self.grpc_channel)
if self.with_repo:
self.repo_wrapper, self.grpc_repo = make_empty_repo(
self.server_repos_root)
self.repos_to_cleanup = [self.repo_wrapper.path]
else: # pragma no cover (not used _yet_)
self.repo_wrapper = self.grpc_repo = None
self.repos_to_cleanup = []
return self
def __exit__(self, *exc_args):
if not self.auto_cleanup:
return
for repo_path in self.repos_to_cleanup: # pragma no cover
# auto cleanup is not used yet
if repo_path.exists():
shutil.rmtree(repo_path)
......@@ -49,4 +49,5 @@
)
from hgitaly.stub.ref_pb2_grpc import RefServiceStub
from .fixture import ServiceFixture
......@@ -52,3 +53,2 @@
class RefFixture:
......@@ -54,7 +54,8 @@
def __init__(self, grpc_channel, server_repos_root):
self.stub = RefServiceStub(grpc_channel)
self.repo_wrapper, self.grpc_repo = make_empty_repo(server_repos_root)
class RefFixture(ServiceFixture):
def __init__(self, *a, **kw):
super(RefFixture, self).__init__(RefServiceStub, *a, **kw)
def commit_file(self, *args, **kwargs):
return self.repo_wrapper.commit_file(*args, **kwargs)
......@@ -82,7 +83,8 @@
@pytest.fixture
def ref_fixture(grpc_channel, server_repos_root):
yield RefFixture(grpc_channel, server_repos_root)
with RefFixture(grpc_channel, server_repos_root) as fixture:
yield fixture
def test_find_all_branch_names(ref_fixture):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment