|
3 | 3 |
|
4 | 4 | import json |
5 | 5 | import os |
| 6 | +import socket |
6 | 7 | import sys |
7 | 8 | from pathlib import Path |
8 | | -from threading import Thread |
| 9 | +from types import SimpleNamespace as NS |
9 | 10 |
|
10 | 11 | import django |
11 | 12 | import django.core.handlers.exception |
|
15 | 16 | import pytest |
16 | 17 | from django.template.loader import render_to_string |
17 | 18 | from django.test.client import MULTIPART_CONTENT |
| 19 | +from xprocess import ProcessStarter |
18 | 20 |
|
19 | 21 | import appmap |
20 | 22 | import appmap.django # noqa: F401 |
21 | 23 | from _appmap.metadata import Metadata |
22 | 24 |
|
23 | 25 | from ..test.helpers import DictIncluding |
24 | | - |
25 | | -# Make sure assertions in web_framework get rewritten (e.g. to show |
26 | | -# diffs in generated appmaps) |
27 | | -pytest.register_assert_rewrite("_appmap.test.web_framework") |
28 | | - |
29 | | -# pylint: disable=unused-import,wrong-import-position |
30 | | -from .web_framework import TestRemoteRecording # pyright:ignore |
31 | | -from .web_framework import TestRequestCapture # pyright: ignore |
32 | | -from .web_framework import _TestRecordRequests, exec_cmd, wait_until_port_is |
33 | | - |
34 | | -# pylint: enable=unused-import |
| 26 | +from .web_framework import ( |
| 27 | + _TestFormCapture, |
| 28 | + _TestFormData, |
| 29 | + _TestRecordRequests, |
| 30 | + _TestRemoteRecording, |
| 31 | + _TestRequestCapture, |
| 32 | +) |
35 | 33 |
|
36 | 34 | sys.path += [str(Path(__file__).parent / "data" / "django")] |
37 | 35 |
|
38 | 36 | # Import app just for the side-effects. It must happen after sys.path has been modified. |
39 | 37 | import djangoapp # pyright: ignore pylint: disable=import-error, unused-import,wrong-import-order,wrong-import-position |
40 | 38 |
|
41 | 39 |
|
| 40 | +class TestFormCapture(_TestFormCapture): |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +class TestFormTest(_TestFormData): |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +class TestRecordRequests(_TestRecordRequests): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +class TestRemoteRecording(_TestRemoteRecording): |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +class TestRequestCapture(_TestRequestCapture): |
| 57 | + pass |
| 58 | + |
| 59 | + |
42 | 60 | @pytest.mark.django_db |
43 | 61 | @pytest.mark.appmap_enabled(appmap_enabled=False) |
44 | 62 | def test_sql_capture(events): |
@@ -200,55 +218,21 @@ def test_disabled(self, pytester, monkeypatch): |
200 | 218 | assert not (pytester.path / "tmp").exists() |
201 | 219 |
|
202 | 220 |
|
203 | | -class TestRecordRequestsDjango(_TestRecordRequests): |
204 | | - def server_start_thread(self, debug=True): |
205 | | - # Use appmap from our working copy, not the module installed by virtualenv. Add the init |
206 | | - # directory so the sitecustomize.py file it contains will be loaded on startup. This |
207 | | - # simulates a real installation. |
208 | | - settings = "settings_dev" if debug else "settings" |
209 | | - exec_cmd( |
210 | | - """ |
211 | | -export PYTHONPATH="$PWD" |
212 | | -
|
213 | | -cd _appmap/test/data/django/ |
214 | | -PYTHONPATH="$PYTHONPATH:$PWD/init" |
215 | | -""" |
216 | | - + f" APPMAP_OUTPUT_DIR=/tmp DJANGO_SETTINGS_MODULE=djangoapp.{settings}" |
217 | | - + " python manage.py runserver" |
218 | | - + f" 127.0.0.1:{_TestRecordRequests.server_port}" |
219 | | - ) |
220 | | - |
221 | | - def server_start(self, debug=True): |
222 | | - def start_with_debug(): |
223 | | - self.server_start_thread(debug) |
224 | | - |
225 | | - # start as background thread so running the tests can continue |
226 | | - thread = Thread(target=start_with_debug) |
227 | | - thread.start() |
228 | | - wait_until_port_is("127.0.0.1", _TestRecordRequests.server_port, "open") |
229 | | - |
230 | | - def server_stop(self): |
231 | | - exec_cmd( |
232 | | - "ps -ef" |
233 | | - + "| grep -i 'manage.py runserver'" |
234 | | - + "| grep -v grep" |
235 | | - + "| awk '{ print $2 }'" |
236 | | - + "| xargs kill -9" |
237 | | - ) |
238 | | - wait_until_port_is("127.0.0.1", _TestRecordRequests.server_port, "closed") |
239 | | - |
240 | | - def test_record_request_appmap_enabled_requests_enabled_no_remote(self): |
241 | | - self.server_stop() # ensure it's not running |
242 | | - self.server_start() |
243 | | - self.record_request(False) |
244 | | - self.server_stop() |
245 | | - |
246 | | - def test_record_request_appmap_enabled_requests_enabled_and_remote(self): |
247 | | - self.server_stop() # ensure it's not running |
248 | | - self.server_start() |
249 | | - self.record_request(True) |
250 | | - self.server_stop() |
251 | | - |
252 | | - # it's not possible to test for |
253 | | - # appmap_not_enabled_requests_enabled_and_remote because when |
254 | | - # APPMAP=false the routes for remote recording are disabled. |
| 221 | +@pytest.fixture(name="server") |
| 222 | +def django_server(xprocess, server_base): |
| 223 | + debug = server_base.debug |
| 224 | + host = server_base.host |
| 225 | + port = server_base.port |
| 226 | + settings = "settings_dev" if debug else "settings" |
| 227 | + |
| 228 | + name = "django" |
| 229 | + pattern = f"server at http://{host}:{port}" |
| 230 | + cmd = f"manage.py runserver {host}:{port}" |
| 231 | + env = {"DJANGO_SETTINGS_MODULE": f"djangoapp.{settings}"} |
| 232 | + |
| 233 | + xprocess.ensure(name, server_base.factory(name, cmd, pattern, env)) |
| 234 | + |
| 235 | + url = f"http://{server_base.host}:{port}" |
| 236 | + yield NS(debug=debug, url=url) |
| 237 | + |
| 238 | + xprocess.getinfo(name).terminate() |
0 commit comments