Add a test running entire command

This commit is contained in:
Rohan McGovern 2022-08-02 16:40:39 +10:00
parent c4fd70e240
commit 52ec5f195b
7 changed files with 87 additions and 2 deletions

View file

@ -1,6 +1,5 @@
import argparse import argparse
import asyncio import asyncio
import gzip
import logging import logging
import os import os
@ -19,7 +18,7 @@ async def dump_autoindices(args: argparse.Namespace) -> None:
LOG.info("Wrote %s", output) LOG.info("Wrote %s", output)
def entrypoint(): def entrypoint() -> None:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("url") parser.add_argument("url")
parser.add_argument("--index-filename", default="index.html") parser.add_argument("--index-filename", default="index.html")

Binary file not shown.

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<repomd xmlns="http://linux.duke.edu/metadata/repo" xmlns:rpm="http://linux.duke.edu/metadata/rpm">
<revision>1659419679</revision>
<data type="primary">
<checksum type="sha256">3a7a286e13883d497b2e3c7029ceb7c372ff2529bbfa22d0c890285ce6aa3129</checksum>
<open-checksum type="sha256">ad4149ec99b72282ab4891ea5d224db02cc3d7e0ad5c1bdaba56c21cbd4ab132</open-checksum>
<location href="repodata/3a7a286e13883d497b2e3c7029ceb7c372ff2529bbfa22d0c890285ce6aa3129-primary.xml.gz"/>
<timestamp>1659419679</timestamp>
<size>598</size>
<open-size>1127</open-size>
</data>
<data type="filelists">
<checksum type="sha256">33795fed0c0144a7fe732a9ded8d7529940e4a1384ad654e7214266648c37f0b</checksum>
<open-checksum type="sha256">ae8aa2cca2e1eba056ed56a66da2b1f6cdb142e465a13bb55f603c7481239e39</open-checksum>
<location href="repodata/33795fed0c0144a7fe732a9ded8d7529940e4a1384ad654e7214266648c37f0b-filelists.xml.gz"/>
<timestamp>1659419679</timestamp>
<size>243</size>
<open-size>320</open-size>
</data>
<data type="other">
<checksum type="sha256">834b12e38d809c4b5afd1a7c03ad48c0e15e3d28420987132d7d2c176127b9db</checksum>
<open-checksum type="sha256">ee1c6e87c3b7ebfa2e85d9b56e245ef097a0d928794da75ab63d43ac5593d9d0</open-checksum>
<location href="repodata/834b12e38d809c4b5afd1a7c03ad48c0e15e3d28420987132d7d2c176127b9db-other.xml.gz"/>
<timestamp>1659419679</timestamp>
<size>229</size>
<open-size>285</open-size>
</data>
</repomd>

58
tests/test_cmd.py Normal file
View file

@ -0,0 +1,58 @@
import pathlib
import asyncio
import pytest
from aiohttp import web, test_utils
from repo_autoindex._impl.cmd import entrypoint
THIS_DIR = pathlib.Path(__file__).parent
async def test_command(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
"""Run the repo-autoindex command against a sample repo and check the generated index."""
monkeypatch.chdir(tmp_path)
entrypoint_coro = []
def fake_run(coro):
assert asyncio.iscoroutine(coro)
entrypoint_coro.append(coro)
monkeypatch.setattr("asyncio.run", fake_run)
app = web.Application()
app.add_routes([web.static("/", THIS_DIR)])
async with test_utils.TestServer(app) as server:
repo_url = server.make_url("/sample_repo")
monkeypatch.setattr("sys.argv", ["repo-autoindex", str(repo_url)])
entrypoint()
assert entrypoint_coro
await entrypoint_coro[0]
# It should have written index files reproducing the structure
index_toplevel = tmp_path.joinpath("index.html")
index_pkgs = tmp_path.joinpath("pkgs", "index.html")
index_w = tmp_path.joinpath("pkgs", "w", "index.html")
assert index_toplevel.exists()
assert index_pkgs.exists()
assert index_w.exists()
# Simple sanity check of some expected content
toplevel = index_toplevel.read_text()
pkgs = index_pkgs.read_text()
w = index_w.read_text()
assert '<a href="repodata/index.html">repodata/</a>' in toplevel
assert '<a href="pkgs/index.html">pkgs/</a>' in toplevel
assert '<a href="w/index.html">w/</a>' in pkgs
assert '<a href="walrus-5.21-1.noarch.rpm">walrus-5.21-1.noarch.rpm</a>' in w