summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁrni Dagur <arni@dagur.eu>2020-11-27 03:04:21 +0000
committerÁrni Dagur <arni@dagur.eu>2020-12-19 20:30:10 +0000
commitd2eb3b61ebea49facbfae7485024c13b959f62f6 (patch)
tree20658aabf4f7b13857ae38ae3d1f884d3d069bf3
parent121b5752d430c79ba0437996331609692e53e1fc (diff)
downloadqutebrowser-d2eb3b61ebea49facbfae7485024c13b959f62f6.tar.gz
qutebrowser-d2eb3b61ebea49facbfae7485024c13b959f62f6.zip
Improve test coverage for ModuleInfo class
-rw-r--r--qutebrowser/utils/version.py2
-rw-r--r--tests/unit/utils/test_version.py49
2 files changed, 44 insertions, 7 deletions
diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py
index 53e5287bd..1dc623881 100644
--- a/qutebrowser/utils/version.py
+++ b/qutebrowser/utils/version.py
@@ -305,6 +305,8 @@ class ModuleInfo:
self._version = str(version)
break
+ self._initialized = True
+
def get_version(self) -> Optional[str]:
"""Finds the module version if it exists."""
if not self._initialized:
diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py
index b4875635e..a82514e08 100644
--- a/tests/unit/utils/test_version.py
+++ b/tests/unit/utils/test_version.py
@@ -544,7 +544,7 @@ class ImportFake:
Attributes:
modules: A dict mapping module names to bools. If True, the import will
- success. Otherwise, it'll fail with ImportError.
+ succeed. Otherwise, it'll fail with ImportError.
version_attribute: The name to use in the fake modules for the version
attribute.
version: The version to use for the modules.
@@ -622,7 +622,7 @@ def import_fake(monkeypatch):
class TestModuleVersions:
- """Tests for _module_versions()."""
+ """Tests for _module_versions() and ModuleInfo."""
def test_all_present(self, import_fake):
"""Test with all modules present in version 1.2.3."""
@@ -647,8 +647,39 @@ class TestModuleVersions:
expected: The expected text.
"""
import_fake.modules[module] = False
+ # Needed after mocking the module
+ mod_info = version.MODULE_INFO[module]
+ mod_info._reset_cache()
+
assert version._module_versions()[idx] == expected
+ for method_name, expected in [
+ ("is_installed", False),
+ ("get_version", None),
+ ("is_outdated", None)
+ ]:
+ method = getattr(mod_info, method_name)
+ # With hot cache
+ mod_info._initialize_info()
+ assert method() == expected
+ # With cold cache
+ mod_info._reset_cache()
+ assert method() == expected
+
+ def test_outdated_adblock(self, import_fake):
+ """Test that warning is shown when adblock module is outdated."""
+ min_version = version.MODULE_INFO["adblock"].min_version
+ fake_version = "0.1.0"
+
+ # Needed after mocking version attribute
+ version.MODULE_INFO["adblock"]._reset_cache()
+
+ assert min_version is not None
+ assert fake_version < min_version
+ import_fake.version = fake_version
+ expected = f"adblock: {fake_version} (< {min_version}, outdated)"
+ assert version._module_versions()[6] == expected
+
@pytest.mark.parametrize('attribute, expected_modules', [
('VERSION', ['colorama']),
('SIP_VERSION_STR', ['sip']),
@@ -665,18 +696,22 @@ class TestModuleVersions:
expected: The expected return value.
"""
import_fake.version_attribute = attribute
+
+ for mod_info in version.MODULE_INFO.values():
+ # Invalidate the "version cache" since we just mocked some of the
+ # attributes.
+ mod_info._reset_cache()
+
expected = []
for name in import_fake.modules:
+ mod_info = version.MODULE_INFO[name]
if name in expected_modules:
+ assert mod_info.get_version() == "1.2.3"
expected.append('{}: 1.2.3'.format(name))
else:
+ assert mod_info.get_version() is None
expected.append('{}: yes'.format(name))
- for mod_info in version.MODULE_INFO.values():
- # Invalidate the "version cache" since we just mocked some of the
- # attributes.
- mod_info._reset_cache()
-
assert version._module_versions() == expected
@pytest.mark.parametrize('name, has_version', [