File: noxfile.py

package info (click to toggle)
python-dogpile.cache 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 836 kB
  • sloc: python: 6,620; makefile: 159; sh: 104
file content (270 lines) | stat: -rw-r--r-- 7,884 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""Nox configuration for dogpile.cache."""

from __future__ import annotations

import glob
import itertools
import os
import sys

import nox

if True:
    sys.path.insert(0, ".")
    from tools.toxnox import tox_parameters
    from tools.toxnox import extract_opts


PYTHON_VERSIONS = [
    "3.10",
    "3.11",
    "3.12",
    "3.13",
    "3.13t",
    "3.14",
    "3.14t",
]
TARGETS = [
    "generic",
    "memory",
    "memcached",
    "redis",
    "redis_sentinel",
    "valkey",
    "valkey_sentinel",
    "dbm",
]
FULL = ["_quick", "full"]

pyproject = nox.project.load_toml("pyproject.toml")

nox.options.sessions = ["simple"]
nox.options.tags = ["py-generic-memory-dbm"]


def _pifpaf(
    cmd: list[str],
    module: str,
    *,
    port_env: str = "TOX_DOGPILE_PORT",
    port: str = "11234",
    additonal_args: str = "",
) -> None:
    """prepend a pifpaf execution to the given command arguments.

    This runs a server like memcached or redis up front before invoking
    the subsequent commands.

    """
    cmd[:0] = (
        f"pifpaf --env-prefix DOGPILE run {module} --port "
        f"{os.environ.get(port_env, port)} {additonal_args} --".split()
    )


@nox.session(name="simple")
@tox_parameters(
    ["python", "target", "full"],
    [PYTHON_VERSIONS, ["generic-memory-dbm"], FULL],
    always_include_in_tag=["target"],
)
def test_simple(session: nox.Session, target: str, full: str) -> None:
    """run a general suite including two built-in backends"""
    _tests(session, ["generic", "memory", "dbm"], full=full == "full")


@nox.session(name="backends")
@tox_parameters(
    ["python", "target", "full"],
    [PYTHON_VERSIONS, TARGETS, FULL],
)
def test_backends(session: nox.Session, target: str, full: str) -> None:
    """Run the main test suite, with optional single backend tests"""

    _tests(session, [target], full=full == "full")


@nox.session(name="coverage")
@tox_parameters(["target"], [TARGETS], base_tag="coverage")
def coverage(session: nox.Session, target: str) -> None:
    """Run tests with coverage."""

    _tests(session, [target], coverage=True)


def _tests(
    session: nox.Session,
    targets: list[str],
    coverage: bool = False,
    full: bool = False,
) -> None:

    if coverage:
        session.install("-e", ".")
    else:
        session.install(".")

    session.install(*nox.project.dependency_groups(pyproject, "tests"))

    if coverage:
        session.install(*nox.project.dependency_groups(pyproject, "coverage"))

    cmd = ["python", "-m", "pytest"]

    if coverage:
        cmd.extend(
            [
                "--cov=dogpile",
                "--cov-append",
                "--cov-report",
                "term",
                "--cov-report",
                "xml",
            ]
        )

    if not full:
        # disable time_intensive
        cmd.extend(["-m", "not time_intensive"])

    backend_cmd: list[str] = []
    pifpaf_cmd: list[str] = []

    ports = itertools.count(11212)

    for target in targets:
        # note the default target is "generic", which means run all the
        # normal tests but no backend tests

        match target:
            case "generic":
                backend_cmd.extend(
                    [
                        path
                        for path in glob.glob("tests/**/*.py", recursive=True)
                        if "backend" not in path
                    ]
                )
            case "memory":
                backend_cmd.append("tests/cache/test_memory_backend.py")
            case "memcached":
                session.install(
                    *nox.project.dependency_groups(
                        pyproject,
                        "tests_memcached",
                    )
                )

                if full:
                    # special install of pylibmc from source for full build
                    # this can't be in pyproject.toml since it's rejected
                    # by pypi
                    session.install(
                        "pylibmc @ "
                        "git+https://github.com/lericson/pylibmc.git@master"
                    )

                _pifpaf(
                    pifpaf_cmd,
                    "memcached",
                    port=str(next(ports)),
                )
                _pifpaf(
                    pifpaf_cmd,
                    "memcached",
                    port_env="TOX_DOGPILE_TLS_PORT",
                    port=str(next(ports)),
                    additonal_args=(
                        "--ssl_chain_cert=tests/tls/server_chain.pem "
                        "--ssl_key=tests/tls/server.key"
                    ),
                )

                backend_cmd.append("tests/cache/test_memcached_backend.py")
            case "redis":
                session.install(
                    *nox.project.dependency_groups(pyproject, "tests_redis")
                )
                _pifpaf(pifpaf_cmd, "redis", port=str(next(ports)))
                backend_cmd.append("tests/cache/test_redis_backend.py")
            case "valkey":
                session.install(
                    *nox.project.dependency_groups(pyproject, "tests_valkey")
                )
                _pifpaf(pifpaf_cmd, "valkey", port=str(next(ports)))
                backend_cmd.append("tests/cache/test_valkey_backend.py")
            case "redis_sentinel":
                session.install(
                    *nox.project.dependency_groups(pyproject, "tests_redis")
                )

                _pifpaf(
                    pifpaf_cmd,
                    "redis",
                    port=str(next(ports)),
                    additonal_args=f"--sentinel  --sentinel "
                    f"--sentinel-port "
                    f"""{os.environ.get(
                        'TOX_DOGPILE_SENTINEL_PORT', str(next(ports)))}""",
                )
                backend_cmd.append(
                    "tests/cache/test_redis_sentinel_backend.py"
                )
            case "valkey_sentinel":
                session.install(
                    *nox.project.dependency_groups(pyproject, "tests_valkey")
                )
                _pifpaf(
                    pifpaf_cmd,
                    "valkey",
                    port=str(next(ports)),
                    additonal_args=f"--sentinel  --sentinel "
                    f"--sentinel-port "
                    f"""{os.environ.get(
                        'TOX_DOGPILE_SENTINEL_PORT', str(next(ports)))}""",
                )
                backend_cmd.append(
                    "tests/cache/test_valkey_sentinel_backend.py"
                )
            case "dbm":
                backend_cmd.append("tests/cache/test_dbm_backend.py")

    posargs, opts = extract_opts(session.posargs, "generate-junit")

    if opts.generate_junit:
        # produce individual junit files that are per-database (or as
        # close as we can get).  jenkins junit plugin will merge all
        # the files...
        junit_suffix = "-".join(targets)
        junitfile = f"junit-{junit_suffix}.xml"
        cmd.extend(["--junitxml", junitfile])

    session.run(*pifpaf_cmd, *cmd, *backend_cmd, *posargs)


@nox.session(name="pep484")
def mypy_check(session: nox.Session) -> None:
    """Run mypy type checking."""

    session.install(*nox.project.dependency_groups(pyproject, "mypy"))

    session.install("-e", ".")

    session.run("mypy", "noxfile.py", "./dogpile/")


@nox.session(name="pep8")
def lint(session: nox.Session) -> None:
    """Run linting and formatting checks."""

    session.install(*nox.project.dependency_groups(pyproject, "lint"))

    file_paths = [
        "./dogpile/",
        "./tests/",
        "./tools/",
        "noxfile.py",
        "docs/build/conf.py",
    ]
    session.run("flake8", *file_paths)
    session.run("black", "--check", *file_paths)