From: Scott Worley Date: Mon, 21 Jun 2021 19:56:06 +0000 (-0700) Subject: _setenv() X-Git-Tag: v1.4.0~4 X-Git-Url: http://git.scottworley.com/git-cache/commitdiff_plain/8d251eca144e4c5c68155331f129fa02f5b4fa48 _setenv() --- diff --git a/test_git_cache.py b/test_git_cache.py index caac6a3..bbe7118 100644 --- a/test_git_cache.py +++ b/test_git_cache.py @@ -4,9 +4,18 @@ import shutil 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) @@ -30,7 +39,7 @@ class TestGitCache(unittest.TestCase): 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' @@ -45,10 +54,7 @@ class TestGitCache(unittest.TestCase): _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()