]> git.scottworley.com Git - git-cache/commitdiff
_setenv()
authorScott Worley <scottworley@scottworley.com>
Mon, 21 Jun 2021 19:56:06 +0000 (12:56 -0700)
committerScott Worley <scottworley@scottworley.com>
Mon, 21 Jun 2021 19:56:06 +0000 (12:56 -0700)
test_git_cache.py

index caac6a33289432744717167dc5f0da9c7f397da1..bbe71188272c9d13254c5b4f4674bb6a4e43001f 100644 (file)
@@ -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()