Skip to content

Commit ae7ba4e

Browse files
committed
Remove pytest-server-fixtures
The upstream for this module appears dead, and it is broken with the latest version of python due to [1], so remove it and implement the functionality locally [1]: man-group/pytest-plugins#224
1 parent 777b354 commit ae7ba4e

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ dev = [
3131
"pyshacl >= 0.25.0",
3232
"pytest >= 7.4",
3333
"pytest-cov >= 4.1",
34-
"pytest-server-fixtures >= 1.7",
3534
]
3635

3736
[project.urls]

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import shutil
1010
import subprocess
1111
import time
12+
from .http import HTTPTestServer
1213

13-
from pytest_server_fixtures.http import SimpleHTTPTestServer
1414
from pathlib import Path
1515

1616
THIS_FILE = Path(__file__)
@@ -21,19 +21,18 @@
2121

2222
@pytest.fixture
2323
def http_server():
24-
with SimpleHTTPTestServer() as s:
24+
with HTTPTestServer() as s:
2525
s.start()
2626
yield s
2727

2828

2929
@pytest.fixture(scope="session")
3030
def model_server():
31-
with SimpleHTTPTestServer() as s:
32-
root = Path(s.document_root)
31+
with HTTPTestServer() as s:
3332
for p in MODEL_DIR.iterdir():
3433
if not p.is_file():
3534
continue
36-
shutil.copyfile(p, root / p.name)
35+
shutil.copyfile(p, s.document_root / p.name)
3736
s.start()
3837
yield s.uri
3938

tests/http.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# Copyright (c) 2024 Joshua Watt
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
import socket
7+
import subprocess
8+
import sys
9+
import tempfile
10+
import time
11+
12+
from pathlib import Path
13+
from contextlib import closing
14+
15+
16+
def get_ephemeral_port(host):
17+
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
18+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
s.bind((host, 0))
20+
return s.getsockname()[1]
21+
22+
23+
class HTTPTestServer(object):
24+
def __init__(self):
25+
self.p = None
26+
self.temp_dir = None
27+
28+
def start(self):
29+
assert self.p is None, "Server already started"
30+
31+
self.host = "127.0.0.1"
32+
self.port = get_ephemeral_port(self.host)
33+
self.p = subprocess.Popen(
34+
[sys.executable, "-m", "http.server", "--bind", self.host, str(self.port)],
35+
cwd=self.document_root,
36+
)
37+
self.uri = f"http://{self.host}:{self.port}"
38+
39+
# Wait for server to start
40+
start_time = time.monotonic()
41+
while time.monotonic() < start_time + 30:
42+
assert self.p.poll() is None
43+
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
44+
try:
45+
s.connect((self.host, self.port))
46+
return
47+
48+
except ConnectionRefusedError:
49+
continue
50+
51+
# Timeout
52+
self.p.terminate()
53+
self.p.wait()
54+
assert False, "Timeout waiting for server to be ready"
55+
56+
def stop(self):
57+
if self.p is None:
58+
return
59+
60+
self.p.terminate()
61+
self.p.wait()
62+
63+
def __enter__(self):
64+
self.temp_dir = tempfile.TemporaryDirectory()
65+
return self
66+
67+
def __exit__(self, typ, value, tb):
68+
self.stop()
69+
self.temp_dir.cleanup()
70+
self.temp_dir = None
71+
72+
@property
73+
def document_root(self):
74+
return Path(self.temp_dir.name)

tests/test_model_source.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# SPDX-License-Identifier: MIT
55

6-
import os
76
import shutil
87
import subprocess
98
import pytest
@@ -289,12 +288,8 @@ def test_context_url(model_server):
289288

290289

291290
def test_context_args(http_server):
292-
shutil.copyfile(
293-
TEST_CONTEXT, os.path.join(http_server.document_root, "context.json")
294-
)
295-
shutil.copyfile(
296-
TEST_CONTEXT, os.path.join(http_server.document_root, "context2.json")
297-
)
291+
shutil.copyfile(TEST_CONTEXT, http_server.document_root / "context.json")
292+
shutil.copyfile(TEST_CONTEXT, http_server.document_root / "context2.json")
298293

299294
def do_test(*, contexts=[], url_contexts=[]):
300295
cmd = [

0 commit comments

Comments
 (0)