|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import os |
3 | 4 | from pathlib import Path
|
| 5 | +from typing import cast |
4 | 6 |
|
5 | 7 | import pytest
|
6 | 8 | from _pytest.fixtures import SubRequest
|
|
9 | 11 |
|
10 | 12 | @pytest.fixture()
|
11 | 13 | def build_outcome(app: SphinxTestApp, request: SubRequest) -> str:
|
| 14 | + prepare_marker = request.node.get_closest_marker("prepare") |
| 15 | + if prepare_marker: |
| 16 | + directive_args: list[str] | None = prepare_marker.kwargs.get("directive_args") |
| 17 | + if directive_args: |
| 18 | + index = Path(cast(str, app.confdir)) / "index.rst" |
| 19 | + if not any(i for i in directive_args if i.startswith(":module:")): |
| 20 | + directive_args.append(":module: parser") |
| 21 | + if not any(i for i in directive_args if i.startswith(":func:")): |
| 22 | + directive_args.append(":func: make") |
| 23 | + args = [f" {i}" for i in directive_args] |
| 24 | + index.write_text(os.linesep.join([".. sphinx_argparse_cli::"] + args)) |
| 25 | + |
| 26 | + ext_mapping = {"html": "html", "text": "txt"} |
| 27 | + sphinx_marker = request.node.get_closest_marker("sphinx") |
| 28 | + assert sphinx_marker is not None |
| 29 | + ext = ext_mapping[sphinx_marker.kwargs.get("buildername")] |
| 30 | + |
12 | 31 | app.build()
|
13 |
| - ext = {"html": "html", "text": "txt"} |
14 |
| - ext = ext.get(request.node.get_closest_marker("sphinx").args[0]) |
15 |
| - assert ext is not None |
16 | 32 | return (Path(app.outdir) / f"index.{ext}").read_text()
|
17 | 33 |
|
18 | 34 |
|
19 |
| -@pytest.mark.sphinx("html", testroot="basic") |
| 35 | +@pytest.mark.sphinx(buildername="html", testroot="basic") |
20 | 36 | def test_basic_as_html(build_outcome: str) -> None:
|
21 | 37 | assert build_outcome
|
22 | 38 |
|
23 | 39 |
|
24 |
| -@pytest.mark.sphinx("text", testroot="complex") |
| 40 | +@pytest.mark.sphinx(buildername="text", testroot="complex") |
25 | 41 | def test_complex_as_text(build_outcome: str) -> None:
|
26 | 42 | expected = (Path(__file__).parent / "complex.txt").read_text()
|
27 | 43 | assert build_outcome == expected
|
28 | 44 |
|
29 | 45 |
|
30 |
| -@pytest.mark.sphinx("html", testroot="complex") |
| 46 | +@pytest.mark.sphinx(buildername="html", testroot="complex") |
31 | 47 | def test_complex_as_html(build_outcome: str) -> None:
|
32 | 48 | assert build_outcome
|
33 | 49 |
|
34 | 50 |
|
35 |
| -@pytest.mark.sphinx("text", testroot="prog") |
| 51 | +@pytest.mark.sphinx(buildername="text", testroot="prog") |
36 | 52 | def test_prog_as_text(build_outcome: str) -> None:
|
37 | 53 | assert build_outcome == "magic - CLI interface\n*********************\n\n magic\n"
|
38 | 54 |
|
39 | 55 |
|
40 |
| -@pytest.mark.sphinx("text", testroot="title-set") |
| 56 | +@pytest.mark.sphinx(buildername="text", testroot="title-set") |
41 | 57 | def test_set_title_as_text(build_outcome: str) -> None:
|
42 | 58 | assert build_outcome == "My own title\n************\n\n foo\n"
|
43 | 59 |
|
44 | 60 |
|
45 |
| -@pytest.mark.sphinx("text", testroot="title-empty") |
| 61 | +@pytest.mark.sphinx(buildername="text", testroot="title-empty") |
46 | 62 | def test_empty_title_as_text(build_outcome: str) -> None:
|
47 | 63 | assert build_outcome == " foo\n"
|
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.sphinx(buildername="text", testroot="complex") |
| 67 | +@pytest.mark.prepare(directive_args=[":usage_width: 100"]) |
| 68 | +def test_usage_width_default(build_outcome: str) -> None: |
| 69 | + assert "complex second [-h] [--flag] [--root] one pos_two\n" in build_outcome |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.sphinx(buildername="text", testroot="complex") |
| 73 | +@pytest.mark.prepare(directive_args=[":usage_width: 50"]) |
| 74 | +def test_usage_width_custom(build_outcome: str) -> None: |
| 75 | + assert "complex second [-h] [--flag] [--root]\n" in build_outcome |
0 commit comments