def test_dist()

in utils/build-dists.py [0:0]


def test_dist(dist):
    with set_tmp_dir() as tmp_dir:
        dist_name = re.match(
            r"^(elasticsearch_serverless\d*)-", os.path.basename(dist)
        ).group(1)

        # Build the venv and install the dist
        run("python", "-m", "venv", os.path.join(tmp_dir, "venv"))
        venv_python = os.path.join(tmp_dir, "venv/bin/python")
        run(
            venv_python,
            "-m",
            "pip",
            "install",
            "-U",
            "pip",
            "mypy",
            "numpy",
            "pandas-stubs",
            "opentelemetry-api",
        )
        run(venv_python, "-m", "pip", "install", dist)

        # Test the sync namespaces
        run(venv_python, "-c", f"from {dist_name} import Elasticsearch")
        run(
            venv_python,
            "-c",
            f"from {dist_name}.helpers import scan, bulk, streaming_bulk, reindex",
        )
        run(
            venv_python,
            "-c",
            f"from {dist_name} import Elasticsearch, AsyncElasticsearch",
        )
        run(
            venv_python,
            "-c",
            f"from {dist_name}.helpers import scan, bulk, streaming_bulk, reindex, async_scan, async_bulk, async_streaming_bulk, async_reindex",
        )

        # Install aiohttp and see that async is now available
        run(venv_python, "-m", "pip", "install", "aiohttp")
        run(venv_python, "-c", f"from {dist_name} import AsyncElasticsearch")
        run(
            venv_python,
            "-c",
            f"from {dist_name}.helpers import async_scan, async_bulk, async_streaming_bulk, async_reindex",
        )

        # Only need to test 'async_types' for non-aliased package
        # since 'aliased_types' tests both async and sync.
        if dist_name == "elasticsearch_serverless":
            run(
                venv_python,
                "-m",
                "mypy",
                "--strict",
                "--install-types",
                "--non-interactive",
                "--ignore-missing-imports",
                os.path.join(
                    base_dir, "test_elasticsearch_serverless/test_types/async_types.py"
                ),
            )

        # Ensure that the namespaces are correct for the dist
        for suffix in ("", "1", "2", "5", "6", "7", "8", "9", "10"):
            distx_name = f"elasticsearch_serverless{suffix}"
            run(
                venv_python,
                "-c",
                f"import {distx_name}",
                expect_exit_code=256 if distx_name != dist_name else 0,
            )

        # Check that sync types work for 'elasticsearch_serverless' and
        # that aliased types work for 'elasticsearch_serverlessX'
        if dist_name == "elasticsearch_serverless":
            run(
                venv_python,
                "-m",
                "mypy",
                "--strict",
                "--install-types",
                "--non-interactive",
                "--ignore-missing-imports",
                os.path.join(
                    base_dir, "test_elasticsearch_serverless/test_types/sync_types.py"
                ),
            )
        else:
            run(
                venv_python,
                "-m",
                "mypy",
                "--strict",
                "--install-types",
                "--non-interactive",
                os.path.join(
                    base_dir,
                    "test_elasticsearch_serverless/test_types/aliased_types.py",
                ),
            )

        # Uninstall the dist, see that we can't import things anymore
        run(venv_python, "-m", "pip", "uninstall", "--yes", dist_name)
        run(
            venv_python,
            "-c",
            f"from {dist_name} import Elasticsearch",
            expect_exit_code=256,
        )