meltano
/
noxfile.py
175 строк · 4.5 Кб
1"""Nox configuration.
2
3- Run `nox -l` to list the sessions Nox can run.
4- Run `nox -t lint` to run linting.
5- Run `nox -t test` to run tests.
6- Run `nox` to run all sessions.
7- Run `nox -s <session name>` to run a particular session.
8"""
9
10from __future__ import annotations
11
12import os
13import sys
14import typing as t
15from pathlib import Path
16from random import randint
17
18if t.TYPE_CHECKING:
19from nox import Session
20
21try:
22from nox_poetry import session as nox_session
23except ImportError:
24raise SystemExit(
25"Nox failed to import the 'nox-poetry' package. Please install it " # noqa: EM102
26f"using the following command: {sys.executable} -m pip install nox-poetry",
27) from None
28
29# NOTE: The module docstring above is printed when `nox -l` is run.
30
31# Dependencies for tests and type checking are defined in `pyproject.toml`, and
32# locked in `poetry.lock`. The various Nox sessions defined here install the
33# subset of them they require.
34
35# We use `nox-poetry` to ensure the version installed is consistent with
36# `poetry.lock`. The single source of truth for our Python test and type checking
37# dependencies is `pyproject.toml`. Other linting and checks are performed by
38# `pre-commit`, where each check specifies its own dependencies. There should be
39# no duplicated dependencies between `pyproject.toml` and
40# `.pre-commit-config.yaml`.
41
42root_path = Path(__file__).parent
43python_versions = ("3.8", "3.9", "3.10", "3.11", "3.12")
44main_python_version = "3.10"
45pytest_deps = (
46"backoff",
47"colorama", # colored output in Windows
48"freezegun",
49"hypothesis",
50"mock",
51"moto",
52"pytest",
53"pytest-asyncio",
54"pytest-cov",
55"pytest-docker",
56"pytest-httpserver",
57"pytest-order",
58"pytest-randomly",
59"pytest-rerunfailures",
60"pytest-xdist",
61"requests-mock",
62"pytest-structlog",
63)
64
65
66def _run_pytest(session: Session) -> None:
67random_seed = randint(0, 2**32 - 1) # noqa: S311, WPS432
68args = session.posargs or ("tests/",)
69try:
70session.env.update(
71{
72"COVERAGE_RCFILE": str(root_path / "pyproject.toml"),
73"COVERAGE_FILE": str(
74root_path / f".coverage.{random_seed:010}.{session.name}",
75),
76"NOX_CURRENT_SESSION": "tests",
77},
78)
79session.run(
80"pytest",
81"--cov=meltano",
82"--cov=tests",
83f"--randomly-seed={random_seed}",
84*args,
85)
86finally:
87if session.interactive:
88session.notify("coverage", posargs=[])
89
90
91@nox_session(
92name="pytest",
93python=python_versions,
94tags=("test", "pytest"),
95)
96def pytest_meltano(session: Session) -> None:
97"""Run pytest to test Meltano.
98
99Args:
100session: Nox session.
101"""
102backend_db = os.environ.get("PYTEST_BACKEND", "sqlite")
103extras = ["azure", "gcs", "s3", "uv"]
104
105if backend_db == "mssql":
106extras.append("mssql")
107elif backend_db == "postgresql":
108extras.append("psycopg2")
109elif backend_db == "postgresql_psycopg3":
110extras.append("postgres")
111
112session.install(
113f".[{','.join(extras)}]",
114*pytest_deps,
115)
116_run_pytest(session)
117
118
119@nox_session(python=main_python_version)
120def coverage(session: Session) -> None:
121"""Combine and report previously generated coverage data.
122
123Args:
124session: Nox session.
125"""
126args = session.posargs or ("report",)
127
128session.install("coverage[toml]")
129
130if not session.posargs and any(Path().glob(".coverage.*")):
131session.run("coverage", "combine")
132
133session.run("coverage", *args)
134
135
136@nox_session(
137name="pre-commit",
138python=main_python_version,
139tags=("lint",),
140)
141def pre_commit(session: Session) -> None:
142"""Run pre-commit linting and auto-fixes.
143
144Args:
145session: Nox session.
146"""
147args = session.posargs or ("run", "--all-files")
148session.install("pre-commit")
149session.run("pre-commit", *args)
150
151
152@nox_session(
153python=main_python_version,
154tags=("lint",),
155)
156def mypy(session: Session) -> None:
157"""Run mypy type checking.
158
159Args:
160session: Nox session.
161"""
162session.install(
163".[mssql,azure,gcs,s3,uv]",
164"boto3-stubs",
165"mypy",
166"types-croniter",
167"types-python-dateutil",
168"types-jsonschema",
169"types-psutil",
170"types-python-slugify",
171"types-PyYAML",
172"types-requests",
173"types-tabulate",
174)
175session.run("mypy", *session.posargs)
176