From 913ba096009ee2d3dc6d8094307d65ce0a96d093 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 6 Oct 2026 13:59:50 +0200 Subject: [PATCH] chore: hoist test imports to module level --- tests/test_extract_targets.py | 4 ++-- tests/test_file_consumers.py | 4 ++-- tests/test_scraper_contract.py | 31 ++++++++----------------------- tests/test_site_availability.py | 5 ++--- tests/test_split_names.py | 6 +++--- tests/test_split_pack.py | 6 ++---- tests/test_standalone_mode.py | 9 ++++----- tests/test_target_verify.py | 4 ++-- tests/test_truth_diff.py | 9 ++++++--- 9 files changed, 31 insertions(+), 47 deletions(-) diff --git a/tests/test_extract_targets.py b/tests/test_extract_targets.py index fb969ba8..37a78089 100644 --- a/tests/test_extract_targets.py +++ b/tests/test_extract_targets.py @@ -16,8 +16,8 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -from common import load_platform_config, load_platform_registry -from generate_readme import extract_notes, extract_targets +from common import load_platform_config, load_platform_registry # noqa: E402 +from generate_readme import extract_notes, extract_targets # noqa: E402 PLATFORMS = str(REPO_ROOT / "platforms") diff --git a/tests/test_file_consumers.py b/tests/test_file_consumers.py index 6dce611b..837a9c7d 100644 --- a/tests/test_file_consumers.py +++ b/tests/test_file_consumers.py @@ -17,8 +17,8 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -from cross_reference import FileConsumers -from generate_site import generate_system_page +from cross_reference import FileConsumers # noqa: E402 +from generate_site import generate_system_page # noqa: E402 _TMP = tempfile.TemporaryDirectory() _ROOT = Path(_TMP.name) diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index acf24280..0d1ab7ef 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -10,17 +10,23 @@ exercised is a promise to contributors backed by nothing. from __future__ import annotations +import importlib import sys import unittest +import urllib.error from pathlib import Path +from unittest import mock REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) +from scraper import emudeck_scraper, libretro_scraper # noqa: E402 from scraper.base_scraper import ( # noqa: E402 BaseScraper, BiosRequirement, ChangeSet, + fetch_github_latest_version, + github_headers, ) @@ -238,14 +244,9 @@ class UnreadableReleaseStopsTheScrape(unittest.TestCase): @staticmethod def _refuse(*_args, **_kwargs): - import urllib.error - raise urllib.error.HTTPError("https://api.github.com/x", 403, "rate limited", {}, None) def test_every_pinned_scraper_raises(self): - import importlib - from unittest import mock - for name in ( "scraper.bizhawk_scraper", "scraper.romm_scraper", @@ -259,45 +260,29 @@ class UnreadableReleaseStopsTheScrape(unittest.TestCase): module.Scraper() def test_a_release_lookup_raises(self): - from unittest import mock - - from scraper.base_scraper import fetch_github_latest_version - with mock.patch("urllib.request.urlopen", self._refuse), self.assertRaises( RuntimeError ): fetch_github_latest_version("libretro/RetroArch") def test_the_token_is_sent(self): - from unittest import mock - - from scraper.base_scraper import github_headers - with mock.patch.dict("os.environ", {"GITHUB_TOKEN": "t0k"}): self.assertEqual(github_headers()["Authorization"], "Bearer t0k") def test_emudeck_cores_raise(self): """EmuDeck wrote cores: [] when the GitHub listing was refused.""" - from unittest import mock - - from scraper.emudeck_scraper import Scraper - with mock.patch("urllib.request.urlopen", self._refuse), self.assertRaises( RuntimeError ): - Scraper()._fetch_installed_emulators() + emudeck_scraper.Scraper()._fetch_installed_emulators() def test_libretro_core_metadata_raises(self): """libretro dropped every system name and manufacturer when the core-info listing was refused.""" - from unittest import mock - - from scraper.libretro_scraper import Scraper - with mock.patch("urllib.request.urlopen", self._refuse), self.assertRaises( RuntimeError ): - Scraper()._fetch_core_metadata() + libretro_scraper.Scraper()._fetch_core_metadata() if __name__ == "__main__": diff --git a/tests/test_site_availability.py b/tests/test_site_availability.py index 19012687..a1e09fbe 100644 --- a/tests/test_site_availability.py +++ b/tests/test_site_availability.py @@ -17,7 +17,8 @@ import yaml REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -from generate_site import _availability_check, _file_badges +from common import load_database # noqa: E402 +from generate_site import _availability_check, _file_badges # noqa: E402 SHA1 = "a" * 40 DB = { @@ -40,8 +41,6 @@ class UnsourceableIsNotHeld(unittest.TestCase): self.assertNotIn("in repo", badges) def test_no_profile_entry_is_shown_held(self): - from common import load_database - db_path = REPO_ROOT / "database.json" if not db_path.exists(): self.skipTest("database.json is not built") diff --git a/tests/test_split_names.py b/tests/test_split_names.py index ecab4b93..5ae9ed83 100644 --- a/tests/test_split_names.py +++ b/tests/test_split_names.py @@ -21,9 +21,9 @@ import yaml REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -import common -from common import build_zip_contents_index, compute_hashes -from generate_pack import generate_split_packs +import common # noqa: E402 +from common import build_zip_contents_index, compute_hashes # noqa: E402 +from generate_pack import generate_split_packs # noqa: E402 class SplitPartNames(unittest.TestCase): diff --git a/tests/test_split_pack.py b/tests/test_split_pack.py index e6c9ccfe..f62f3fc0 100644 --- a/tests/test_split_pack.py +++ b/tests/test_split_pack.py @@ -16,6 +16,7 @@ from __future__ import annotations import os import stat +import subprocess import sys import tempfile import unittest @@ -26,6 +27,7 @@ REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) import generate_pack as builder # noqa: E402 +from artifacts import artifact_lock # noqa: E402 import split_pack # noqa: E402 LIMIT = 4096 @@ -141,10 +143,6 @@ class SplitHoldsTheDirectory(SplitFixture): the pack the build had just written.""" def test_a_held_directory_is_left_alone(self): - import subprocess - - from artifacts import artifact_lock - for target in (self.dist, self.pack): with self.subTest(target=target.name), artifact_lock(str(self.dist)): proc = subprocess.run( diff --git a/tests/test_standalone_mode.py b/tests/test_standalone_mode.py index 2de43020..720a0db8 100644 --- a/tests/test_standalone_mode.py +++ b/tests/test_standalone_mode.py @@ -11,12 +11,16 @@ standalone_cores dropped them from its pack. from __future__ import annotations import json +import sys import unittest from pathlib import Path import yaml REPO_ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(REPO_ROOT / "scripts")) + +from cross_reference import cross_reference # noqa: E402 DUAL_PROFILE = { "emulator": "Dual", @@ -73,11 +77,6 @@ class CrossReferenceReadsTheBuildThePlatformRuns(unittest.TestCase): where verify and the pack builder found 26.""" def _gaps(self, standalone_cores): - import sys - - sys.path.insert(0, str(REPO_ROOT / "scripts")) - from cross_reference import cross_reference - empty_db = {"files": {}, "indexes": {}} report = cross_reference( {"dual": DUAL_PROFILE}, {}, empty_db, standalone_cores=standalone_cores diff --git a/tests/test_target_verify.py b/tests/test_target_verify.py index b53080cb..0da3a55f 100644 --- a/tests/test_target_verify.py +++ b/tests/test_target_verify.py @@ -17,8 +17,8 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -from common import build_target_cores_cache -from generate_pack import _target_cores_for +from common import build_target_cores_cache # noqa: E402 +from generate_pack import _target_cores_for # noqa: E402 class CheckReadsTheTargetLikeTheBuild(unittest.TestCase): diff --git a/tests/test_truth_diff.py b/tests/test_truth_diff.py index f8325895..de462aa4 100644 --- a/tests/test_truth_diff.py +++ b/tests/test_truth_diff.py @@ -19,7 +19,12 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "scripts")) -from truth import _diff_system, _match_renames, _merge_file_into_system # noqa: E402 +from truth import ( # noqa: E402 + _diff_system, + _match_renames, + _merge_file_into_system, + generate_platform_truth, +) def _entry(name: str, **hashes) -> dict: @@ -205,8 +210,6 @@ class OnlySystemDirectoryEntries(unittest.TestCase): verify deliberately leave them out of.""" def test_a_file_read_elsewhere_is_not_asked_of_the_platform(self): - from truth import generate_platform_truth - profiles = { "core": { "emulator": "Core",