1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
import contextlib
import os
import tempfile
import pygit2
import pytest
import gitubuntu.git_repository
@pytest.fixture
def repo():
"""An empty GitUbuntuRepository repository fixture
The GitUbuntuRepository class methods (other than main entry point)
assume they are running with the current work directory set to the
Git repository's filesystem path. Additionally, pristine-tar and
other commands require this to be the case, so chdir in the fixture
itself.
"""
try:
oldcwd = os.getcwd()
except OSError:
oldcwd = None
# The git CLI requires a valid email address to be available. Normally, we
# might expect this to be the git-ubuntu CLI caller's responsibility to
# set. However, in tests, since GitUbuntuRepository calls the git CLI for
# some operations that cannot currently be done through pygit2, it makes
# sense to set EMAIL temporarily so that the test suite is independent of
# the environment in which it is running.
old_email = os.environ.get('EMAIL')
os.environ['EMAIL'] = 'test@example.com'
with tempfile.TemporaryDirectory() as path:
os.chdir(path)
try:
yield gitubuntu.git_repository.GitUbuntuRepository(path)
except:
raise
finally:
if oldcwd:
os.chdir(oldcwd)
# Reset the EMAIL environment variable to what it was. This makes
# little practical difference, but may prevent confusion and a
# headache if in the future a different test is written that does
# not use this fixture but does rely somehow on the EMAIL
# environment variable not changing, as such a test would become
# non-deterministic depending on whether other tests have run that
# use this fixture.
if old_email is None:
del os.environ['EMAIL']
else:
os.environ['EMAIL'] = old_email
@pytest.fixture
def pygit2_repo():
"""An empty pygit2 repository fixture in a temporary directory"""
with tempfile.TemporaryDirectory() as tmp_dir:
yield pygit2.init_repository(tmp_dir)
|