disnake

Форк
0
/
noxfile.py 
250 строк · 7.3 Кб
1
# SPDX-License-Identifier: MIT
2

3
from __future__ import annotations
4

5
import os
6
import pathlib
7
from itertools import chain
8
from typing import TYPE_CHECKING, Callable, Dict, List, Tuple, TypeVar
9

10
import nox
11

12
if TYPE_CHECKING:
13
    from typing_extensions import Concatenate, ParamSpec
14

15
    P = ParamSpec("P")
16
    T = TypeVar("T")
17

18
    NoxSessionFunc = Callable[Concatenate[nox.Session, P], T]
19

20

21
# see https://pdm.fming.dev/latest/usage/advanced/#use-nox-as-the-runner
22
os.environ.update(
23
    {
24
        "PDM_IGNORE_SAVED_PYTHON": "1",
25
    },
26
)
27

28

29
nox.options.error_on_external_run = True
30
nox.options.reuse_existing_virtualenvs = True
31
nox.options.sessions = [
32
    "lint",
33
    "check-manifest",
34
    "slotscheck",
35
    "pyright",
36
    "test",
37
]
38
nox.needs_version = ">=2022.1.7"
39

40

41
# used to reset cached coverage data once for the first test run only
42
reset_coverage = True
43

44

45
@nox.session
46
def docs(session: nox.Session) -> None:
47
    """Build and generate the documentation.
48

49
    If running locally, will build automatic reloading docs.
50
    If running in CI, will build a production version of the documentation.
51
    """
52
    session.run_always("pdm", "install", "--prod", "-G", "docs", external=True)
53
    with session.chdir("docs"):
54
        args = ["-b", "html", "-n", ".", "_build/html", *session.posargs]
55
        if session.interactive:
56
            session.run(
57
                "sphinx-autobuild",
58
                "--ignore",
59
                "_build",
60
                "--watch",
61
                "../disnake",
62
                "--watch",
63
                "../changelog",
64
                "--port",
65
                "8009",
66
                "-j",
67
                "auto",
68
                *args,
69
            )
70
        else:
71
            session.run(
72
                "sphinx-build",
73
                "-aE",
74
                *args,
75
            )
76

77

78
@nox.session
79
def lint(session: nox.Session) -> None:
80
    """Check all files for linting errors"""
81
    session.run_always("pdm", "install", "-G", "tools", external=True)
82

83
    session.run("pre-commit", "run", "--all-files", *session.posargs)
84

85

86
@nox.session(name="check-manifest")
87
def check_manifest(session: nox.Session) -> None:
88
    """Run check-manifest."""
89
    # --no-self is provided here because check-manifest builds disnake. There's no reason to build twice, so we don't.
90
    session.run_always("pdm", "install", "--no-self", "-dG", "tools", external=True)
91
    session.run("check-manifest", "-v")
92

93

94
@nox.session()
95
def slotscheck(session: nox.Session) -> None:
96
    """Run slotscheck."""
97
    session.run_always("pdm", "install", "-dG", "tools", external=True)
98
    session.run("python", "-m", "slotscheck", "--verbose", "-m", "disnake")
99

100

101
@nox.session
102
def autotyping(session: nox.Session) -> None:
103
    """Run autotyping.
104

105
    Because of the nature of changes that autotyping makes, and the goal design of examples,
106
    this runs on each folder in the repository with specific settings.
107
    """
108
    session.run_always("pdm", "install", "-dG", "codemod", external=True)
109

110
    base_command = ["python", "-m", "libcst.tool", "codemod", "autotyping.AutotypeCommand"]
111
    if not session.interactive:
112
        base_command += ["--hide-progress"]
113

114
    dir_options: Dict[Tuple[str, ...], Tuple[str, ...]] = {
115
        (
116
            "disnake",
117
            "scripts",
118
            "tests",
119
            "test_bot",
120
            "noxfile.py",
121
        ): ("--aggressive",),
122
        ("examples",): (
123
            "--scalar-return",
124
            "--bool-param",
125
            "--bool-param",
126
            "--int-param",
127
            "--float-param",
128
            "--str-param",
129
            "--bytes-param",
130
        ),
131
    }
132

133
    if session.posargs:
134
        # short circuit with the provided arguments
135
        # if there's just one file argument, give it the defaults that we normally use
136
        posargs = session.posargs.copy()
137
        if len(posargs) == 1 and not (path := posargs[0]).startswith("--"):
138
            path = pathlib.Path(path).absolute()
139
            try:
140
                path = path.relative_to(pathlib.Path.cwd())
141
            except ValueError:
142
                pass
143
            else:
144
                module = path.parts[0]
145
                for modules, options in dir_options.items():
146
                    if module in modules:
147
                        posargs += options
148
                        break
149

150
        session.run(
151
            *base_command,
152
            *posargs,
153
        )
154
        return
155

156
    # run the custom fixers
157
    for module, options in dir_options.items():
158
        session.run(
159
            *base_command,
160
            *module,
161
            *options,
162
        )
163

164

165
@nox.session(name="codemod")
166
def codemod(session: nox.Session) -> None:
167
    """Run libcst codemods."""
168
    session.run_always("pdm", "install", "-dG", "codemod", external=True)
169

170
    base_command = ["python", "-m", "libcst.tool"]
171
    base_command_codemod = base_command + ["codemod"]
172
    if not session.interactive:
173
        base_command_codemod += ["--hide-progress"]
174

175
    if (session.posargs and session.posargs[0] == "run-all") or not session.interactive:
176
        # run all of the transformers on disnake
177
        session.log("Running all transformers.")
178

179
        session.run(*base_command_codemod, "combined.CombinedCodemod", "disnake")
180
    elif session.posargs:
181
        if len(session.posargs) < 2:
182
            session.posargs.append("disnake")
183

184
        session.run(*base_command_codemod, *session.posargs)
185
    else:
186
        session.run(*base_command, "list")
187
        return  # don't run autotyping in this case
188

189
    session.notify("autotyping", posargs=[])
190

191

192
@nox.session()
193
def pyright(session: nox.Session) -> None:
194
    """Run pyright."""
195
    session.run_always("pdm", "install", "-d", "-Gspeed", "-Gdocs", "-Gvoice", external=True)
196
    env = {
197
        "PYRIGHT_PYTHON_IGNORE_WARNINGS": "1",
198
    }
199
    try:
200
        session.run("python", "-m", "pyright", *session.posargs, env=env)
201
    except KeyboardInterrupt:
202
        pass
203

204

205
@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"])
206
@nox.parametrize(
207
    "extras",
208
    [
209
        [],
210
        # NOTE: disabled while there are no tests that would require these dependencies
211
        # ["speed"],
212
        # ["voice"],
213
    ],
214
)
215
def test(session: nox.Session, extras: List[str]) -> None:
216
    """Run tests."""
217
    # shell splitting is not done by nox
218
    extras = list(chain(*(["-G", extra] for extra in extras)))
219

220
    session.run_always("pdm", "install", "-dG", "test", "-dG", "typing", *extras, external=True)
221

222
    pytest_args = ["--cov", "--cov-context=test"]
223
    global reset_coverage  # noqa: PLW0603
224
    if reset_coverage:
225
        # don't use `--cov-append` for first run
226
        reset_coverage = False
227
    else:
228
        # use `--cov-append` in all subsequent runs
229
        pytest_args.append("--cov-append")
230

231
    # TODO: only run tests that depend on the different dependencies
232
    session.run(
233
        "pytest",
234
        *pytest_args,
235
        *session.posargs,
236
    )
237

238

239
@nox.session()
240
def coverage(session: nox.Session) -> None:
241
    """Display coverage information from the tests."""
242
    session.run_always("pdm", "install", "-dG", "test", external=True)
243
    if "html" in session.posargs or "serve" in session.posargs:
244
        session.run("coverage", "html", "--show-contexts")
245
    if "serve" in session.posargs:
246
        session.run(
247
            "python", "-m", "http.server", "8012", "--directory", "htmlcov", "--bind", "127.0.0.1"
248
        )
249
    if "erase" in session.posargs:
250
        session.run("coverage", "erase")
251

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.