From 2e2e0c8031ed3449565aefd7908435661c1c2b48 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 22 Nov 2023 17:50:29 +0100 Subject: pakjoy: 100% test coverage --- qutebrowser/misc/pakjoy.py | 2 +- scripts/dev/check_coverage.py | 2 ++ tests/unit/misc/test_pakjoy.py | 46 +++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/qutebrowser/misc/pakjoy.py b/qutebrowser/misc/pakjoy.py index 71d51d7a6..ec9906731 100644 --- a/qutebrowser/misc/pakjoy.py +++ b/qutebrowser/misc/pakjoy.py @@ -149,7 +149,7 @@ class PakParser: def copy_webengine_resources() -> Optional[pathlib.Path]: """Copy qtwebengine resources to local dir for patching.""" resources_dir = qtutils.library_path(qtutils.LibraryPath.data) - if utils.is_mac: + if utils.is_mac: # pragma: no cover # I'm not sure how to arrive at this path without hardcoding it # ourselves. importlib_resources("PyQt6.Qt6") can serve as a # replacement for the qtutils bit but it doesn't seem to help find the diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 228fc7bd9..38a8f6ca1 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -123,6 +123,8 @@ PERFECT_FILES = [ 'qutebrowser/misc/objects.py'), ('tests/unit/misc/test_throttle.py', 'qutebrowser/misc/throttle.py'), + ('tests/unit/misc/test_pakjoy.py', + 'qutebrowser/misc/pakjoy.py'), (None, 'qutebrowser/mainwindow/statusbar/keystring.py'), diff --git a/tests/unit/misc/test_pakjoy.py b/tests/unit/misc/test_pakjoy.py index 599127fef..e8463a8b2 100644 --- a/tests/unit/misc/test_pakjoy.py +++ b/tests/unit/misc/test_pakjoy.py @@ -89,6 +89,15 @@ def json_without_comments(bytestring): return json.loads(str_without_comments) +def read_patched_manifest(): + patched_resources = pathlib.Path(os.environ[pakjoy.RESOURCES_ENV_VAR]) + + with open(patched_resources / pakjoy.PAK_FILENAME, "rb") as fd: + reparsed = pakjoy.PakParser(fd) + + return json_without_comments(reparsed.manifest) + + @pytest.mark.usefixtures("affected_version") class TestWithRealResourcesFile: """Tests that use the real pak file form the Qt installation.""" @@ -100,13 +109,7 @@ class TestWithRealResourcesFile: # afterwards. pakjoy.patch_webengine() - patched_resources = pathlib.Path(os.environ[pakjoy.RESOURCES_ENV_VAR]) - - with open(patched_resources / pakjoy.PAK_FILENAME, "rb") as fd: - reparsed = pakjoy.PakParser(fd) - - json_manifest = json_without_comments(reparsed.manifest) - + json_manifest = read_patched_manifest() assert ( pakjoy.REPLACEMENT_URL.decode("utf-8") in json_manifest["externally_connectable"]["matches"] @@ -124,6 +127,7 @@ class TestWithRealResourcesFile: def test_copying_resources_overwrites(self): work_dir = pakjoy.copy_webengine_resources() + assert work_dir is not None tmpfile = work_dir / "tmp.txt" tmpfile.touch() @@ -218,8 +222,14 @@ def pak_factory(version=5, entries=None, encoding=1, sentinel_position=-1): class TestWithConstructedResourcesFile: """Tests that use a constructed pak file to give us more control over it.""" - def test_happy_path(self): - buffer = pak_factory() + @pytest.mark.parametrize( + "offset", + [0, 42, pakjoy.HANGOUTS_ID], # test both slow search and fast path + ) + def test_happy_path(self, offset): + entries = [b""] * offset + [json_manifest_factory()] + assert entries[offset] != b"" + buffer = pak_factory(entries=entries) parser = pakjoy.PakParser(buffer) @@ -290,3 +300,21 @@ class TestWithConstructedResourcesFile: pakjoy._patch(tmpfile) assert caplog.messages == ["Failed to apply quirk to resources pak."] + + def test_patching(self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path): + """Go through the full patching processes with a fake resources file.""" + resources_path = tmp_path / "resources" + resources_path.mkdir() + + buffer = pak_factory() + with open(resources_path / pakjoy.PAK_FILENAME, "wb") as fd: + fd.write(buffer.read()) + + monkeypatch.setattr(pakjoy.qtutils, "library_path", lambda _which: tmp_path) + pakjoy.patch_webengine() + + json_manifest = read_patched_manifest() + assert ( + pakjoy.REPLACEMENT_URL.decode("utf-8") + in json_manifest["externally_connectable"]["matches"] + ) -- cgit v1.2.3-54-g00ecf