From 63fba6c892fa3e5f23198bdd8a719de8a663ea82 Mon Sep 17 00:00:00 2001 From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com> Date: Sat, 17 Feb 2024 17:35:16 -0800 Subject: [PATCH] ulwgl_test: add tests for cache --- ulwgl_test.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/ulwgl_test.py b/ulwgl_test.py index 18c1257..bbe5eb8 100644 --- a/ulwgl_test.py +++ b/ulwgl_test.py @@ -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.