import subprocess
import unittest
+from typing import Optional
+
import git_cache
+def _setenv(var: str, value: Optional[str]) -> None:
+ if value is None:
+ del os.environ[var]
+ else:
+ os.environ[var] = value
+
+
def _git(directory: str, *args: str) -> bytes:
p = subprocess.run(['git', '-C', directory] + list(args),
stdout=subprocess.PIPE, check=True)
def setUp(self) -> None:
self.xdgcache = tempfile.TemporaryDirectory(prefix='git_cache_test-')
self.old_XDG_CACHE_HOME = os.environ.get('XDG_CACHE_HOME')
- os.environ['XDG_CACHE_HOME'] = self.xdgcache.name
+ _setenv('XDG_CACHE_HOME', self.xdgcache.name)
os.environ['GIT_AUTHOR_NAME'] = 'test_git_cache'
os.environ['GIT_COMMITTER_NAME'] = 'test_git_cache'
_commit_file(self.upstream, 'file', 'Contents', 'First commit')
def tearDown(self) -> None:
- if self.old_XDG_CACHE_HOME is None:
- del os.environ['XDG_CACHE_HOME']
- else:
- os.environ['XDG_CACHE_HOME'] = self.old_XDG_CACHE_HOME
+ _setenv('XDG_CACHE_HOME', self.old_XDG_CACHE_HOME)
self.tempdir.cleanup()
self.xdgcache.cleanup()