From 550e2a79512b75f09e57878baaa52bb5310aefbe Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Mon, 12 Dec 2022 16:05:47 +0100 Subject: [PATCH] feat(tests): add button to update fixtures directly from ui test report [no changelog] --- tests/show_results.py | 24 +++++++++++++++++ tests/ui_tests/reporting/testreport.css | 11 +++++++- tests/ui_tests/reporting/testreport.js | 35 ++++++++++++++++++------- tests/ui_tests/reporting/testreport.py | 1 + 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/tests/show_results.py b/tests/show_results.py index ed53777c6..3255593c6 100755 --- a/tests/show_results.py +++ b/tests/show_results.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import http.server +import json import multiprocessing import os import posixpath @@ -13,6 +14,7 @@ import click ROOT = Path(__file__).resolve().parent.parent TEST_RESULT_PATH = ROOT / "tests" / "ui_tests" / "reporting" / "reports" / "test" +FIXTURES_PATH = ROOT / "tests" / "ui_tests" / "fixtures.json" class NoCacheRequestHandler(http.server.SimpleHTTPRequestHandler): @@ -52,6 +54,28 @@ class NoCacheRequestHandler(http.server.SimpleHTTPRequestHandler): path += "/" return path + 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() + def launch_http_server(port: int) -> None: http.server.test(HandlerClass=NoCacheRequestHandler, bind="localhost", port=port) # type: ignore [test is defined] diff --git a/tests/ui_tests/reporting/testreport.css b/tests/ui_tests/reporting/testreport.css index f526d65ab..6397b3431 100644 --- a/tests/ui_tests/reporting/testreport.css +++ b/tests/ui_tests/reporting/testreport.css @@ -14,7 +14,7 @@ tr.bad a, tr.bad a:visited { position: fixed; top: 50px; right: 5px; - width: 300px; + width: 500px; padding: 1em; } @@ -39,6 +39,15 @@ tr.bad a, tr.bad a:visited { border-color: lightgreen; } +#markbox #mark-update { + color: green; + border-color: darkgreen; +} + +#markbox #mark-update:hover { + border-color: lightgreen; +} + #markbox #mark-bad { color: darkred; border-color: darkred; diff --git a/tests/ui_tests/reporting/testreport.js b/tests/ui_tests/reporting/testreport.js index 8329146d6..cb5f8cfc9 100644 --- a/tests/ui_tests/reporting/testreport.js +++ b/tests/ui_tests/reporting/testreport.js @@ -25,7 +25,25 @@ function itemKeyFromIndexEntry(entry) { function markState(state) { - window.localStorage.setItem(itemKeyFromOneTest(), state) + if (state === 'update') { + let lastIndex = decodeURIComponent(window.location.href).split("/").reverse()[0].lastIndexOf(".") + let stem = decodeURIComponent(window.location.href).split("/").reverse()[0].slice(0, lastIndex) + fetch('http://localhost:8000/fixtures.json', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + "test": stem, + "hash": document.body.dataset.actualHash + }) + }).then(() => {}) + window.localStorage.setItem(itemKeyFromOneTest(), 'ok') + } else { + window.localStorage.setItem(itemKeyFromOneTest(), state) + } + if (window.nextHref) { window.location.assign(window.nextHref) } else { @@ -36,9 +54,9 @@ function markState(state) { function resetState(whichState) { function shouldReset(value) { - if (value == whichState) return true - if (whichState != "all") return false - return (value == "bad" || value == "ok") + if (value === whichState) return true + if (whichState !== "all") return false + return (value === "bad" || value === "ok") } let keysToReset = [] @@ -64,13 +82,13 @@ function findNextForHref(doc, href) { let a = tr.querySelector("a") if (!a) continue if (foundIt) return a.href - else if (a.href == href) foundIt = true + else if (a.href === href) foundIt = true } } function openLink(ev) { - if (ev.button == 2) { + if (ev.button === 2) { // let right click through return true; } @@ -78,8 +96,7 @@ function openLink(ev) { // capture other clicks ev.preventDefault() let href = ev.target.href - let newWindow = window.open(href) - newWindow + window.open(href) } @@ -124,7 +141,7 @@ function onLoadTestCase() { function onLoad() { - if (window.location.protocol == "file") return + if (window.location.protocol === "file") return for (let elem of document.getElementsByClassName("script-hidden")) { elem.classList.remove("script-hidden") diff --git a/tests/ui_tests/reporting/testreport.py b/tests/ui_tests/reporting/testreport.py index bf359d292..4c496a011 100644 --- a/tests/ui_tests/reporting/testreport.py +++ b/tests/ui_tests/reporting/testreport.py @@ -271,6 +271,7 @@ def failed( p("Click a button to mark the test result as:") with div(id="buttons"): t.button("OK", id="mark-ok", onclick="markState('ok')") + t.button("OK & UPDATE", id="mark-update", onclick="markState('update')") t.button("BAD", id="mark-bad", onclick="markState('bad')") if download_failed: