2021-06-22 09:01:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import http.server
|
2022-12-12 15:05:47 +00:00
|
|
|
import json
|
2021-06-22 09:01:29 +00:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import posixpath
|
2023-01-19 12:56:17 +00:00
|
|
|
import sys
|
2021-06-22 09:01:29 +00:00
|
|
|
import time
|
|
|
|
import webbrowser
|
|
|
|
from pathlib import Path
|
2022-01-28 18:26:03 +00:00
|
|
|
from urllib.parse import unquote
|
2021-06-22 09:01:29 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
|
2022-01-28 15:02:17 +00:00
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
2023-01-19 12:56:17 +00:00
|
|
|
UI_TESTS_PATH = ROOT / "tests" / "ui_tests"
|
|
|
|
TEST_RESULT_PATH = UI_TESTS_PATH / "reporting" / "reports" / "test"
|
2022-12-12 15:05:47 +00:00
|
|
|
FIXTURES_PATH = ROOT / "tests" / "ui_tests" / "fixtures.json"
|
2021-06-22 09:01:29 +00:00
|
|
|
|
2023-01-19 12:56:17 +00:00
|
|
|
sys.path.append(str(UI_TESTS_PATH))
|
|
|
|
from reporting import testreport # noqa: E402
|
|
|
|
|
2021-06-22 09:01:29 +00:00
|
|
|
|
|
|
|
class NoCacheRequestHandler(http.server.SimpleHTTPRequestHandler):
|
2022-01-28 18:26:03 +00:00
|
|
|
def end_headers(self) -> None:
|
2021-06-22 09:01:29 +00:00
|
|
|
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
self.send_header("Pragma", "no-cache")
|
|
|
|
self.send_header("Expires", "0")
|
|
|
|
return super().end_headers()
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def log_message(self, format, *args) -> None:
|
2021-06-22 09:01:29 +00:00
|
|
|
pass
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def translate_path(self, path: str) -> str:
|
2021-06-22 09:01:29 +00:00
|
|
|
# XXX
|
|
|
|
# Copy-pasted from Python 3.8 BaseHTTPRequestHandler so that we can inject
|
|
|
|
# the `directory` parameter.
|
|
|
|
# Otherwise, to keep compatible with 3.6, we'd need to mess with CWD. Which is
|
|
|
|
# unstable when we expect it to be erased and recreated under us.
|
|
|
|
path = path.split("?", 1)[0]
|
|
|
|
path = path.split("#", 1)[0]
|
|
|
|
# Don't forget explicit trailing slash when normalizing. Issue17324
|
|
|
|
trailing_slash = path.rstrip().endswith("/")
|
|
|
|
try:
|
2022-01-28 18:26:03 +00:00
|
|
|
path = unquote(path, errors="surrogatepass")
|
2021-06-22 09:01:29 +00:00
|
|
|
except UnicodeDecodeError:
|
2022-01-28 18:26:03 +00:00
|
|
|
path = unquote(path)
|
2021-06-22 09:01:29 +00:00
|
|
|
path = posixpath.normpath(path)
|
|
|
|
words = path.split("/")
|
|
|
|
words = filter(None, words)
|
|
|
|
path = str(TEST_RESULT_PATH) # XXX this is the only modified line
|
|
|
|
for word in words:
|
|
|
|
if os.path.dirname(word) or word in (os.curdir, os.pardir):
|
|
|
|
# Ignore components that are not a simple file/directory name
|
|
|
|
continue
|
|
|
|
path = os.path.join(path, word)
|
|
|
|
if trailing_slash:
|
|
|
|
path += "/"
|
|
|
|
return path
|
2023-01-19 12:56:17 +00:00
|
|
|
|
|
|
|
def do_GET(self) -> None:
|
|
|
|
if self.path in ("/", "/index.html"):
|
|
|
|
testreport.index()
|
|
|
|
|
|
|
|
return super().do_GET()
|
2021-06-22 09:01:29 +00:00
|
|
|
|
2022-12-12 15:05:47 +00:00
|
|
|
def do_POST(self) -> None:
|
|
|
|
if self.path == "/fixtures.json" and FIXTURES_PATH.exists():
|
|
|
|
|
|
|
|
length = int(self.headers.get("content-length"))
|
|
|
|
field_data = self.rfile.read(length)
|
|
|
|
data = json.loads(field_data)
|
|
|
|
|
|
|
|
test_name = data.get("test")
|
|
|
|
test_hash = data.get("hash")
|
|
|
|
|
|
|
|
if test_name is not None and test_hash is not None:
|
|
|
|
with open(FIXTURES_PATH, "r") as jsonFile:
|
|
|
|
fixtures = json.load(jsonFile)
|
|
|
|
fixtures[test_name] = test_hash
|
|
|
|
with open(FIXTURES_PATH, "w") as jsonFile:
|
|
|
|
json.dump(fixtures, jsonFile, indent=0)
|
|
|
|
jsonFile.write("\n")
|
|
|
|
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-Type", "text/plain")
|
|
|
|
self.end_headers()
|
|
|
|
|
2021-06-22 09:01:29 +00:00
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def launch_http_server(port: int) -> None:
|
|
|
|
http.server.test(HandlerClass=NoCacheRequestHandler, bind="localhost", port=port) # type: ignore [test is defined]
|
2021-06-22 09:01:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option("-p", "--port", type=int, default=8000)
|
2022-01-28 18:26:03 +00:00
|
|
|
def main(port: int):
|
2021-06-22 09:01:29 +00:00
|
|
|
httpd = multiprocessing.Process(target=launch_http_server, args=(port,))
|
|
|
|
httpd.start()
|
|
|
|
time.sleep(0.5)
|
|
|
|
webbrowser.open(f"http://localhost:{port}/")
|
|
|
|
httpd.join()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|