diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 00000000..738bb8ea --- /dev/null +++ b/scripts/__init__.py @@ -0,0 +1,19 @@ +"""Build and verification tooling for the collection. + +These modules are run as scripts (``python scripts/verify.py``), as modules +(``python -m scripts.scraper.libretro_scraper``), and imported as a package by +the tests and the type checker. The first form puts this directory on the path +itself; the other two do not, so importing the package adds it. Without that, +``import scripts.common`` fails on the first sibling import it reaches, and +every absolute import between siblings would have to be rewritten to be +relative -a change that would break running a script directly. +""" + +from __future__ import annotations + +import os +import sys + +_HERE = os.path.dirname(os.path.abspath(__file__)) +if _HERE not in sys.path: + sys.path.insert(0, _HERE) diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 15d07f21..d590c713 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -926,3 +926,34 @@ class FreshnessGuardMechanics(unittest.TestCase): if __name__ == "__main__": unittest.main() + + +class ScriptsImportThreeWays(unittest.TestCase): + """A script is run directly, run as a module, and imported as a package. + + The three do not agree on what is on the path: only the first form adds + the scripts directory. Adding the package marker without the bootstrap + made `import scripts.common` fail on the first sibling import it reached. + """ + + def _run(self, *args: str): + import subprocess + + return subprocess.run( + [sys.executable, *args], + capture_output=True, text=True, cwd=str(ROOT), timeout=300, + ) + + def test_imported_as_a_package(self): + result = self._run( + "-c", "import scripts.common, scripts.verify, scripts.generate_pack" + ) + self.assertEqual(result.returncode, 0, result.stderr[-400:]) + + def test_run_as_a_module(self): + result = self._run("-m", "scripts.scraper.libretro_scraper", "--help") + self.assertEqual(result.returncode, 0, result.stderr[-400:]) + + def test_run_as_a_script(self): + result = self._run("scripts/list_platforms.py") + self.assertEqual(result.returncode, 0, result.stderr[-400:])