Rework fetch_bytes and _fetch_pypi_json patches to make testing easier. Various improvements to test_micropip. The main feature is a new fixture
'mock_fetch' with an add_pkg method that takes a package name, a map
from version to requirements, and a choice of platform. This should
hopefully make writing more tests a lot easier (which is good because
we could use more micropip test coverage but we are limited by the
difficulty of writing good tests).

This also adds a fixture to create distinct dummy package names and
enables @pytest.mark.asyncio to handle the async calls rather than
using `asyncio.get_event_loop.run_until_complete`.
import argparse
import http.server
import os
import pathlib
import socketserver
import sys


class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Access-Control-Allow-Origin", "*")
        super().end_headers()


Handler.extensions_map[".wasm"] = "application/wasm"


def make_parser(parser):
    parser.description = "Start a server with the supplied dist-dir and port."
    parser.add_argument(
        "--dist-dir",
        action="store",
        type=str,
        default="dist",
        help="set the dist directory (default: %(default)s)",
    )
    parser.add_argument(
        "--port",
        action="store",
        type=int,
        default=8000,
        help="set the PORT number (default: %(default)s)",
    )
    return parser


def server(port):
    httpd = socketserver.TCPServer(("", port), Handler)
    return httpd


def main(args):
    build_dir = pathlib.Path(args.build_dir).resolve()
    port = args.port
    httpd = server(port)
    os.chdir(build_dir)
    print(f"serving from {build_dir} at localhost:{port}")
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n...shutting down http server")
        httpd.shutdown()
        sys.exit()


if __name__ == "__main__":
    parser = make_parser(argparse.ArgumentParser())
    args = parser.parse_args()
    main(args)

import argparse
import http.server
import os
import pathlib
import socketserver
import sys


class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Access-Control-Allow-Origin", "*")
        super().end_headers()


Handler.extensions_map[".wasm"] = "application/wasm"


def make_parser(parser):
    parser.description = "Start a server with the supplied dist-dir and port."
    parser.add_argument(
        "--dist-dir",
        action="store",
        type=str,
        default="dist",
        help="set the dist directory (default: %(default)s)",
    )
    parser.add_argument(
        "--port",
        action="store",
        type=int,
        default=8000,
        help="set the PORT number (default: %(default)s)",
    )
    return parser


def server(port):
    httpd = socketserver.TCPServer(("", port), Handler)
    return httpd


def main(args):
    build_dir = pathlib.Path(args.build_dir).resolve()
    port = args.port
    httpd = server(port)
    os.chdir(build_dir)
    print(f"serving from {build_dir} at localhost:{port}")
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n...shutting down http server")
        httpd.shutdown()
        sys.exit()


if __name__ == "__main__":
    parser = make_parser(argparse.ArgumentParser())
    args = parser.parse_args()
    main(args)
