Fix ConfigTest failures locally

A couple of `ConfigTest` would previously fail locally since they
somehow depended on the local environment to work fine. This commmit
decouples them from any environment by setting up patching of the
environment variables properly.
This commit is contained in:
Šarūnas Nejus 2024-07-13 19:18:27 +01:00
parent 8d85cfd72a
commit 92f79dbfb9
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435

View file

@ -831,12 +831,9 @@ class ConfigTest(BeetsTestCase):
# Don't use the BEETSDIR from `helper`. Instead, we point the home # Don't use the BEETSDIR from `helper`. Instead, we point the home
# directory there. Some tests will set `BEETSDIR` themselves. # directory there. Some tests will set `BEETSDIR` themselves.
del os.environ["BEETSDIR"] del os.environ["BEETSDIR"]
self._old_home = os.environ.get("HOME")
os.environ["HOME"] = os.fsdecode(self.temp_dir)
# Also set APPDATA, the Windows equivalent of setting $HOME. # Also set APPDATA, the Windows equivalent of setting $HOME.
self._old_appdata = os.environ.get("APPDATA") appdata_dir = os.fsdecode(
os.environ["APPDATA"] = os.fsdecode(
os.path.join(self.temp_dir, b"AppData", b"Roaming") os.path.join(self.temp_dir, b"AppData", b"Roaming")
) )
@ -846,8 +843,8 @@ class ConfigTest(BeetsTestCase):
# Default user configuration # Default user configuration
if platform.system() == "Windows": if platform.system() == "Windows":
self.user_config_dir = os.path.join( self.user_config_dir = os.fsencode(
self.temp_dir, b"AppData", b"Roaming", b"beets" os.path.join(appdata_dir, "beets")
) )
else: else:
self.user_config_dir = os.path.join( self.user_config_dir = os.path.join(
@ -861,19 +858,19 @@ class ConfigTest(BeetsTestCase):
# Custom BEETSDIR # Custom BEETSDIR
self.beetsdir = os.path.join(self.temp_dir, b"beetsdir") self.beetsdir = os.path.join(self.temp_dir, b"beetsdir")
os.makedirs(syspath(self.beetsdir)) os.makedirs(syspath(self.beetsdir))
self.env_patcher = patch(
"os.environ",
{"HOME": os.fsdecode(self.temp_dir), "APPDATA": appdata_dir},
)
self.env_patcher.start()
self._reset_config() self._reset_config()
self.load_plugins() self.load_plugins()
def tearDown(self): def tearDown(self):
self.env_patcher.stop()
commands.default_commands.pop() commands.default_commands.pop()
os.chdir(syspath(self._orig_cwd)) os.chdir(syspath(self._orig_cwd))
if self._old_home is not None:
os.environ["HOME"] = self._old_home
if self._old_appdata is None:
del os.environ["APPDATA"]
else:
os.environ["APPDATA"] = self._old_appdata
self.unload_plugins() self.unload_plugins()
super().tearDown() super().tearDown()