Ignore parallel coverage files (#9293)

* ignore parallel coverage files

* Properly shutdown & close HTTP server
This commit is contained in:
Brad Warren
2022-05-07 13:31:59 +10:00
committed by GitHub
parent 2017669544
commit 7dd1e814fb
2 changed files with 15 additions and 27 deletions
+1
View File
@@ -13,6 +13,7 @@ poetry.lock
# coverage # coverage
.coverage .coverage
.coverage.*
/htmlcov/ /htmlcov/
/.vagrant /.vagrant
@@ -4,8 +4,8 @@ or outside during setup/teardown of the integration tests environment.
""" """
import contextlib import contextlib
import errno import errno
import functools
import http.server as SimpleHTTPServer import http.server as SimpleHTTPServer
import multiprocessing
import os import os
import re import re
import shutil import shutil
@@ -13,6 +13,7 @@ import socketserver
import stat import stat
import sys import sys
import tempfile import tempfile
import threading
import time import time
import warnings import warnings
from typing import Generator from typing import Generator
@@ -80,10 +81,6 @@ class GracefulTCPServer(socketserver.TCPServer):
allow_reuse_address = True allow_reuse_address = True
def _run_server(port: int) -> None:
GracefulTCPServer(('', port), SimpleHTTPServer.SimpleHTTPRequestHandler).serve_forever()
@contextlib.contextmanager @contextlib.contextmanager
def create_http_server(port: int) -> Generator[str, None, None]: def create_http_server(port: int) -> Generator[str, None, None]:
""" """
@@ -93,30 +90,20 @@ def create_http_server(port: int) -> Generator[str, None, None]:
:param int port: the TCP port to use :param int port: the TCP port to use
:return str: the temporary webroot attached to this server :return str: the temporary webroot attached to this server
""" """
current_cwd = os.getcwd() with tempfile.TemporaryDirectory() as webroot:
webroot = tempfile.mkdtemp() # Setting the directory argument of SimpleHTTPRequestHandler causes
# files to be served from that directory.
process = multiprocessing.Process(target=_run_server, args=(port,)) handler = functools.partial(SimpleHTTPServer.SimpleHTTPRequestHandler, directory=webroot)
server = GracefulTCPServer(('', port), handler)
thread = threading.Thread(target=server.serve_forever)
thread.start()
try: try:
# SimpleHTTPServer is designed to serve files from the current working directory at the
# time it starts. So we temporarily change the cwd to our crafted webroot before launch.
try:
os.chdir(webroot)
process.start()
finally:
os.chdir(current_cwd)
check_until_timeout('http://localhost:{0}/'.format(port)) check_until_timeout('http://localhost:{0}/'.format(port))
yield webroot yield webroot
finally: finally:
try: server.shutdown()
if process.is_alive(): thread.join()
process.terminate() server.server_close()
process.join() # Block until process is effectively terminated
finally:
shutil.rmtree(webroot)
def list_renewal_hooks_dirs(config_dir: str) -> List[str]: def list_renewal_hooks_dirs(config_dir: str) -> List[str]: