Skip to content

Commit

Permalink
Remove pytest-server-fixtures
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JPEWdev committed Aug 2, 2024
1 parent 777b354 commit 7741dab
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dev = [
"pyshacl >= 0.25.0",
"pytest >= 7.4",
"pytest-cov >= 4.1",
"pytest-server-fixtures >= 1.7",
]

[project.urls]
Expand Down
Empty file added tests/__init__.py
Empty file.
9 changes: 4 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import shutil
import subprocess
import time
from .http import HTTPTestServer

from pytest_server_fixtures.http import SimpleHTTPTestServer
from pathlib import Path

THIS_FILE = Path(__file__)
Expand All @@ -21,19 +21,18 @@

@pytest.fixture
def http_server():
with SimpleHTTPTestServer() as s:
with HTTPTestServer() as s:
s.start()
yield s


@pytest.fixture(scope="session")
def model_server():
with SimpleHTTPTestServer() as s:
root = Path(s.document_root)
with HTTPTestServer() as s:
for p in MODEL_DIR.iterdir():
if not p.is_file():
continue
shutil.copyfile(p, root / p.name)
shutil.copyfile(p, s.document_root / p.name)
s.start()
yield s.uri

Expand Down
65 changes: 65 additions & 0 deletions tests/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright (c) 2024 Joshua Watt
#
# SPDX-License-Identifier: MIT

import socket
import subprocess
import sys
import tempfile

from pathlib import Path
from contextlib import closing


def get_ephemeral_port(host):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, 0))
return s.getsockname()[1]


class HTTPTestServer(object):
def __init__(self):
self.p = None
self.temp_dir = None

def start(self):
assert self.p is None, "Server already started"

self.host = "localhost"
self.port = get_ephemeral_port(self.host)
self.p = subprocess.Popen(
[sys.executable, "-m", "http.server", "--bind", self.host, str(self.port)],
cwd=self.document_root,
)
self.uri = f"http://{self.host}:{self.port}"

# Wait for server to start
while True:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
try:
s.connect((self.host, self.port))
return
except ConnectionRefusedError:
continue

def stop(self):
if self.p is None:
return

self.p.terminate()
self.p.wait()

def __enter__(self):
self.temp_dir = tempfile.TemporaryDirectory()
return self

def __exit__(self, typ, value, tb):
self.stop()
self.temp_dir.cleanup()
self.temp_dir = None

@property
def document_root(self):
return Path(self.temp_dir.name)
9 changes: 2 additions & 7 deletions tests/test_model_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#
# SPDX-License-Identifier: MIT

import os
import shutil
import subprocess
import pytest
Expand Down Expand Up @@ -289,12 +288,8 @@ def test_context_url(model_server):


def test_context_args(http_server):
shutil.copyfile(
TEST_CONTEXT, os.path.join(http_server.document_root, "context.json")
)
shutil.copyfile(
TEST_CONTEXT, os.path.join(http_server.document_root, "context2.json")
)
shutil.copyfile(TEST_CONTEXT, http_server.document_root / "context.json")
shutil.copyfile(TEST_CONTEXT, http_server.document_root / "context2.json")

def do_test(*, contexts=[], url_contexts=[]):
cmd = [
Expand Down

0 comments on commit 7741dab

Please sign in to comment.