File: test_contributors.py

package info (click to toggle)
python-sphinx-contributors 0.2.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: python: 239; makefile: 20
file content (179 lines) | stat: -rw-r--r-- 5,168 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
Tests for collapse extension.
(c) 2022 - present David Garcia (@dgarcia360)
# This code is licensed under MIT license (see LICENSE.md for details)
"""

import subprocess
import sys
from pathlib import Path
from textwrap import dedent

from sphinx_contributors import Contributor, ContributorsRepository

_CLASS_NAME = "sphinx_contributors"


def test_contributor_build() -> None:
    """
    Initializing a contributor.
    """
    contributor = Contributor("dgarcia360", "http://#", contributions=10).build(
        _CLASS_NAME
    )
    assert contributor.astext() == "dgarcia360\n\n10 contributions"


def test_contributor_build_with_no_contribution() -> None:
    """
    Create a contributor with no contributions.
    """
    contributor = Contributor("dgarcia360", "http://#").build(_CLASS_NAME)
    assert contributor.astext() == "dgarcia360"


def test_contributor_build_with_one_contribution() -> None:
    """
    Create a contributor with one contribution.
    """
    contributor = Contributor("dgarcia360", "http://#", contributions=1).build(
        _CLASS_NAME
    )
    assert contributor.astext() == "dgarcia360\n\n1 contribution"


def test_contributor_repository_build() -> None:
    """
    Initializing a repository build.
    """
    contributors = [
        Contributor("dgarcia360", "http://#", contributions=2),
        Contributor("user", "http://#", contributions=1),
    ]
    contributor_repository = ContributorsRepository(contributors, reverse=True).build(
        _CLASS_NAME
    )
    assert (
        contributor_repository.astext()
        == "dgarcia360\n\n2 contributions\n\nuser\n\n1 contribution"
    )


def test_contributor_repository_build_empty() -> None:
    """
    Initializing a contributor repository with no contributors.
    """
    contributors = []
    contributor_repository = ContributorsRepository(contributors, reverse=True).build(
        _CLASS_NAME
    )
    assert contributor_repository.astext() == ""


def test_contributor_repository_build_order_desc() -> None:
    """
    The sort option (descending).
    """
    contributors = [
        Contributor("user", "http://#", contributions=1),
        Contributor("dgarcia360", "http://#", contributions=2),
    ]
    contributor_repository = ContributorsRepository(contributors, reverse=True).build(
        _CLASS_NAME
    )
    assert (
        contributor_repository.astext()
        == "dgarcia360\n\n2 contributions\n\nuser\n\n1 contribution"
    )


def test_contributor_repository_build_order_asc() -> None:
    """
    The sort option works (ascending).
    """
    contributors = [
        Contributor("dgarcia360", "http://#", contributions=2),
        Contributor("user", "http://#", 1),
    ]
    contributor_repository = ContributorsRepository(contributors, reverse=False).build(
        _CLASS_NAME
    )
    assert (
        contributor_repository.astext()
        == "user\n\n1 contribution\n\ndgarcia360\n\n2 contributions"
    )


def test_contributor_repository_build_with_limit() -> None:
    """
    The limit option works.
    """
    contributors = [
        Contributor("dgarcia360", "http://#", contributions=2),
        Contributor("user", "http://#", contributions=1),
    ]
    contributor_repository = ContributorsRepository(
        contributors, reverse=True, limit=1
    ).build(_CLASS_NAME)
    assert contributor_repository.astext() == "dgarcia360\n\n2 contributions"


def test_contributor_repository_build_exclude() -> None:
    """
    The exclude option works.
    """
    contributors = [
        Contributor("dgarcia360", "http://#", contributions=2),
        Contributor("sphinx", "http://#", contributions=1),
        Contributor("user", "http://#", contributions=1),
    ]
    exclude = "sphinx,user"
    contributor_repository = ContributorsRepository(
        contributors, reverse=True, limit=10, exclude=exclude
    ).build(_CLASS_NAME)
    assert contributor_repository.astext() == "dgarcia360\n\n2 contributions"


def test_contributor_directive(tmp_path: Path) -> None:
    """
    The ``contributors`` directive runs with no errors.
    """
    source_directory = tmp_path / "source"
    source_directory.mkdir()
    source_file = source_directory / "index.rst"
    conf_py = source_directory / "conf.py"
    conf_py.touch()
    source_file.touch()
    conf_py_content = dedent(
        """\
        extensions = ['sphinx_contributors']
        """,
    )
    conf_py.write_text(conf_py_content)
    source_file_content = dedent(
        """\
        .. contributors:: sphinx-doc/sphinx
        """,
    )
    source_file.write_text(source_file_content)
    destination_directory = tmp_path / "destination"
    args = [
        sys.executable,
        "-m",
        "sphinx",
        "-b",
        "html",
        "-W",
        # Directory containing source and configuration files.
        str(source_directory),
        # Directory containing build files.
        str(destination_directory),
        # Source file to process.
        str(source_file),
    ]
    result = subprocess.run(
        args=args,
        check=False,
        stderr=subprocess.PIPE,
    )
    assert result.returncode == 0