async def test_http_package__contents_toml__spec_dirs_ignored(
        aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server, web.get("/test/1.0.0/package", package_file_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["package"]
    spec.xml_subdir = "thereisnoxmlhere"
    spec.include_subdir = "adocissomewhereelse"

    packages = await collect([spec], tmp_path)

    assert len(packages) == 1
    pkg = packages[0]
    assert pkg.name == "package"
    assert pkg.scoped is True
    assert pkg.reference_type == "doxygen"

    assert pkg.reference_dir == tmp_path / "test" / "1.0.0" / "xml"
    assert pkg.reference_dir.is_dir()
    assert (pkg.reference_dir / "content.xml").is_file()

    assert pkg.adoc_src_dir == tmp_path / "test" / "1.0.0" / "adoc"
    assert pkg.adoc_src_dir.is_dir()
    assert (pkg.adoc_src_dir / "content.adoc").is_file()

    assert pkg.adoc_image_dir == tmp_path / "test" / "1.0.0" / "images"
    assert pkg.adoc_image_dir.is_dir()
    assert (pkg.adoc_image_dir / "picture.png").is_file()

    assert pkg.adoc_root_doc == tmp_path / "test" / "1.0.0" / "adoc" / "content.adoc"
    assert pkg.adoc_root_doc.is_file()
async def test_http_package__cache_corrupt(aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server, web.get("/test/1.0.0/package", package_file_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["package"]

    (tmp_path / "test" / "1.0.0").mkdir(parents=True)
    (tmp_path / "test" / "1.0.0" / "contents.toml").touch()

    packages = await collect([spec], tmp_path)

    assert len(packages) == 1
    pkg = packages[0]
    assert pkg.name == "package"
    assert pkg.scoped is True
    assert pkg.reference_type == "doxygen"

    assert pkg.reference_dir == tmp_path / "test" / "1.0.0" / "xml"
    assert pkg.reference_dir.is_dir()
    assert (pkg.reference_dir / "content.xml").is_file()

    assert pkg.adoc_src_dir == tmp_path / "test" / "1.0.0" / "adoc"
    assert pkg.adoc_src_dir.is_dir()
    assert (pkg.adoc_src_dir / "content.adoc").is_file()

    assert pkg.adoc_image_dir == tmp_path / "test" / "1.0.0" / "images"
    assert pkg.adoc_image_dir.is_dir()
    assert (pkg.adoc_image_dir / "picture.png").is_file()

    assert pkg.adoc_root_doc == tmp_path / "test" / "1.0.0" / "adoc" / "content.adoc"
    assert pkg.adoc_root_doc.is_file()
async def test_http_package__old__xml_and_include(aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server,
        web.get("/test/1.0.0/include", include_file_response),
        web.get("/test/1.0.0/xml", xml_file_response),
    )

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.xml_subdir = "xml"
    spec.include_subdir = "adoc"
    spec.file_names = ["include", "xml"]

    packages = await collect([spec], tmp_path)

    assert len(packages) == 1
    pkg = packages[0]
    assert pkg.name == "test"
    assert pkg.scoped is False
    assert pkg.reference_dir is not None
    assert pkg.reference_type == "doxygen"
    assert pkg.adoc_src_dir is not None

    assert pkg.reference_dir.is_dir()
    assert (pkg.reference_dir / "content.xml").is_file()

    assert (tmp_path / "test" / "1.0.0" / "xml").is_dir()
    assert (tmp_path / "test" / "1.0.0" / "xml" / "content.xml").is_file()

    assert pkg.adoc_src_dir.is_dir()
    assert (pkg.adoc_src_dir / "content.adoc").is_file()

    assert (tmp_path / "test" / "1.0.0" / "adoc").is_dir()
    assert (tmp_path / "test" / "1.0.0" / "adoc" / "content.adoc").is_file()
async def test_http_package_error404(aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server, web.get("/test/1.0.0/error", error404_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["error"]

    with pytest.raises(DownloadError):
        await collect([spec], tmp_path)
async def test_http_package_not_a_tarfile(aiohttp_server, tmp_path):
    server = await start_server(aiohttp_server,
                                web.get("/test/1.0.0/text", text_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["text"]

    with pytest.raises(DownloadError):
        await collect([spec], tmp_path)
    assert not (tmp_path / "test" / "1.0.0").exists()
async def test_http_package__contents_toml__corrupt(aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server,
        web.get("/test/1.0.0/package", currupt_package_file_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["package"]

    with pytest.raises(InvalidPackageError):
        await collect([spec], tmp_path)
    assert not (tmp_path / "test" / "1.0.0").exists()
async def test_http_package_version_and_name_interpolation_in_file_names(
        aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server, web.get("/test/1.0.0/test-1.0.0", xml_file_response))

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.xml_subdir = "xml"
    spec.include_subdir = "adoc"
    spec.file_names = ["{name}-{version}"]

    await collect([spec], tmp_path)

    assert (tmp_path / "test" / "1.0.0" / "xml" / "content.xml").is_file()
async def test_http_package__old__subdirs_not_specified(
        aiohttp_server, tmp_path):
    server = await start_server(
        aiohttp_server,
        web.get("/test/1.0.0/include", include_file_response),
        web.get("/test/1.0.0/xml", xml_file_response),
    )

    spec = HttpPackageSpec(
        "test", "1.0.0",
        f"http://localhost:{server.port}/{{name}}/{{version}}/{{file_name}}")
    spec.file_names = ["include", "xml"]

    with pytest.raises(InvalidPackageError):
        await collect([spec], tmp_path)
    assert not (tmp_path / "test" / "1.0.0").exists()