Skip to content

Commit

Permalink
track_metrics/upload_assets: simplify hook handling
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jul 25, 2024
1 parent 481b07a commit 38d5331
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
16 changes: 3 additions & 13 deletions qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}")

Expand Down
17 changes: 3 additions & 14 deletions qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")

Expand Down
12 changes: 9 additions & 3 deletions qa/unittests/tests/test_pytest_track_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 38d5331

Please sign in to comment.