-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters