From 606a9e2e151dc182a189acdb0f4c178156799663 Mon Sep 17 00:00:00 2001 From: Patrik Teivonen Date: Tue, 29 Nov 2022 11:59:30 +0200 Subject: Create temporary directories with temppathlib's TemporaryDirectory temppathlib wraps tempfile making it easier to use with pathlib Paths update copyright years for changed files Change-Id: I2447d7808922f1250ff585f5b90497aa4bb508b4 Reviewed-by: Antti Kokko --- Pipfile | 1 + packaging-tools/bldinstallercommon.py | 27 ++++--- packaging-tools/create_installer.py | 5 +- packaging-tools/install_qt.py | 8 +- packaging-tools/release_repo_updater.py | 13 ++-- packaging-tools/tests/test_bld_python.py | 25 +++--- packaging-tools/tests/test_bldinstallercommon.py | 54 ++++++------- packaging-tools/tests/test_build_wrapper.py | 29 ++++--- packaging-tools/tests/test_content_cleaner.py | 36 +++++---- packaging-tools/tests/test_create_installer.py | 27 +++---- packaging-tools/tests/test_installer_utils.py | 39 +++++----- packaging-tools/tests/test_packaging.py | 18 ++--- .../tests/test_release_repo_meta_update.py | 50 ++++++------ packaging-tools/tests/test_release_repo_updater.py | 89 +++++++++++++--------- packaging-tools/tests/test_runner.py | 15 ++-- packaging-tools/tests/test_sdkcomponent.py | 29 +++---- 16 files changed, 242 insertions(+), 223 deletions(-) diff --git a/Pipfile b/Pipfile index 1d7a0d94a..1b8e7cc64 100644 --- a/Pipfile +++ b/Pipfile @@ -20,6 +20,7 @@ asyncio-backport = {version = "==0.1.1", markers="(python_version >= '3.6' and p requests = "==2.27.1" htmllistparse = "==0.6.0" urlpath = "==1.2.0" +temppathlib = "==1.2.0" [dev-packages] ddt = "==1.6.0" diff --git a/packaging-tools/bldinstallercommon.py b/packaging-tools/bldinstallercommon.py index 526e09e3c..84896d9a0 100644 --- a/packaging-tools/bldinstallercommon.py +++ b/packaging-tools/bldinstallercommon.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -41,7 +41,6 @@ from contextlib import suppress from fnmatch import fnmatch from pathlib import Path from subprocess import CalledProcessError -from tempfile import mkdtemp from traceback import print_exc from types import TracebackType from typing import Any, Callable, Dict, List, Optional, Tuple, Union @@ -49,6 +48,7 @@ from urllib.parse import urlparse from urllib.request import url2pathname, urlcleanup, urlretrieve import requests +from temppathlib import TemporaryDirectory from bld_utils import download, is_linux, is_macos, is_windows, run_command from installer_utils import PackagingError @@ -228,11 +228,10 @@ def remove_one_tree_level(directory: str) -> None: dir_name = dircontents[0] full_dir_name = os.path.join(directory, dir_name) # avoid directory name collision by first moving to temporary dir - tempdir_base = mkdtemp() - tempdir = os.path.join(tempdir_base, 'a') # dummy name - shutil.move(full_dir_name, tempdir) - move_tree(tempdir, directory) - remove_tree(tempdir_base) + with TemporaryDirectory() as tempdir_base: + tempdir = tempdir_base.path / 'a' # dummy name + shutil.move(full_dir_name, str(tempdir)) + move_tree(str(tempdir), directory) else: raise IOError(f'Cannot remove one level of directory structure of "{dir}", it has {items} subdirectories') @@ -554,13 +553,13 @@ def git_archive_repo(repo_and_ref: str) -> str: if os.path.isfile(archive_name): os.remove(archive_name) # create temp directory - checkout_dir = mkdtemp() - # clone given repo to temp - clone_repository(repository, ref, checkout_dir, full_clone=True, init_subrepos=True) - # git archive repo with given name - run_cmd(cmd=["git", "--no-pager", "archive", ref, "-o", archive_name], cwd=checkout_dir) - log.info("Created archive: %s", archive_name) - shutil.rmtree(checkout_dir, ignore_errors=True) + with TemporaryDirectory() as checkout_dir: + checkout_path = checkout_dir.path + # clone given repo to temp + clone_repository(repository, ref, str(checkout_path), full_clone=True, init_subrepos=True) + # git archive repo with given name + run_cmd(cmd=["git", "--no-pager", "archive", ref, "-o", archive_name], cwd=checkout_path) + log.info("Created archive: %s", archive_name) return archive_name diff --git a/packaging-tools/create_installer.py b/packaging-tools/create_installer.py index 59ae18f6d..9644092c4 100644 --- a/packaging-tools/create_installer.py +++ b/packaging-tools/create_installer.py @@ -40,10 +40,11 @@ from dataclasses import dataclass, field from enum import Enum from multiprocessing import cpu_count from pathlib import Path -from tempfile import TemporaryDirectory from time import gmtime, strftime from typing import Any, Dict, Generator, Generic, List, Optional, Tuple, TypeVar +from temppathlib import TemporaryDirectory + from bld_utils import download, is_linux, is_macos, is_windows from bldinstallercommon import ( copy_tree, @@ -617,7 +618,7 @@ def get_component_data( return # Download payload to a temporary directory to avoid naming clashes with TemporaryDirectory() as temp_dir: - download_path = Path(temp_dir, download_name) + download_path = temp_dir.path / download_name log.info("[%s] Download: %s", archive.package_name, download_name) download(archive.archive_uri, str(download_path)) # For non-archive payload and non-extractable archives, move to install_dir for packing diff --git a/packaging-tools/install_qt.py b/packaging-tools/install_qt.py index 1fb39181c..b75625f0d 100644 --- a/packaging-tools/install_qt.py +++ b/packaging-tools/install_qt.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -32,9 +32,10 @@ import argparse import os import sys -from tempfile import TemporaryDirectory from typing import List, Optional +from temppathlib import TemporaryDirectory + from bldinstallercommon import create_qt_download_task, patch_qt from logging_util import init_logger from threadedwork import ThreadedWork @@ -112,8 +113,7 @@ def install_qt( else: with TemporaryDirectory() as temporary_dir: dl_pkgs_work.add_task_object( - create_qt_download_task(qt_modules, qt_path, temporary_dir, - opts) + create_qt_download_task(qt_modules, qt_path, str(temporary_dir.path), opts) ) # run task if needed diff --git a/packaging-tools/release_repo_updater.py b/packaging-tools/release_repo_updater.py index 2779cb61e..24725956d 100755 --- a/packaging-tools/release_repo_updater.py +++ b/packaging-tools/release_repo_updater.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -44,12 +44,13 @@ from datetime import datetime from enum import Enum from pathlib import Path from subprocess import PIPE -from tempfile import TemporaryDirectory from time import gmtime, sleep, strftime, time from typing import Any, Dict, List, Optional, Tuple from urllib.error import HTTPError, URLError from urllib.request import urlopen, urlretrieve +from temppathlib import TemporaryDirectory + from bld_utils import is_linux from bldinstallercommon import locate_path from create_installer import DryRunMode, QtInstallerTask, create_installer @@ -246,13 +247,13 @@ def create_remote_script( server: str, cmd: List[str], remote_script_path: str, script_file_name: str ) -> str: with TemporaryDirectory() as tmp_base_dir: - temp_file_path = os.path.join(tmp_base_dir, script_file_name) - with open(temp_file_path, 'w+', encoding="utf-8") as handle: + temp_file_path = tmp_base_dir.path / script_file_name + with temp_file_path.open('w+', encoding="utf-8") as handle: handle.write("#!/usr/bin/env bash\n") handle.write(' '.join(cmd)) - os.chmod(temp_file_path, 0o755) + temp_file_path.chmod(0o755) create_remote_paths(server, [remote_script_path]) - cmd = ['rsync', '-avzh', temp_file_path, server + ":" + remote_script_path] + cmd = ['rsync', '-avzh', str(temp_file_path), server + ":" + remote_script_path] run_cmd(cmd=cmd, timeout=60 * 60) return os.path.join(remote_script_path, script_file_name) diff --git a/packaging-tools/tests/test_bld_python.py b/packaging-tools/tests/test_bld_python.py index e1baf7e8c..f0c233f66 100755 --- a/packaging-tools/tests/test_bld_python.py +++ b/packaging-tools/tests/test_bld_python.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -29,9 +29,9 @@ # ############################################################################# -import os import unittest -from tempfile import TemporaryDirectory + +from temppathlib import TemporaryDirectory from bld_python import BldPythonError, locate_source_root from tests.testhelpers import asyncio_test @@ -42,19 +42,20 @@ class TestBldPython(unittest.TestCase): @asyncio_test async def test_locate_source_root(self) -> None: with TemporaryDirectory() as tmp_base_dir: - temp_dir = os.path.join(tmp_base_dir, "foo", "bar", "test", "dir") - os.makedirs(temp_dir) - temp_file_path = os.path.join(temp_dir, "configure") - with open(temp_file_path, 'w+', encoding="utf-8") as handle: + base_dir_path = tmp_base_dir.path + temp_dir = base_dir_path / "foo" / "bar" / "test" / "dir" + temp_dir.mkdir(parents=True) + temp_file_path = temp_dir / "configure" + with temp_file_path.open('w+', encoding="utf-8") as handle: handle.write("\n") - found_dir = locate_source_root(tmp_base_dir) - self.assertEqual(found_dir, temp_dir) + found_dir = locate_source_root(str(base_dir_path)) + self.assertEqual(found_dir, str(temp_dir)) - invalid_dir = os.path.join(tmp_base_dir, "foo2", "bar", "test", "dir") - os.makedirs(invalid_dir) + invalid_dir = base_dir_path / "foo2" / "bar" / "test" / "dir" + invalid_dir.mkdir(parents=True) with self.assertRaises(BldPythonError): - locate_source_root(os.path.join(tmp_base_dir, "foo2")) + locate_source_root(str(base_dir_path / "foo2")) if __name__ == '__main__': diff --git a/packaging-tools/tests/test_bldinstallercommon.py b/packaging-tools/tests/test_bldinstallercommon.py index 1c1b46367..f6816a76e 100644 --- a/packaging-tools/tests/test_bldinstallercommon.py +++ b/packaging-tools/tests/test_bldinstallercommon.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -32,10 +32,10 @@ import os import unittest from pathlib import Path -from tempfile import TemporaryDirectory from typing import Callable, List, Optional, Tuple from ddt import data, ddt # type: ignore +from temppathlib import TemporaryDirectory from bld_utils import is_windows from bldinstallercommon import ( @@ -79,12 +79,12 @@ class TestCommon(unittest.TestCase): file_contents, replacements, expected_file_content = test_data with TemporaryDirectory() as tmp_base_dir: # run tag substitution with data - tmp_file = Path(tmp_base_dir) / "test" - with open(tmp_file, "a", encoding="utf-8") as handle: + tmp_file = tmp_base_dir.path / "test" + with tmp_file.open("a", encoding="utf-8") as handle: handle.write(file_contents) for key, value in replacements: replace_in_files([str(tmp_file)], key, value) - with open(tmp_file, "r", encoding="utf-8") as handle: + with tmp_file.open("r", encoding="utf-8") as handle: file_contents = handle.read() # check that file contents match self.assertEqual(file_contents, expected_file_content) @@ -92,7 +92,7 @@ class TestCommon(unittest.TestCase): def test_replace_in_files_invalid_path(self) -> None: with TemporaryDirectory() as tmp_base_dir: # invalid file path should raise FileNotFoundError - invalid_path = Path(tmp_base_dir) / "invalid" + invalid_path = tmp_base_dir.path / "invalid" with self.assertRaises(FileNotFoundError): replace_in_files([str(invalid_path)], "foo", "bar") @@ -141,12 +141,12 @@ class TestCommon(unittest.TestCase): file, params, expected_files = test_data with TemporaryDirectory() as tmp_base_dir: path, content = file - tmp_file = Path(tmp_base_dir) / path + tmp_file = tmp_base_dir.path / path tmp_file.parents[0].mkdir(parents=True, exist_ok=True) - with open(tmp_file, "a", encoding="utf-8") as handle: + with tmp_file.open("a", encoding="utf-8") as handle: handle.write(content) extensions, rgx = params - result = search_for_files(tmp_base_dir, extensions, rgx) + result = search_for_files(tmp_base_dir.path, extensions, rgx) self.assertEqual(len(result), len(expected_files)) for result_path, expected_path in zip(result, expected_files): self.assertEqual(Path(result_path).name, expected_path) @@ -166,45 +166,45 @@ class TestCommon(unittest.TestCase): pattern, filters, expected_results = test_data with TemporaryDirectory() as tmp_base_dir: # Create files and folders - test_folders = ["/tempty", "/d/n", "/.d"] - test_files = ["/tst.t", "/tst.y", "/d/tst.t", "/.t", "/.d/.t"] + test_folders = ["tempty", "d/n", ".d"] + test_files = ["tst.t", "tst.y", "d/tst.t", ".t", ".d/.t"] for folder in test_folders: - Path(tmp_base_dir + folder).mkdir(parents=True) + (tmp_base_dir.path / folder).mkdir(parents=True) for file in test_files: - Path(tmp_base_dir + file).touch() - result = locate_paths(tmp_base_dir, pattern, filters) - result = [str(Path(p).relative_to(tmp_base_dir)) for p in result] + (tmp_base_dir.path / file).touch() + result = locate_paths(tmp_base_dir.path, pattern, filters) + result = [str(Path(p).relative_to(tmp_base_dir.path)) for p in result] self.assertCountEqual(expected_results, result) def test_locate_path(self) -> None: with TemporaryDirectory() as tmp_base_dir: - test_file = tmp_base_dir + "/test" - Path(test_file).touch() - self.assertEqual(test_file, locate_path(tmp_base_dir, ["test"], [os.path.isfile])) + test_file = tmp_base_dir.path / "test" + test_file.touch() + self.assertEqual(str(test_file), locate_path(tmp_base_dir.path, ["test"], [os.path.isfile])) def test_locate_path_no_matches(self) -> None: with TemporaryDirectory() as tmp_base_dir: with self.assertRaises(PackagingError): - locate_path(tmp_base_dir, ["test"], [os.path.isfile]) + locate_path(tmp_base_dir.path, ["test"], [os.path.isfile]) def test_locate_path_multiple_matches(self) -> None: with TemporaryDirectory() as tmp_base_dir: - Path(tmp_base_dir + "/file").touch() - Path(tmp_base_dir + "/file2").touch() + (tmp_base_dir.path / "file").touch() + (tmp_base_dir.path / "file2").touch() with self.assertRaises(PackagingError): - locate_path(tmp_base_dir, ["file", "file2"]) + locate_path(tmp_base_dir.path, ["file", "file2"]) @unittest.skipIf(is_windows(), "Windows not supported for this test") def test_locate_executable(self) -> None: with TemporaryDirectory() as tmp_base_dir: - Path(tmp_base_dir + "/test_file").touch() - Path(tmp_base_dir + "/test_file2").touch(0o755) + (tmp_base_dir.path / "test_file").touch() + (tmp_base_dir.path / "test_file2").touch(0o755) # File with executable bit not set should throw an error with self.assertRaises(PackagingError): - locate_executable(tmp_base_dir, ["test_file"]) + locate_executable(tmp_base_dir.path, ["test_file"]) self.assertEqual( - locate_executable(tmp_base_dir, ["test_file2"]), - tmp_base_dir + "/test_file2") + locate_executable(tmp_base_dir.path, ["test_file2"]), + str(tmp_base_dir.path / "test_file2")) @data( # type: ignore ("/home/qt/bin/foo/bar", "/home/qt/lib", "../../../lib"), diff --git a/packaging-tools/tests/test_build_wrapper.py b/packaging-tools/tests/test_build_wrapper.py index 47a9ac157..3082f219c 100644 --- a/packaging-tools/tests/test_build_wrapper.py +++ b/packaging-tools/tests/test_build_wrapper.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -29,15 +29,13 @@ # ############################################################################# -import os import unittest from getpass import getuser -from glob import glob from pathlib import Path -from tempfile import TemporaryDirectory from typing import Dict from ddt import data, ddt, unpack # type: ignore +from temppathlib import TemporaryDirectory from build_wrapper import init_snapshot_dir_and_upload_files @@ -56,25 +54,26 @@ class TestBuildWrapper(unittest.TestCase): self, project_name: str, version_branch: str, build_number: str, subdir: str = "" ) -> None: with TemporaryDirectory() as temp_dir: + work_dir = Path.cwd() option_dict: Dict[str, str] = { - 'WORK_DIR': str(Path.cwd()), 'SSH_COMMAND': 'ssh', 'SCP_COMMAND': 'scp', + 'WORK_DIR': str(work_dir), 'SSH_COMMAND': 'ssh', 'SCP_COMMAND': 'scp', 'PACKAGE_STORAGE_SERVER_ADDR': getuser() + '@127.0.0.1', - 'PACKAGE_STORAGE_SERVER_BASE_DIR': temp_dir + 'PACKAGE_STORAGE_SERVER_BASE_DIR': str(temp_dir.path) } - files_to_upload = [os.path.basename(x) for x in glob('./*.sh')] + files_to_upload = [x.name for x in work_dir.glob('*.sh')] init_snapshot_dir_and_upload_files( option_dict, project_name, version_branch, build_number, files_to_upload, subdir ) - remote_path_base = os.path.join(temp_dir, project_name, version_branch) - remote_path_snapshot_dir = os.path.join(remote_path_base, build_number) - remote_path_latest_link = os.path.join(remote_path_base, 'latest') - self.assertTrue(os.path.isdir(remote_path_base)) - self.assertTrue(os.path.isdir(remote_path_snapshot_dir)) - self.assertTrue(os.path.islink(remote_path_latest_link)) + remote_path_base = temp_dir.path / project_name / version_branch + remote_path_snapshot_dir = remote_path_base / build_number + remote_path_latest_link = remote_path_base / 'latest' + self.assertTrue(remote_path_base.is_dir()) + self.assertTrue(remote_path_snapshot_dir.is_dir()) + self.assertTrue(remote_path_latest_link.is_symlink()) - search_dir = os.path.join(remote_path_latest_link, subdir, '*.sh') - uploaded_files = [os.path.basename(x) for x in glob(search_dir)] + search_dir = remote_path_latest_link / subdir + uploaded_files = [x.name for x in search_dir.glob('*.sh')] self.assertListEqual(sorted(files_to_upload), sorted(uploaded_files)) diff --git a/packaging-tools/tests/test_content_cleaner.py b/packaging-tools/tests/test_content_cleaner.py index 5a3bbb768..377252894 100644 --- a/packaging-tools/tests/test_content_cleaner.py +++ b/packaging-tools/tests/test_content_cleaner.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -32,10 +32,10 @@ import os import unittest -from tempfile import TemporaryDirectory from typing import List from ddt import data, ddt, unpack # type: ignore +from temppathlib import TemporaryDirectory from content_cleaner import preserve_content, remove_content, remove_empty_directories @@ -105,11 +105,11 @@ class TestContentCleaner(unittest.TestCase): preserve_rules: List[str], ) -> None: with TemporaryDirectory() as tmp_base_dir: - test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") - self.generate_test_content(test_base_dir, test_content) - preserve_content(test_base_dir, preserve_rules) + test_base_dir = tmp_base_dir.path / "test-base-dir" + self.generate_test_content(str(test_base_dir), test_content) + preserve_content(str(test_base_dir), preserve_rules) for item in expected_result: - self.assertTrue(os.path.isfile(os.path.join(test_base_dir, item))) + self.assertTrue((test_base_dir / item).is_file()) @data( # type: ignore ( @@ -148,13 +148,13 @@ class TestContentCleaner(unittest.TestCase): ) -> None: try: with TemporaryDirectory() as tmp_base_dir: - test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") - self.generate_test_content(test_base_dir, test_content) - remove_content(test_base_dir, remove_rules) + test_base_dir = tmp_base_dir.path / "test-base-dir" + self.generate_test_content(str(test_base_dir), test_content) + remove_content(str(test_base_dir), remove_rules) for file_path in verify_removed_files: for path in test_content: - if file_path in os.path.join(test_base_dir, path): - self.assertFalse(os.path.isfile(os.path.join(test_base_dir, path))) + if file_path in str(test_base_dir / path): + self.assertFalse((test_base_dir / path).is_file()) # Python 3.7 and below will throw FileNotFoundError on cleanup when exiting context # if the TemporaryDirectory was removed. except FileNotFoundError: @@ -168,15 +168,13 @@ class TestContentCleaner(unittest.TestCase): def test_remove_empty_directories(self, test_content: List[str], remove_dir: bool) -> None: try: with TemporaryDirectory() as tmp_base_dir: - test_base_dir = os.path.join(tmp_base_dir, "test-base-dir") - self.generate_test_content(test_base_dir, test_content) - remove_empty_directories(test_base_dir) + test_base_dir = tmp_base_dir.path / "test-base-dir" + self.generate_test_content(str(test_base_dir), test_content) + remove_empty_directories(str(test_base_dir)) for path in test_content: - verify_path = os.path.join(test_base_dir, path) - if remove_dir: - self.assertFalse(os.path.exists(verify_path)) - else: - self.assertTrue(os.path.exists(verify_path)) + verify_path = test_base_dir / path + exists = verify_path.exists() + self.assertTrue(not exists if remove_dir else exists) # Python 3.7 and below will throw FileNotFoundError on cleanup when exiting context # if the temporary directory was removed. except FileNotFoundError: diff --git a/packaging-tools/tests/test_create_installer.py b/packaging-tools/tests/test_create_installer.py index 34e17d694..e13dfa756 100644 --- a/packaging-tools/tests/test_create_installer.py +++ b/packaging-tools/tests/test_create_installer.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -32,10 +32,10 @@ import os import unittest from pathlib import Path -from tempfile import TemporaryDirectory from typing import Optional, Tuple from ddt import data, ddt # type: ignore +from temppathlib import TemporaryDirectory from bld_utils import is_macos, is_windows from bldinstallercommon import locate_paths @@ -46,7 +46,7 @@ from sdkcomponent import IfwSdkComponent @ddt class TestCommon(unittest.TestCase): @data(( # type: ignore - ("/bin/", "/lib/", "/qml/", "/plugins/", "/unrelated/"), + ("bin", "lib", "qml", "plugins", "unrelated"), ( "Foo.lib", "Food.lib", "dd.qml", "Bar.exe", "Bard.exe", "Qt3d.dll", "Qt3dd.dll" ) if is_windows() else ( @@ -59,16 +59,17 @@ class TestCommon(unittest.TestCase): @unittest.skipIf(not(is_windows() or is_macos()), "This test is only for Windows and macOS") def test_remove_all_debug_libraries_win(self, test_data: Tuple[str, str, str]) -> None: dirs, files, remaining_files = test_data - with TemporaryDirectory() as tmpdir: + with TemporaryDirectory() as tmp_dir: + tmp_path = tmp_dir.path for directory in dirs: - Path(tmpdir + directory).mkdir() + (tmp_path / directory).mkdir() for file in files: - Path(tmpdir + directory + file).touch() - remove_all_debug_libraries(tmpdir) + (tmp_path / directory / file).touch() + remove_all_debug_libraries(str(tmp_path)) for directory in dirs: - result_paths = locate_paths(tmpdir + directory, ["*"], [os.path.isfile]) - result_rel = [str(Path(p).relative_to(tmpdir + directory)) for p in result_paths] - if directory == "/unrelated/": + result_paths = locate_paths(tmp_path / directory, ["*"], [os.path.isfile]) + result_rel = [str(Path(p).relative_to(tmp_path / directory)) for p in result_paths] + if directory == "unrelated": self.assertCountEqual(result_rel, files) else: self.assertCountEqual(result_rel, remaining_files) @@ -81,7 +82,7 @@ class TestCommon(unittest.TestCase): sha, exp = test_data sdk_comp = IfwSdkComponent("", "", "", "", "", "", "", "", "", "", "") # type: ignore with TemporaryDirectory() as tmpdir: - test_sha = Path(tmpdir) / "test" + test_sha = tmpdir.path / "test" test_sha.write_text(sha, encoding="utf-8") read_component_sha(sdk_comp, test_sha) self.assertEqual(sdk_comp.component_sha1, exp) @@ -95,7 +96,7 @@ class TestCommon(unittest.TestCase): def test_read_component_sha_invalid_content(self, test_sha1: str) -> None: sdk_comp = IfwSdkComponent("", "", "", "", "", "", "", "", "", "", "") # type: ignore with TemporaryDirectory() as tmpdir: - test_sha = Path(tmpdir) / "test" + test_sha = tmpdir.path / "test" test_sha.write_text(test_sha1, encoding="utf-8") with self.assertRaises(CreateInstallerError): read_component_sha(sdk_comp, test_sha) @@ -104,7 +105,7 @@ class TestCommon(unittest.TestCase): sdk_comp = IfwSdkComponent("", "", "", "", "", "", "", "", "", "", "") # type: ignore with TemporaryDirectory() as tmpdir: with self.assertRaises(CreateInstallerError): - read_component_sha(sdk_comp, Path(tmpdir) / "invalid") + read_component_sha(sdk_comp, tmpdir.path / "invalid") if __name__ == "__main__": diff --git a/packaging-tools/tests/test_installer_utils.py b/packaging-tools/tests/test_installer_utils.py index c95c8d377..3ae821fe1 100644 --- a/packaging-tools/tests/test_installer_utils.py +++ b/packaging-tools/tests/test_installer_utils.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -34,7 +34,8 @@ import os import tarfile import unittest from pathlib import Path -from tempfile import TemporaryDirectory + +from temppathlib import TemporaryDirectory from installer_utils import ( PackagingError, @@ -56,11 +57,9 @@ class TestInstallerUtils(unittest.TestCase): @asyncio_test async def test_ch_dir(self) -> None: - cwd = Path.cwd() with TemporaryDirectory() as tmp_base_dir: - with ch_dir(tmp_base_dir): - self.assertEqual(Path(tmp_base_dir), Path.cwd()) - self.assertEqual(cwd, Path.cwd()) + with ch_dir(str(tmp_base_dir.path)): + self.assertEqual(tmp_base_dir.path, Path.cwd()) @asyncio_test_parallel_data( # type: ignore ("https://www.qt.io", False), @@ -91,26 +90,28 @@ class TestInstallerUtils(unittest.TestCase): async def test_extract_archive(self) -> None: with TemporaryDirectory() as tmp_base_dir: # create some test paths - temp_path = os.path.join("foo", "bar") - absolute_temp_path = os.path.join(tmp_base_dir, temp_path) - os.makedirs(absolute_temp_path) + temp_path = Path("foo", "bar") + absolute_temp_path = tmp_base_dir.path / temp_path + absolute_temp_path.mkdir(parents=True) # create tmp file temp_file_name = "foobar.txt" - temp_file_path = os.path.join(absolute_temp_path, temp_file_name) - with open(temp_file_path, 'w+', encoding="utf-8") as handle: + temp_file_path = absolute_temp_path / temp_file_name + with temp_file_path.open('w+', encoding="utf-8") as handle: handle.write("\n") - self.assertTrue(os.path.isfile(temp_file_path)) + self.assertTrue(temp_file_path.is_file()) # create fake tar archive - tar_archive_path = os.path.join(tmp_base_dir, "foobar.tar") + tar_archive_path = tmp_base_dir.path / "foobar.tar" with tarfile.open(tar_archive_path, "w") as tar_file: - with open(temp_file_path, mode='rb') as _temp_file: + with temp_file_path.open(mode='rb') as _temp_file: file_data = _temp_file.read() - tar_file.addfile(tarfile.TarInfo(os.path.join(temp_path, temp_file_name)), io.BytesIO(file_data)) + tar_file.addfile( + tarfile.TarInfo(str(temp_path / temp_file_name)), io.BytesIO(file_data) + ) - dest_dir = os.path.join(tmp_base_dir, "dest_dir") - await extract_archive(tar_archive_path, dest_dir) - self.assertTrue(os.path.isfile(os.path.join(dest_dir, temp_path, "foobar.txt"))) + dest_dir = tmp_base_dir.path / "dest_dir" + await extract_archive(str(tar_archive_path), str(dest_dir)) + self.assertTrue((dest_dir / temp_path / "foobar.txt").is_file()) @unittest.skipUnless(is_internal_file_server_reachable(), "Skipping because file server is not accessible") @@ -119,7 +120,7 @@ class TestInstallerUtils(unittest.TestCase): with TemporaryDirectory() as tmp_base_dir: pkg_srv = get_pkg_value("PACKAGE_STORAGE_SERVER_PATH_HTTP") test_file_url = pkg_srv + "/archive/packaging/qtsdk_testing.txt" - downloaded_file = download_archive(test_file_url, tmp_base_dir) + downloaded_file = download_archive(test_file_url, str(tmp_base_dir.path)) self.assertTrue(os.path.isfile(downloaded_file)) diff --git a/packaging-tools/tests/test_packaging.py b/packaging-tools/tests/test_packaging.py index 1fb16b2bc..570d58153 100755 --- a/packaging-tools/tests/test_packaging.py +++ b/packaging-tools/tests/test_packaging.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -34,8 +34,8 @@ import platform import sys import unittest from fileinput import FileInput -from shutil import rmtree -from tempfile import mkdtemp + +from temppathlib import TemporaryDirectory from create_installer import parse_package_finalize_items from patch_qt import ( @@ -119,18 +119,18 @@ class TestPackaging(unittest.TestCase): self.assertEqual(match_count, len(data[2])) def test_patch_qt_edition(self) -> None: - temp_dir = mkdtemp() - temp_file = os.path.join(temp_dir, "qconfig.pri") + with TemporaryDirectory() as temp_dir: + temp_path = temp_dir.path + temp_file = temp_path / "qconfig.pri" - try: - with open(temp_file, "a", encoding="utf-8") as handle: + with temp_file.open("a", encoding="utf-8") as handle: handle.write("something foo\n") handle.write("QT_EDITION = foobar\n") handle.write("nonsense\n") licheck_name = "licheck_foo" release_timestamp = "11223344" - patch_qt_edition(temp_dir, licheck_name, release_timestamp) + patch_qt_edition(str(temp_path), licheck_name, release_timestamp) expected_data = [] expected_data.append("something foo") @@ -144,8 +144,6 @@ class TestPackaging(unittest.TestCase): msg = f"Received data: [{line}] differs from expected data: [{expected_data[idx]}]" self.assertEqual(line.strip(), expected_data[idx], msg) idx += 1 - finally: - rmtree(temp_dir) @unittest.skipUnless(os.environ.get("PKG_TEST_QT_CONFIG_BASE_PATH"), "Skipping because 'PKG_TEST_QT_CONFIG_BASE_PATH' is not set") @unittest.skipUnless(os.environ.get("PKG_TEST_QT_ARTIFACTS_URL"), "Skipping because 'PKG_TEST_QT_CONFIG_BASE_PATH' is not set") diff --git a/packaging-tools/tests/test_release_repo_meta_update.py b/packaging-tools/tests/test_release_repo_meta_update.py index bf9b1ce94..643a7ea70 100755 --- a/packaging-tools/tests/test_release_repo_meta_update.py +++ b/packaging-tools/tests/test_release_repo_meta_update.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -31,10 +31,10 @@ import os import unittest -from tempfile import TemporaryDirectory from typing import List from ddt import ddt # type: ignore +from temppathlib import TemporaryDirectory from release_repo_meta_update import ( BACKUP_SUFFIX, @@ -105,49 +105,53 @@ class TestReleaseRepoMetaUpdate(unittest.TestCase): @asyncio_test async def test_scan_repositories(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - self._write_test_repo(tmp_base_dir, self.paths) + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + self._write_test_repo(str(tmp_base_dir), self.paths) - done_repos, pending_repos, unconverted_repos, broken_repos = scan_repositories(tmp_base_dir) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in broken_repos]), + done_repos, pending_repos, unconverted_repos, broken_repos = scan_repositories(str(tmp_base_dir)) + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in broken_repos]), sorted(["/repo9" + CONVERT_SUFFIX])) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in unconverted_repos]), + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in unconverted_repos]), sorted(["/repo1", "/repo2", "/repo3", "/repo4", "/repo6", "/repo8", "/repo9"])) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in pending_repos]), + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in pending_repos]), sorted(["/repo2" + CONVERT_SUFFIX, "/repo3" + CONVERT_SUFFIX, "/repo7" + CONVERT_SUFFIX])) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in done_repos]), + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in done_repos]), sorted(["/repo5", "/repo7"])) @asyncio_test async def test_check_repos_which_can_be_updated(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - self._write_test_repo(tmp_base_dir, self.paths) - done_repos, pending_repos, unconverted_repos, _ = scan_repositories(tmp_base_dir) + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + self._write_test_repo(str(tmp_base_dir), self.paths) + done_repos, pending_repos, unconverted_repos, _ = scan_repositories(str(tmp_base_dir)) updatable_repos, existing_pending_repos = check_repos_which_can_be_updated(done_repos + pending_repos + unconverted_repos) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in updatable_repos]), + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in updatable_repos]), sorted(["/repo1", "/repo4", "/repo5", "/repo6", "/repo8"])) - self.assertListEqual(sorted([repo.split(tmp_base_dir)[-1] for repo in existing_pending_repos]), + self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in existing_pending_repos]), sorted(["/repo2", "/repo3", "/repo7", "/repo9"])) @asyncio_test async def test_swap_repositories_invalid(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - self._write_test_repo(tmp_base_dir, self.paths) - unconverted_repos = scan_repositories(tmp_base_dir)[2] + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + self._write_test_repo(str(tmp_base_dir), self.paths) + unconverted_repos = scan_repositories(str(tmp_base_dir))[2] with self.assertRaises(IfwRepoUpdateError): await create_converted_repositories(repogen="foobar-repogen", repositories_to_migrate=unconverted_repos, dry_run=True) @asyncio_test async def test_swap_repositories_valid(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - self._write_test_repo(tmp_base_dir, self.non_migrated_paths) - unconverted_repos = scan_repositories(tmp_base_dir)[2] - successful_conversions, failed_conversions = await create_converted_repositories(repogen="foobar-repogen", - repositories_to_migrate=unconverted_repos, - dry_run=True) + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + self._write_test_repo(str(tmp_base_dir), self.non_migrated_paths) + unconverted_repos = scan_repositories(str(tmp_base_dir))[2] + successful_conversions, failed_conversions = await create_converted_repositories( + repogen="foobar-repogen", repositories_to_migrate=unconverted_repos, dry_run=True + ) self.assertTrue(not failed_conversions) # as it was dry-run we need to create the dummy migrated repo directories here for _, migrated_repo in successful_conversions.items(): diff --git a/packaging-tools/tests/test_release_repo_updater.py b/packaging-tools/tests/test_release_repo_updater.py index 34bb6fae1..89cd6ca1f 100755 --- a/packaging-tools/tests/test_release_repo_updater.py +++ b/packaging-tools/tests/test_release_repo_updater.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -32,11 +32,12 @@ import os import unittest from configparser import ConfigParser +from pathlib import Path from shutil import rmtree -from tempfile import TemporaryDirectory from typing import List from ddt import ddt # type: ignore +from temppathlib import TemporaryDirectory from installer_utils import PackagingError, ch_dir from read_remote_config import get_pkg_value @@ -111,7 +112,7 @@ async def _get_repogen() -> str: server = "127.0.0.1" server_home = os.path.expanduser("~") with TemporaryDirectory() as temp_dir: - with ch_dir(temp_dir): + with ch_dir(str(temp_dir.path)): return await upload_ifw_to_remote(ifw_tools, server, server_home) @@ -140,48 +141,62 @@ class TestReleaseRepoUpdater(unittest.TestCase): @asyncio_test async def test_upload_pending_repository_content(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - source_repo = os.path.join(tmp_base_dir, "repository") - destination_repo = os.path.join(tmp_base_dir, "destination_online_repository") + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + source_repo = tmp_base_dir / "repository" + destination_repo = tmp_base_dir / "destination_online_repository" - _write_dummy_file(os.path.join(source_repo, "qt.foo.bar1", "meta", "package.xml")) - _write_dummy_file(os.path.join(source_repo, "qt.foo.bar2", "meta", "package.xml")) - _write_dummy_file(os.path.join(source_repo, "Updates.xml")) + _write_dummy_file(str(source_repo / "qt.foo.bar1" / "meta" / "package.xml")) + _write_dummy_file(str(source_repo / "qt.foo.bar2" / "meta" / "package.xml")) + _write_dummy_file(str(source_repo / "Updates.xml")) - upload_pending_repository_content(self.server, source_repo, destination_repo) - self.assertListEqual(sorted(os.listdir(source_repo)), sorted(os.listdir(destination_repo))) + upload_pending_repository_content(self.server, str(source_repo), str(destination_repo)) + self.assertListEqual( + sorted([x.relative_to(source_repo) for x in source_repo.iterdir()]), + sorted([x.relative_to(destination_repo) for x in destination_repo.iterdir()]) + ) @asyncio_test async def test_reset_new_remote_repository(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - remote_source_repo_path = os.path.join(tmp_base_dir, "repository") - remote_target_repo_path = os.path.join(tmp_base_dir, "destination_online_repository") + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + remote_source_repo_path = tmp_base_dir / "repository" + remote_target_repo_path = tmp_base_dir / "destination_online_repository" - _write_dummy_file(os.path.join(remote_source_repo_path, "qt.foo.bar1", "meta", "package.xml")) - _write_dummy_file(os.path.join(remote_source_repo_path, "qt.foo.bar2", "meta", "package.xml")) - _write_dummy_file(os.path.join(remote_source_repo_path, "Updates.xml")) + _write_dummy_file(str(remote_source_repo_path / "qt.foo.bar1" / "meta" / "package.xml")) + _write_dummy_file(str(remote_source_repo_path / "qt.foo.bar2" / "meta" / "package.xml")) + _write_dummy_file(str(remote_source_repo_path / "Updates.xml")) - reset_new_remote_repository(self.server, remote_source_repo_path, remote_target_repo_path) - self.assertTrue(os.path.isfile(os.path.join(remote_target_repo_path, "qt.foo.bar1", "meta", "package.xml"))) - self.assertTrue(os.path.isfile(os.path.join(remote_target_repo_path, "qt.foo.bar2", "meta", "package.xml"))) - self.assertTrue(os.path.isfile(os.path.join(remote_target_repo_path, "Updates.xml"))) + reset_new_remote_repository(self.server, str(remote_source_repo_path), str(remote_target_repo_path)) + self.assertTrue((remote_target_repo_path / "qt.foo.bar1" / "meta" / "package.xml").is_file()) + self.assertTrue((remote_target_repo_path / "qt.foo.bar2" / "meta" / "package.xml").is_file()) + self.assertTrue((remote_target_repo_path / "Updates.xml").is_file()) # existing repository should be automatically be moved as backup - reset_new_remote_repository(self.server, remote_source_repo_path, remote_target_repo_path) - self.assertTrue(os.path.exists(remote_target_repo_path + "____snapshot_backup")) + reset_new_remote_repository(self.server, str(remote_source_repo_path), str(remote_target_repo_path)) + backup_name = remote_target_repo_path.name + "____snapshot_backup" + self.assertTrue((remote_target_repo_path.with_name(backup_name)).exists()) @asyncio_test async def test_create_remote_repository_backup(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - remote_source_repo_path = os.path.join(tmp_base_dir, "repository") - - _write_dummy_file(os.path.join(remote_source_repo_path, "qt.foo.bar1", "meta", "package.xml")) - _write_dummy_file(os.path.join(remote_source_repo_path, "qt.foo.bar2", "meta", "package.xml")) - _write_dummy_file(os.path.join(remote_source_repo_path, "Updates.xml")) - - remote_repo_backup_path = create_remote_repository_backup(self.server, remote_source_repo_path) - self.assertFalse(os.path.exists(remote_source_repo_path)) - self.assertListEqual(sorted(["Updates.xml", "qt.foo.bar1", "qt.foo.bar2"]), sorted(os.listdir(remote_repo_backup_path))) + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + tmp_base_dir = tmp_dir.path + remote_source_repo_path = tmp_base_dir / "repository" + + _write_dummy_file(str(remote_source_repo_path / "qt.foo.bar1" / "meta" / "package.xml")) + _write_dummy_file(str(remote_source_repo_path / "qt.foo.bar2" / "meta" / "package.xml")) + _write_dummy_file(str(remote_source_repo_path / "Updates.xml")) + + remote_repo_backup_path = Path( + create_remote_repository_backup( + self.server, str(remote_source_repo_path) + ) + ) + self.assertFalse(remote_source_repo_path.exists()) + self.assertListEqual( + sorted(["Updates.xml", "qt.foo.bar1", "qt.foo.bar2"]), + sorted([x.name for x in remote_repo_backup_path.iterdir()]) + ) @asyncio_test_parallel_data( # type: ignore (True, True), @@ -216,10 +231,10 @@ class TestReleaseRepoUpdater(unittest.TestCase): @asyncio_test async def test_ensure_ext_repo_paths(self) -> None: - with TemporaryDirectory(prefix="_repo_tmp_") as tmp_base_dir: - expected_repo = os.path.join(tmp_base_dir, "some", "test", "path") - await ensure_ext_repo_paths(self.server, self.server, expected_repo) - self.assertTrue(os.path.isdir(expected_repo)) + with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir: + expected_repo = tmp_dir.path / "some" / "test" / "path" + await ensure_ext_repo_paths(self.server, self.server, str(expected_repo)) + self.assertTrue(expected_repo.is_dir()) @asyncio_test_parallel_data( # type: ignore ("user@server.com:/foo/bar"), diff --git a/packaging-tools/tests/test_runner.py b/packaging-tools/tests/test_runner.py index 1ebd3b532..c1b4f5478 100755 --- a/packaging-tools/tests/test_runner.py +++ b/packaging-tools/tests/test_runner.py @@ -3,7 +3,7 @@ ############################################################################# # -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # Contact: https://www.qt.io/licensing/ # # This file is part of the release tools of the Qt Toolkit. @@ -31,12 +31,11 @@ import asyncio import unittest -from pathlib import Path from subprocess import TimeoutExpired -from tempfile import TemporaryDirectory from typing import Any, Dict, Tuple from ddt import data, ddt # type: ignore +from temppathlib import TemporaryDirectory from bld_utils import is_windows from runner import run_cmd, run_cmd_async @@ -77,16 +76,16 @@ class TestRunner(unittest.TestCase): check_work_dir_args = ["cd"] if is_windows() else ["pwd"] # Test execution_path with TemporaryDirectory() as tmp_base_dir: - output = run_cmd(cmd=check_work_dir_args, cwd=tmp_base_dir) - self.assertEqual(output, tmp_base_dir + "\n") + output = run_cmd(cmd=check_work_dir_args, cwd=tmp_base_dir.path) + self.assertEqual(output, str(tmp_base_dir.path) + "\n") def test_exec_cmd_log_to_file(self) -> None: # Test log_to_file with TemporaryDirectory() as tmp_base_dir: - log_file = Path(tmp_base_dir) / "log" + log_file = tmp_base_dir.path / "log" log_file.touch() - run_cmd(cmd=["echo", "TEST"], cwd=tmp_base_dir, redirect=str(log_file)) - with open(str(log_file), "r", encoding="utf-8") as handle: + run_cmd(cmd=["echo", "TEST"], cwd=tmp_base_dir.path, redirect=log_file) + with log_file.open("r", encoding="utf-8") as handle: self.assertEqual(handle.read().strip(), "TEST") @data( # type: ignore diff --git a/packaging-tools/tests/test_sdkcomponent.py b/packaging-tools/tests/test_sdkcomponent.py index 8c543aa9b..e535d599e 100644 --- a/packaging-tools/tests/test_sdkcomponent.py +++ b/packaging-tools/tests/test_sdkcomponent.py @@ -30,7 +30,6 @@ import os import sys -import tempfile import unittest from configparser import ConfigParser, ExtendedInterpolation from pathlib import Path @@ -38,6 +37,7 @@ from typing import Any, Dict, List, Optional, Tuple from ddt import data, ddt, unpack # type: ignore from htmllistparse import FileEntry # type: ignore +from temppathlib import TemporaryDirectory from urlpath import URL # type: ignore from sdkcomponent import ( @@ -143,10 +143,11 @@ class TestRunner(unittest.TestCase): pkg_template_paths = ifw_pkg_templ_dirs([section]) ifw_sdk_config = ifw_sdk_config_valid(section) - with tempfile.TemporaryDirectory() as tmp_base_dir: + with TemporaryDirectory() as tmp_dir: + temp_path = tmp_dir.path pkg_template_search_dirs: List[str] = [] - create_paths(tmp_base_dir, pkg_template_paths) - pkg_template_search_dirs.append(os.path.join(tmp_base_dir, "pkg_templates")) + create_paths(str(temp_path), pkg_template_paths) + pkg_template_search_dirs.append(str(temp_path / "pkg_templates")) file_share_base_url = "http://fileshare.intra/base/path/" comp = parse_ifw_sdk_comp( @@ -268,16 +269,16 @@ class TestRunner(unittest.TestCase): self.assertEqual(item.requires_patching, expected_requires_patching) def test_archive_resolver(self) -> None: - with tempfile.TemporaryDirectory() as tmp_base_dir: - template_folder = os.path.join(tmp_base_dir, "qt.tools.foo") - data_folder = os.path.join(template_folder, "data") - payload_file = os.path.join(data_folder, "readme.txt") - os.makedirs(data_folder, exist_ok=True) - with open(payload_file, "a", encoding="utf-8"): + with TemporaryDirectory() as tmp_base_dir: + template_folder = tmp_base_dir.path / "qt.tools.foo" + data_folder = template_folder / "data" + payload_file = data_folder / "readme.txt" + data_folder.mkdir(parents=True, exist_ok=True) + with payload_file.open("a", encoding="utf-8"): pass - resolver = ArchiveResolver("http://intranet.local.it/artifacts", template_folder) - self.assertEqual(resolver.resolve_payload_uri("readme.txt").pop(), payload_file) + resolver = ArchiveResolver("http://intranet.local.it/artifacts", str(template_folder)) + self.assertEqual(resolver.resolve_payload_uri("readme.txt").pop(), str(payload_file)) self.assertEqual( resolver.resolve_payload_uri("qt/qtsvg/qtsvg-RHEL_7_4.7z").pop(), "http://intranet.local.it/artifacts/qt/qtsvg/qtsvg-RHEL_7_4.7z", @@ -347,9 +348,9 @@ class TestRunner(unittest.TestCase): self.assertCountEqual(asyncio.run(resolver.resolve_uri_pattern(pattern, None)), expected) def test_locate_pkg_templ_dir_invalid(self) -> None: - with tempfile.TemporaryDirectory() as tmp_base_dir: + with TemporaryDirectory() as tmp_base_dir: with self.assertRaises(IfwSdkError): - locate_pkg_templ_dir([tmp_base_dir], "qt.foo") + locate_pkg_templ_dir([str(tmp_base_dir.path)], "qt.foo") if __name__ == "__main__": -- cgit v1.2.3