|
| 1 | +# This is an integration test, rather than a unit test. |
| 2 | +# It should be run in a Python runtime that has been installed. |
| 3 | +# The 'pymanager.exe' under test should be first on PATH |
| 4 | + |
| 5 | +import json |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +EXIT_SETUP_FAILED = 1 |
| 13 | +EXIT_ALIAS_NOT_CREATED = 2 |
| 14 | +EXIT_ALIAS_INVALID = 3 |
| 15 | + |
| 16 | +CLEANUP = [] |
| 17 | + |
| 18 | +def run(*args, **kwargs): |
| 19 | + print("##[command]", end="") |
| 20 | + print(*args) |
| 21 | + with subprocess.Popen( |
| 22 | + args, |
| 23 | + stdout=kwargs.pop("stdout", subprocess.PIPE), |
| 24 | + stderr=kwargs.pop("stderr", subprocess.STDOUT), |
| 25 | + encoding=kwargs.pop("encoding", "ascii"), |
| 26 | + errors=kwargs.pop("errors", "replace"), |
| 27 | + **kwargs, |
| 28 | + ) as p: |
| 29 | + out, err = p.communicate(None) |
| 30 | + if p.returncode: |
| 31 | + raise subprocess.CalledProcessError(p.returncode, args, out, err) |
| 32 | + return out, err |
| 33 | + |
| 34 | +def main(): |
| 35 | + out, _ = run("pymanager", "list", "-f=json", "-q") |
| 36 | + for install in json.loads(out)["versions"]: |
| 37 | + if not install.get("unmanaged"): |
| 38 | + break |
| 39 | + else: |
| 40 | + print("##[error]No suitable (managed) runtime found.") |
| 41 | + sys.exit(EXIT_SETUP_FAILED) |
| 42 | + |
| 43 | + print("Using", install["display-name"], "from", install["prefix"], "for test") |
| 44 | + |
| 45 | + prefix = install["prefix"] |
| 46 | + exe = install["executable"] |
| 47 | + |
| 48 | + site = Path(prefix) / "Lib/site-packages" |
| 49 | + if not site.is_dir(): |
| 50 | + print("##[error]Selected runtime has no site-packages folder.") |
| 51 | + sys.exit(EXIT_SETUP_FAILED) |
| 52 | + |
| 53 | + eptest_src = Path(__file__).parent / "eptestpackage" |
| 54 | + if not eptest_src.is_dir(): |
| 55 | + print("##[error]eptestpackage is missing from test script location.") |
| 56 | + sys.exit(EXIT_SETUP_FAILED) |
| 57 | + |
| 58 | + dist_info = site / "eptestpackage-1.0.dist-info" |
| 59 | + dist_info.mkdir(parents=True, exist_ok=True) |
| 60 | + CLEANUP.append(lambda: shutil.rmtree(dist_info)) |
| 61 | + for f in (eptest_src / "eptestpackage.dist-info").glob("*"): |
| 62 | + (dist_info / f.name).write_bytes(f.read_bytes()) |
| 63 | + (site / "eptestpackage.py").write_bytes((eptest_src / "eptestpackage.py").read_bytes()) |
| 64 | + CLEANUP.append((site / "eptestpackage.py").unlink) |
| 65 | + |
| 66 | + print("Listing 'installed' packages (should include eptestpackage)") |
| 67 | + print(*site.glob("*"), sep="\n") |
| 68 | + print() |
| 69 | + |
| 70 | + out, _ = run(exe, "-c", "import eptestpackage; eptestpackage.main()") |
| 71 | + if out.strip() != "eptestpackage:main": |
| 72 | + print(out) |
| 73 | + print("##[error]Failed to import eptestpackage") |
| 74 | + sys.exit(EXIT_SETUP_FAILED) |
| 75 | + print("Confirmed eptestpackage is importable") |
| 76 | + |
| 77 | + out, _ = run("pymanager", "list", "-f=config", "-q") |
| 78 | + try: |
| 79 | + config = json.loads(out) |
| 80 | + except json.JSONDecodeError: |
| 81 | + print("py list -f=config output:") |
| 82 | + print(out) |
| 83 | + raise |
| 84 | + bin_dir = Path(config["global_dir"]) |
| 85 | + print(bin_dir) |
| 86 | + |
| 87 | + refresh_log, _ = run("pymanager", "install", "--refresh", "-vv") |
| 88 | + CLEANUP.append(lambda: run("pymanager", "install", "--refresh")) |
| 89 | + |
| 90 | + print("Listing global aliases (should include eptest, eptestw, eptest-refresh)") |
| 91 | + print(*bin_dir.glob("eptest*"), sep="\n") |
| 92 | + |
| 93 | + for n in ["eptest.exe", "eptestw.exe", "eptest-refresh.exe"]: |
| 94 | + if not (bin_dir / n).is_file(): |
| 95 | + print("--refresh log follows") |
| 96 | + print(refresh_log) |
| 97 | + print("##[error]Did not create", n) |
| 98 | + sys.exit(EXIT_ALIAS_NOT_CREATED) |
| 99 | + |
| 100 | + out, _ = run(bin_dir / "eptest.exe") |
| 101 | + print(out) |
| 102 | + if out.strip() != "eptestpackage:main": |
| 103 | + print("##[error]eptest.exe alias failed") |
| 104 | + sys.exit(EXIT_ALIAS_INVALID) |
| 105 | + |
| 106 | + out, _ = run(bin_dir / "eptestw.exe") |
| 107 | + print(out) |
| 108 | + if out.strip() != "eptestpackage:mainw": |
| 109 | + print("##[error]eptestw.exe alias failed") |
| 110 | + sys.exit(EXIT_ALIAS_INVALID) |
| 111 | + |
| 112 | + out, _ = run(bin_dir / "eptest-refresh.exe") |
| 113 | + print(out) |
| 114 | + if not out.strip().endswith("eptestpackage:do_refresh"): |
| 115 | + print("##[error]eptest-refresh.exe alias failed") |
| 116 | + sys.exit(EXIT_ALIAS_INVALID) |
| 117 | + |
| 118 | + |
| 119 | +try: |
| 120 | + main() |
| 121 | +finally: |
| 122 | + print("Beginning cleanup") |
| 123 | + while CLEANUP: |
| 124 | + try: |
| 125 | + CLEANUP.pop()() |
| 126 | + except subprocess.CalledProcessError as ex: |
| 127 | + print("Subprocess failed during cleanup:") |
| 128 | + print(ex.args, ex.returncode) |
| 129 | + print(ex.output) |
0 commit comments