ulwgl_test: add tests for cache

This commit is contained in:
R1kaB3rN 2024-02-17 17:35:16 -08:00
parent 15c483ed60
commit 63fba6c892
No known key found for this signature in database

View File

@ -100,6 +100,120 @@ class TestGameLauncher(unittest.TestCase):
if self.test_proton_dir.exists():
rmtree(self.test_proton_dir.as_posix())
def test_cache_interrupt(self):
"""Test _get_from_cache on keyboard interrupt."""
result = None
# In the real usage, should be populated after successful callout for latest Proton releases
# Just mock it and assumes its the latest
files = [("", ""), (self.test_archive.name, "")]
# Populate the Steam compat dir by extracting the tarball
ulwgl_dl_util._extract_dir(self.test_archive, self.test_compat)
self.assertTrue(
self.test_compat.joinpath(
self.test_archive.name[: self.test_archive.name.find(".tar.gz")]
).exists(),
"Expected Proton dir to exist in compat",
)
with patch("ulwgl_dl_util._extract_dir") as mock_function:
with self.assertRaisesRegex(KeyboardInterrupt, ""):
# Mock the interrupt
# We want the dir we tried to extract to be cleaned
mock_function.side_effect = KeyboardInterrupt
result = ulwgl_dl_util._get_from_cache(
self.env, self.test_compat, self.test_cache, files, True
)
self.assertFalse(result, "Expected None on keyboard interrupt")
self.assertFalse(
self.env["PROTONPATH"],
"Expected PROTONPATH to be empty when the cache is empty",
)
self.assertFalse(
self.test_compat.joinpath(
self.test_archive.name[: self.test_archive.name.find(".tar.gz")]
).exists(),
"Expected Proton dir in compat to be cleaned",
)
def test_cache_old(self):
"""Test _get_from_cache when the cache is empty.
In real usage, this only happens as a last resort when: download fails, digests mismatched, etc.
"""
result = None
# In the real usage, should be populated after successful callout for latest Proton releases
# Just mock it and assumes its the latest
files = [("", ""), (self.test_archive.name, "")]
# Mock old Proton versions in the cache
test_proton_dir = Path("ULWGL-Proton-foo")
test_proton_dir.mkdir(exist_ok=True)
test_archive = Path(self.test_cache).joinpath(
f"{test_proton_dir.as_posix()}.tar.gz"
)
with tarfile.open(test_archive.as_posix(), "w:gz") as tar:
tar.add(test_proton_dir.as_posix(), arcname=test_proton_dir.as_posix())
result = ulwgl_dl_util._get_from_cache(
self.env, self.test_compat, self.test_cache, files, False
)
# Verify that the old Proton was assigned
self.assertTrue(result is self.env, "Expected the same reference")
self.assertEqual(
self.env["PROTONPATH"],
self.test_compat.joinpath(
test_archive.name[: test_archive.name.find(".tar.gz")]
).as_posix(),
"Expected PROTONPATH to be proton dir in compat",
)
test_archive.unlink()
test_proton_dir.rmdir()
def test_cache_empty(self):
"""Test _get_from_cache when the cache is empty."""
result = None
# In the real usage, should be populated after successful callout for latest Proton releases
# Just mock it and assumes its the latest
files = [("", ""), (self.test_archive.name, "")]
self.test_archive.unlink()
result = ulwgl_dl_util._get_from_cache(
self.env, self.test_compat, self.test_cache, files, True
)
self.assertFalse(result, "Expected None when calling _get_from_cache")
self.assertFalse(
self.env["PROTONPATH"],
"Expected PROTONPATH to be empty when the cache is empty",
)
def test_cache(self):
"""Test _get_from_cache.
Tests the case when the latest Proton already exists in the cache
"""
result = None
# In the real usage, should be populated after successful callout for latest Proton releases
# Just mock it and assumes its the latest
files = [("", ""), (self.test_archive.name, "")]
result = ulwgl_dl_util._get_from_cache(
self.env, self.test_compat, self.test_cache, files, True
)
self.assertTrue(result is self.env, "Expected the same reference")
self.assertEqual(
self.env["PROTONPATH"],
self.test_compat.joinpath(
self.test_archive.name[: self.test_archive.name.find(".tar.gz")]
).as_posix(),
"Expected PROTONPATH to be proton dir in compat",
)
def test_steamcompat_nodir(self):
"""Test _get_from_steamcompat when a Proton doesn't exist in the Steam compat dir.