|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The release conda leg smoke-tests its package as Conda Update does (#1855). |
| 3 | +
|
| 4 | +#1821 added ``util/conda_vendor.py --smoke`` to ``conda-dist`` in |
| 5 | +:file:`.github/workflows/cron-conda.yml`, after ``conda build`` and before any |
| 6 | +upload. ``conda`` in :file:`.github/workflows/create-release.yml`, which |
| 7 | +publishes the release packages, had no such step, and its build step did not |
| 8 | +write the ``host_package`` output the step reads. The jobs are read with |
| 9 | +:func:`yaml.safe_load`, and each build step is run with a stand-in ``conda``, |
| 10 | +as ``test_conda_native_platforms_1822.py`` runs it. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import os |
| 17 | +import pathlib |
| 18 | +import re |
| 19 | +import stat |
| 20 | +import subprocess # nosec: B404 |
| 21 | +import tempfile |
| 22 | +import unittest |
| 23 | +from typing import TYPE_CHECKING |
| 24 | + |
| 25 | +from tests._support import scale_timeout |
| 26 | +from tests.project.test_conda_native_platforms_1822 import BASH, CONDA |
| 27 | + |
| 28 | +try: |
| 29 | + import yaml |
| 30 | +except ImportError: # pragma: no cover |
| 31 | + yaml = None # type: ignore[assignment] |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from typing import Any |
| 35 | + |
| 36 | +WORKFLOWS = pathlib.Path(__file__).resolve().parents[2] / '.github' / 'workflows' |
| 37 | +#: The conda jobs, as (workflow, job); the first is the one the release mirrors. |
| 38 | +CRON, RELEASE = ('cron-conda.yml', 'conda-dist'), ('create-release.yml', 'conda') |
| 39 | +#: The steps that publish what the leg built. |
| 40 | +UPLOADS = ('Upload conda packages to Anaconda', 'Upload package to GitHub Release') |
| 41 | + |
| 42 | + |
| 43 | +def steps(workflow: 'str', job_id: 'str') -> 'list[dict[str, Any]]': |
| 44 | + return yaml.safe_load((WORKFLOWS / workflow).read_text(encoding='utf-8'))['jobs'][job_id]['steps'] |
| 45 | + |
| 46 | + |
| 47 | +def smoke_steps(found: 'list[dict[str, Any]]') -> 'list[int]': |
| 48 | + """The index of each step that runs ``util/conda_vendor.py --smoke``.""" |
| 49 | + return [index for index, step in enumerate(found) |
| 50 | + if re.search(r'\butil/conda_vendor\.py\s+--smoke\b', str(step.get('run', '')))] |
| 51 | + |
| 52 | + |
| 53 | +@unittest.skipIf(yaml is None, 'PyYAML, from the test extra, is not installed') |
| 54 | +class TestTheReleaseLegSmokeTests(unittest.TestCase): |
| 55 | + """The release leg checks its package before publishing it.""" |
| 56 | + |
| 57 | + def test_the_smoke_test_runs_after_the_build_and_before_every_upload(self) -> None: |
| 58 | + for workflow, job_id in (CRON, RELEASE): |
| 59 | + found = steps(workflow, job_id) |
| 60 | + smoke = smoke_steps(found) |
| 61 | + with self.subTest(workflow=workflow, job=job_id): |
| 62 | + self.assertEqual(len(smoke), 1, f'{workflow} {job_id} runs no smoke test') |
| 63 | + build = next(index for index, step in enumerate(found) if step.get('id') == 'conda_build') |
| 64 | + self.assertGreater(smoke[0], build) |
| 65 | + uploads = [index for index, step in enumerate(found) if step.get('name') in UPLOADS] |
| 66 | + self.assertEqual(len(uploads), len(UPLOADS)) |
| 67 | + self.assertLess(smoke[0], min(uploads)) |
| 68 | + |
| 69 | + def test_it_is_conda_updates_step(self) -> None: |
| 70 | + """The same interpreter, package and gate: setup-python's, ``host_package``, none.""" |
| 71 | + cron = steps(*CRON) |
| 72 | + release = steps(*RELEASE) |
| 73 | + expected = cron[smoke_steps(cron)[0]] |
| 74 | + self.assertEqual(expected['env'], {'PCAPKIT_CONDA_PACKAGE': '${{ steps.conda_build.outputs.host_package }}'}) |
| 75 | + self.assertIn('"${pythonLocation}/bin/python" util/conda_vendor.py --smoke', expected['run']) |
| 76 | + found = smoke_steps(release) |
| 77 | + self.assertEqual(len(found), 1, 'the release leg runs no smoke test') |
| 78 | + self.assertEqual(release[found[0]], expected) |
| 79 | + |
| 80 | + |
| 81 | +@unittest.skipIf(yaml is None, 'PyYAML, from the test extra, is not installed') |
| 82 | +@unittest.skipIf(not BASH, 'needs bash on PATH') |
| 83 | +class TestEachBuildStepNamesItsPackage(unittest.TestCase): |
| 84 | + """``host_package``, which the smoke step reads, is the package the step built.""" |
| 85 | + |
| 86 | + def outputs(self, workflow: 'str', job_id: 'str') -> 'tuple[str, str]': |
| 87 | + """The build step's ``$GITHUB_OUTPUT`` on ``ubuntu-latest``, and its log.""" |
| 88 | + step = next(step for step in steps(workflow, job_id) if step.get('id') == 'conda_build') |
| 89 | + script = step['run'].replace('${{ matrix.os }}', 'ubuntu-latest').replace('${{ matrix.python-version }}', |
| 90 | + '3.14') |
| 91 | + self.assertNotIn('${{', script) |
| 92 | + with tempfile.TemporaryDirectory() as temp: |
| 93 | + scratch = pathlib.Path(temp) |
| 94 | + (scratch / 'bin').mkdir() |
| 95 | + conda = scratch / 'bin' / 'conda' |
| 96 | + conda.write_text(CONDA, encoding='utf-8') |
| 97 | + conda.chmod(conda.stat().st_mode | stat.S_IXUSR) |
| 98 | + output = scratch / 'output' |
| 99 | + output.write_text('', encoding='utf-8') |
| 100 | + result = subprocess.run( |
| 101 | + [BASH, '-e', '-c', script], cwd=temp, # nosec: B603 |
| 102 | + env=dict(os.environ, PATH=f'{scratch / "bin"}{os.pathsep}{os.environ["PATH"]}', |
| 103 | + TMPDIR=temp, GITHUB_OUTPUT=str(output), FAKE_SUBDIR='linux-64', |
| 104 | + PCAPKIT_VERSION='1.0', PCAPKIT_BUILD='0'), |
| 105 | + capture_output=True, text=True, check=False, timeout=scale_timeout(60)) |
| 106 | + return output.read_text(encoding='utf-8'), result.stdout + result.stderr |
| 107 | + |
| 108 | + def test_host_package_is_the_package_built(self) -> None: |
| 109 | + for workflow, job_id in (CRON, RELEASE): |
| 110 | + written, log = self.outputs(workflow, job_id) |
| 111 | + with self.subTest(workflow=workflow, job=job_id): |
| 112 | + host = re.findall(r'^host_package=(.*)$', written, re.MULTILINE) |
| 113 | + self.assertEqual(len(host), 1, f'no host_package output\n{written}{log}') |
| 114 | + packages = re.search(r'^packages<<EOF\n(.*?)^EOF$', written, re.MULTILINE | re.DOTALL) |
| 115 | + self.assertIsNotNone(packages, log) |
| 116 | + self.assertEqual(host, packages.group(1).splitlines()) # type: ignore[union-attr] |
| 117 | + self.assertEqual(pathlib.PurePosixPath(host[0]).parent.name, 'linux-64') |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + unittest.main() |
0 commit comments