-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathMakefile
More file actions
198 lines (170 loc) · 5.1 KB
/
Makefile
File metadata and controls
198 lines (170 loc) · 5.1 KB
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
# Shell / Make config
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.SILENT:
MAKEFLAGS += --no-print-directory
# -----------------------------
# User-configurable variables (edit this)
# INFRA_SERVICES: long-running infra (db, broker, cache, ...)
# INFRA_INIT_SERVICES: one-shot services that prepare INFRA_SERVICES
# MIGRATION_DB_SERVICE: transactional db service used by alembic (empty = no migrations)
# STAIRWAY_TEST: path to stairway test (empty = skip stairway step)
# -----------------------------
PROJECT_NAME ?= $(notdir $(abspath .))
INFRA_SERVICES ?= db_pg
INFRA_INIT_SERVICES ?=
MIGRATION_DB_SERVICE ?= db_pg
STAIRWAY_TEST ?= tests/integration/migrations/test_stairway.py
# -----------------------------
# Internal vars / aliases
# -----------------------------
DOCKER_COMPOSE := docker compose -p $(PROJECT_NAME)
PIP_AUDIT := scripts/makefile/pip_audit.sh
SLOTSCHECK := scripts/makefile/slotscheck.sh
DOCKER_ENV := scripts/makefile/docker_env.sh
LOCAL_ENV := scripts/makefile/local_env.sh
DOCKER_PRUNE := scripts/makefile/docker_prune.sh
PYCACHE_DEL := scripts/makefile/pycache_del.sh
MIGRATION := scripts/makefile/migration.sh
DISHKA_PLOT_DATA := scripts/dishka/plot_dependencies_data.py
# Test stack is isolated by project name
TEST_PROJECT ?= $(PROJECT_NAME)-test
DC_TEST_DOCKER := docker compose \
-p $(TEST_PROJECT) \
-f docker-compose.yml \
-f docker-compose.test.yml
TEST_RUNNER := $(TEST_PROJECT)-runner
# Pytest paths
PYTEST_PATHS_LIGHT := \
tests/sanity \
tests/unit \
tests/integration/no_infra
PYTEST_PATHS_APP_INFRA := \
$(PYTEST_PATHS_LIGHT) \
tests/smoke \
tests/integration/with_infra
PYTEST_PATHS_MIGRATIONS := \
tests/integration/migrations
# Pytest args
PYTEST_ARGS_VERBOSE := -s -vv
PYTEST_ARGS_COV := \
--cov=src \
--cov-report=term-missing \
--cov-report=html
PYTEST_ARGS_COV_DOCKER := \
--cov=src \
--cov-report=term-missing
# Safety
.PHONY: pip-audit
pip-audit:
$(PIP_AUDIT)
# Code quality
.PHONY: slotscheck lint test check check-ci
slotscheck:
$(SLOTSCHECK) src
lint:
ruff check --fix
ruff format
tombi format
tombi lint
deptry
$(MAKE) slotscheck
lint-imports
mypy
test:
pytest -v \
$(PYTEST_PATHS_LIGHT) \
$(PYTEST_ARGS_COV)
check: lint test
coverage html
check-ci:
ruff check
ruff format --check
tombi format --check
tombi lint
deptry
$(MAKE) slotscheck
lint-imports
mypy
pytest -v \
$(PYTEST_PATHS_LIGHT) \
$(PYTEST_ARGS_COV)
coverage html
# Docker compose
.PHONY: docker-env local-env upd up upd-local up-local down stop-all
docker-env:
$(DOCKER_ENV)
local-env:
$(LOCAL_ENV)
upd: docker-env
$(DOCKER_COMPOSE) up -d --build --force-recreate
up: docker-env
$(DOCKER_COMPOSE) up --build --force-recreate
upd-local: local-env
$(DOCKER_COMPOSE) up -d --build --force-recreate $(INFRA_SERVICES) $(INFRA_INIT_SERVICES)
up-local: local-env
$(DOCKER_COMPOSE) up --build --force-recreate $(INFRA_SERVICES) $(INFRA_INIT_SERVICES)
down:
$(DOCKER_COMPOSE) down
stop-all:
docker ps -q | xargs -r docker stop
# Migrations
.PHONY: migration
migration: local-env
MIGRATION_DB_SERVICE=$(MIGRATION_DB_SERVICE) \
INFRA_INIT_SERVICES="$(INFRA_INIT_SERVICES)" \
STAIRWAY_TEST=$(STAIRWAY_TEST) \
$(MIGRATION) "$(msg)"
# Tests (with infra)
.PHONY: test-docker test-docker-app test-docker-migrations
test-docker-app: docker-env
rc=0; \
$(DC_TEST_DOCKER) down -v --remove-orphans >/dev/null 2>&1 || true; \
if [ -n "$(strip $(INFRA_SERVICES))" ]; then \
$(DC_TEST_DOCKER) up -d --build --wait --wait-timeout 180 $(INFRA_SERVICES); \
if [ -n "$(strip $(INFRA_INIT_SERVICES))" ]; then \
$(DC_TEST_DOCKER) up --build $(INFRA_INIT_SERVICES) >/dev/null; \
fi; \
else \
echo "INFRA_SERVICES is empty, skipping infra startup"; \
fi; \
$(DC_TEST_DOCKER) run --build --name $(TEST_RUNNER) app \
pytest $(PYTEST_ARGS_VERBOSE) \
$(PYTEST_PATHS_APP_INFRA) \
$(PYTEST_ARGS_COV_DOCKER) \
|| rc=$$?; \
docker cp $(TEST_RUNNER):/tmp/.coverage ./.coverage.docker 2>/dev/null || true; \
docker rm $(TEST_RUNNER) >/dev/null 2>&1 || true; \
$(DC_TEST_DOCKER) down -v --remove-orphans; \
exit $$rc
test-docker-migrations: docker-env
if [ -z "$(strip $(PYTEST_PATHS_MIGRATIONS))" ] || [ -z "$(strip $(MIGRATION_DB_SERVICE))" ]; then \
echo "PYTEST_PATHS_MIGRATIONS or MIGRATION_DB_SERVICE is empty, skipping migrations tests"; \
exit 0; \
fi; \
rc=0; \
$(DC_TEST_DOCKER) down -v --remove-orphans >/dev/null 2>&1 || true; \
$(DC_TEST_DOCKER) up -d --build --wait --wait-timeout 180 $(MIGRATION_DB_SERVICE); \
$(DC_TEST_DOCKER) run --build --no-deps --name $(TEST_RUNNER) app \
pytest $(PYTEST_ARGS_VERBOSE) \
$(PYTEST_PATHS_MIGRATIONS) \
|| rc=$$?; \
docker rm $(TEST_RUNNER) >/dev/null 2>&1 || true; \
$(DC_TEST_DOCKER) down -v --remove-orphans; \
exit $$rc
test-docker:
$(MAKE) test-docker-app
$(MAKE) test-docker-migrations
coverage html --data-file=.coverage.docker -d htmlcov-docker && \
echo "Coverage HTML report: htmlcov-docker/index.html" || true
.PHONY: prune
prune:
$(DOCKER_PRUNE)
# Project structure visualization
.PHONY: pycache-del tree plot-data
pycache-del:
$(PYCACHE_DEL)
tree: pycache-del
tree
plot-data:
APP_LOGGING_LEVEL=CRITICAL python $(DISHKA_PLOT_DATA)