From 38d533118051435cf4425a91482dd80fa6101404 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Thu, 25 Jul 2024 17:18:14 +0200 Subject: [PATCH] track_metrics/upload_assets: simplify hook handling --- .../pytest_track_metrics.py | 16 +++------------- .../pytest_upload_assets.py | 17 +++-------------- qa/unittests/tests/test_pytest_track_metrics.py | 12 +++++++++--- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py b/qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py index 8216134..f7ba99f 100644 --- a/qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py +++ b/qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py @@ -55,19 +55,6 @@ def pytest_configure(config): ) -def pytest_report_header(config): - plugin: TrackMetricsReporter | None = config.pluginmanager.get_plugin( - _TRACK_METRICS_PLUGIN_NAME - ) - if plugin: - return f"Plugin `track_metrics` is active, reporting to {plugin.path}" - - -def pytest_unconfigure(config): - if config.pluginmanager.hasplugin(_TRACK_METRICS_PLUGIN_NAME): - config.pluginmanager.unregister(name=_TRACK_METRICS_PLUGIN_NAME) - - class TrackMetricsReporter: def __init__( self, path: Union[str, Path], user_properties_key: str = "track_metrics" @@ -95,6 +82,9 @@ def pytest_sessionfinish(self, session): with self.path.open("w", encoding="utf8") as f: json.dump(self.metrics, f, indent=2) + def pytest_report_header(self): + return f"Plugin `track_metrics` is active, reporting to {self.path}" + def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", f"Generated track_metrics report: {self.path}") diff --git a/qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py b/qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py index d49fa0e..4b2d34e 100644 --- a/qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py +++ b/qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py @@ -79,20 +79,6 @@ def pytest_configure(config: pytest.Config): ) -def pytest_report_header(config): - # TODO Move inside S3UploadPlugin - plugin: S3UploadPlugin | None = config.pluginmanager.get_plugin( - _UPLOAD_ASSETS_PLUGIN_NAME - ) - if plugin: - return f"Plugin `upload_assets` is active, with upload to {plugin.bucket!r}" - - -def pytest_unconfigure(config): - if config.pluginmanager.hasplugin(_UPLOAD_ASSETS_PLUGIN_NAME): - config.pluginmanager.unregister(name=_UPLOAD_ASSETS_PLUGIN_NAME) - - class S3UploadPlugin: def __init__(self, *, run_id: str | None = None, s3_client, bucket: str) -> None: self.run_id = run_id or uuid.uuid4().hex @@ -139,6 +125,9 @@ def _upload(self, nodeid: str) -> Dict[str, str]: assets[name] = url self.upload_stats["uploaded"] += 1 + def pytest_report_header(self): + return f"Plugin `upload_assets` is active, with upload to {self.bucket!r}" + def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", f"`upload_assets` stats: {self.upload_stats}") diff --git a/qa/unittests/tests/test_pytest_track_metrics.py b/qa/unittests/tests/test_pytest_track_metrics.py index 4775857..c1af9a3 100644 --- a/qa/unittests/tests/test_pytest_track_metrics.py +++ b/qa/unittests/tests/test_pytest_track_metrics.py @@ -25,11 +25,17 @@ def test_3plus(track_metric, x): ) metrics_path = tmp_path / "metrics.json" - result = pytester.runpytest(f"--track-metrics-report={metrics_path}") - result.assert_outcomes(passed=1, failed=1) + run_result = pytester.runpytest( + f"--track-metrics-report={metrics_path}", + ) + run_result.stdout.re_match_lines( + [r"Plugin `track_metrics` is active, reporting to"] + ) + + run_result.assert_outcomes(passed=1, failed=1) assert metrics_path.exists() - result.stdout.re_match_lines([f".*Generated.*{re.escape(str(metrics_path))}.*"]) + run_result.stdout.re_match_lines([f".*Generated.*{re.escape(str(metrics_path))}.*"]) with metrics_path.open("r", encoding="utf8") as f: metrics = json.load(f)