|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +from importlib.metadata import PackageNotFoundError, version |
| 4 | +from contextlib import contextmanager |
| 5 | + |
| 6 | + |
| 7 | +@contextmanager |
| 8 | +def run_local_pypi_index(): |
| 9 | + # WARNING : This requires the script to be run from the root of the repo |
| 10 | + p = subprocess.Popen(["python", "-m", "http.server"]) |
| 11 | + try: |
| 12 | + yield |
| 13 | + finally: |
| 14 | + p.terminate() |
| 15 | + |
| 16 | + |
| 17 | +def exists(pkg_name: str) -> bool: |
| 18 | + try: |
| 19 | + version(pkg_name) |
| 20 | + except PackageNotFoundError: |
| 21 | + return False |
| 22 | + else: |
| 23 | + return True |
| 24 | + |
| 25 | + |
| 26 | +def pip_install(pkg_name: str, upgrade: bool = False, version: str = None): |
| 27 | + package_to_install = pkg_name if version is None else f"{pkg_name}=={version}" |
| 28 | + cmd = ["python", "-m", "pip", "install", package_to_install, "--upgrade" if upgrade else "", "--extra-index-url", "http://localhost:8000"] |
| 29 | + subprocess.run([c for c in cmd if c]) |
| 30 | + |
| 31 | + |
| 32 | +def pip_uninstall(pkg_name: str): |
| 33 | + subprocess.run(["python", "-m", "pip", "uninstall", pkg_name, "-y"]) |
| 34 | + |
| 35 | + |
| 36 | +def register(pkg_name: str, pkg_version: str, pkg_link: str): |
| 37 | + env = os.environ.copy() |
| 38 | + env["PKG_ACTION"] = "REGISTER" |
| 39 | + env["PKG_NAME"] = pkg_name |
| 40 | + env["PKG_VERSION"] = pkg_version |
| 41 | + env["PKG_AUTHOR"] = "Dummy author" |
| 42 | + env["PKG_SHORT_DESC"] = "Dummy Description" |
| 43 | + env["PKG_HOMEPAGE"] = pkg_link |
| 44 | + subprocess.run(["python", ".github/actions.py"], env=env) |
| 45 | + |
| 46 | + |
| 47 | +def update(pkg_name: str, pkg_version: str): |
| 48 | + env = os.environ.copy() |
| 49 | + env["PKG_ACTION"] = "UPDATE" |
| 50 | + env["PKG_NAME"] = pkg_name |
| 51 | + env["PKG_VERSION"] = pkg_version |
| 52 | + subprocess.run(["python", ".github/actions.py"], env=env) |
| 53 | + |
| 54 | + |
| 55 | +def delete(pkg_name: str): |
| 56 | + env = os.environ.copy() |
| 57 | + env["PKG_ACTION"] = "DELETE" |
| 58 | + env["PKG_NAME"] = pkg_name |
| 59 | + subprocess.run(["python", ".github/actions.py"], env=env) |
| 60 | + |
| 61 | + |
| 62 | +def run_tests(): |
| 63 | + # This test is checking that the Github actions for registering, updating, |
| 64 | + # and deleting are working and the PyPi index is updated accordingly. |
| 65 | + # What we do is : |
| 66 | + # * Serve the HTML locally so we have a local PyPi index |
| 67 | + # * Run the actions for registering, updating, and deleting packages in |
| 68 | + # this local PyPi |
| 69 | + # * In-between, run some basic checks to see if the installation is |
| 70 | + # working properly |
| 71 | + with run_local_pypi_index(): |
| 72 | + # First, make sure the package is not installed |
| 73 | + assert not exists("public-hello") |
| 74 | + |
| 75 | + # The package `public-hello` is already registered in our local PyPi |
| 76 | + # ACTION : Let's remove it from our local PyPi |
| 77 | + delete("public-hello") |
| 78 | + |
| 79 | + # Trying to install the package, only the version uploaded to PyPi (0.0.0) |
| 80 | + # will be detected and installed (the version in our local PyPi was |
| 81 | + # successfully deleted) |
| 82 | + pip_install("public-hello") |
| 83 | + assert exists("public-hello") and version("public-hello") == "0.0.0" |
| 84 | + |
| 85 | + # ACTION : Register the package, to make it available again |
| 86 | + register("public-hello", "0.1", "https://github.com/astariul/public-hello") |
| 87 | + |
| 88 | + # Now we can install it from the local PyPi |
| 89 | + pip_install("public-hello", upgrade=True) |
| 90 | + assert exists("public-hello") and version("public-hello") == "0.1" |
| 91 | + |
| 92 | + # ACTION : Update the package with a new version |
| 93 | + update("public-hello", "0.2") |
| 94 | + |
| 95 | + # We can update the package to the newest version |
| 96 | + pip_install("public-hello", upgrade=True) |
| 97 | + assert exists("public-hello") and version("public-hello") == "0.2" |
| 98 | + |
| 99 | + # We should still be able to install the old version |
| 100 | + pip_install("public-hello", version="0.1") |
| 101 | + assert exists("public-hello") and version("public-hello") == "0.1" |
| 102 | + |
| 103 | + # Uninstall the package (for consistency with the initial state) |
| 104 | + pip_uninstall("public-hello") |
| 105 | + assert not exists("public-hello") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + run_tests() |
0 commit comments