Repository: miurahr/aqtinstall
Pull request #534 (2022-06-11 03:09:18)

archives: do not keep update.xml text in field
Signed-off-by: Hiroshi Miura <miurahr@linux.com>


Patch:
diff --git a/aqt/archives.py b/aqt/archives.py
index 3a94dbc..fdb141a 100644
--- a/aqt/archives.py
+++ b/aqt/archives.py
@@ -38,9 +38,6 @@ class TargetConfig:
     arch: str
     os_name: str
 
-    def __post_init__(self):
-        self.version = str(self.version)
-
 
 @dataclass
 class QtPackage:
@@ -225,20 +222,19 @@ class QtArchives:
             f"qt{self.version.major}_{self._version_str()}{self._arch_ext()}",
         )
         update_xml_path = posixpath.join(os_target_folder, "Updates.xml")
-        self._download_update_xml(update_xml_path)
-        self._parse_update_xml(os_target_folder, self._target_packages())
+        update_xml_text = self._download_update_xml(update_xml_path)
+        self._parse_update_xml(os_target_folder, update_xml_text, self._target_packages())
 
     def _download_update_xml(self, update_xml_path):
         """Hook for unit test."""
         xml_hash = get_hash(update_xml_path, "sha256", self.timeout)
-        update_xml_text = getUrl(posixpath.join(self.base, update_xml_path), self.timeout, xml_hash)
-        self.update_xml_text = update_xml_text
+        return getUrl(posixpath.join(self.base, update_xml_path), self.timeout, xml_hash)
 
-    def _parse_update_xml(self, os_target_folder, target_packages: Optional[ModuleToPackage]):
+    def _parse_update_xml(self, os_target_folder, update_xml_text, target_packages: Optional[ModuleToPackage]):
         if not target_packages:
             target_packages = ModuleToPackage({})
         try:
-            self.update_xml = ElementTree.fromstring(self.update_xml_text)
+            self.update_xml = ElementTree.fromstring(update_xml_text)
         except ElementTree.ParseError as perror:
             raise ArchiveListError(f"Downloaded metadata is corrupted. {perror}") from perror
 
@@ -324,7 +320,7 @@ class QtArchives:
         :return: configured target and its version with arch
         :rtype: TargetConfig object
         """
-        return TargetConfig(self.version, self.target, self.arch, self.os_name)
+        return TargetConfig(str(self.version), self.target, self.arch, self.os_name)
 
 
 class SrcDocExamplesArchives(QtArchives):
@@ -442,12 +438,12 @@ class ToolArchives(QtArchives):
             self.tool_name,
         )
         update_xml_url = posixpath.join(os_target_folder, "Updates.xml")
-        self._download_update_xml(update_xml_url)  # call super method.
-        self._parse_update_xml(os_target_folder, None)
+        update_xml_text = self._download_update_xml(update_xml_url)  # call super method.
+        self._parse_update_xml(os_target_folder, update_xml_text, None)
 
-    def _parse_update_xml(self, os_target_folder, *ignored):
+    def _parse_update_xml(self, os_target_folder, update_xml_text, *ignored):
         try:
-            self.update_xml = ElementTree.fromstring(self.update_xml_text)
+            self.update_xml = ElementTree.fromstring(update_xml_text)
         except ElementTree.ParseError as perror:
             raise ArchiveListError(f"Downloaded metadata is corrupted. {perror}") from perror
 


Test patch:
diff --git a/tests/test_archives.py b/tests/test_archives.py
index 88300af..2a42a6c 100644
--- a/tests/test_archives.py
+++ b/tests/test_archives.py
@@ -29,8 +29,7 @@ def setup():
 )
 def test_parse_update_xml(monkeypatch, os_name, version, arch, datafile):
     def _mock(self, url):
-        with open(os.path.join(os.path.dirname(__file__), "data", datafile), "r") as f:
-            self.update_xml_text = f.read()
+        return (Path(__file__).parent / "data" / datafile).read_text("utf-8")
 
     monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)
 
@@ -101,10 +100,8 @@ def test_qtarchive_parse_corrupt_xmlfile(monkeypatch, corrupt_xmlfile, archives_
     ),
 )
 def test_qt_archives_modules(monkeypatch, arch, requested_module_names, has_nonexistent_modules: bool):
-    update_xml = (Path(__file__).parent / "data" / "windows-5140-update.xml").read_text("utf-8")
-
     def _mock(self, *args):
-        self.update_xml_text = update_xml
+        return (Path(__file__).parent / "data" / "windows-5140-update.xml").read_text("utf-8")
 
     monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)
 
@@ -194,10 +191,9 @@ def test_qt_archives_modules(monkeypatch, arch, requested_module_names, has_none
 def test_tools_variants(monkeypatch, tool_name, tool_variant_name, is_expect_fail: bool):
     host, target, base = "mac", "desktop", "https://example.com"
     datafile = f"{host}-{target}-{tool_name}"
-    update_xml = (Path(__file__).parent / "data" / f"{datafile}-update.xml").read_text("utf-8")
 
     def _mock(self, *args):
-        self.update_xml_text = update_xml
+        return (Path(__file__).parent / "data" / f"{datafile}-update.xml").read_text("utf-8")
 
     monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)
 
@@ -256,7 +252,7 @@ def to_xml(package_updates: Iterable[Dict]) -> str:
 )
 def test_tool_archive_wrong_version(monkeypatch, tool_name, variant_name, version, actual_version):
     def _mock(self, *args):
-        self.update_xml_text = to_xml([dict(Name=variant_name, Version=actual_version)])
+        return to_xml([dict(Name=variant_name, Version=actual_version)])
 
     monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)
 
diff --git a/tests/test_doc_archives.py b/tests/test_doc_archives.py
index 656407a..1a3c448 100644
--- a/tests/test_doc_archives.py
+++ b/tests/test_doc_archives.py
@@ -1,4 +1,5 @@
 import os
+from pathlib import Path
 
 import pytest
 
@@ -21,8 +22,7 @@ def setup():
 )
 def test_parse_update_xml(monkeypatch, os_name, version, flavor, datafile):
     def _mock(self, url):
-        with open(os.path.join(os.path.dirname(__file__), "data", datafile), "r") as f:
-            self.update_xml_text = f.read()
+        return (Path(__file__).parent / "data" / datafile).read_text("utf-8")
 
     monkeypatch.setattr(QtArchives, "_download_update_xml", _mock)