Skip to content

Commit

Permalink
Issue #5/#7 simplify pytest_addoption handling a bit
Browse files Browse the repository at this point in the history
Let defaults do their thing
  • Loading branch information
soxofaan committed Jul 25, 2024
1 parent 25052aa commit aa36c17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
18 changes: 7 additions & 11 deletions qa/tools/apex_algorithm_qa_tools/pytest_track_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,33 @@ def test_dummy(track_metric):

import pytest

_TRACK_METRICS_PATH = "track_metrics_path"
_TRACK_METRICS_NAME = "track_metrics"
_TRACK_METRICS_PLUGIN_NAME = "track_metrics"


def pytest_addoption(parser):
def pytest_addoption(parser: pytest.Parser):
parser.addoption(
"--track-metrics-report",
metavar="PATH",
action="store",
dest=_TRACK_METRICS_PATH,
default=None,
help="Path to JSON file to store test/benchmark metrics.",
)


def pytest_configure(config):
track_metrics_path = config.getoption(_TRACK_METRICS_PATH)
track_metrics_path = config.getoption("track_metrics_report")
if (
track_metrics_path
# Don't register on xdist worker nodes
and not hasattr(config, "workerinput")
):
config.pluginmanager.register(
TrackMetricsReporter(path=track_metrics_path),
name=_TRACK_METRICS_NAME,
name=_TRACK_METRICS_PLUGIN_NAME,
)


def pytest_unconfigure(config):
if config.pluginmanager.hasplugin(_TRACK_METRICS_NAME):
config.pluginmanager.unregister(name=_TRACK_METRICS_NAME)
if config.pluginmanager.hasplugin(_TRACK_METRICS_PLUGIN_NAME):
config.pluginmanager.unregister(name=_TRACK_METRICS_PLUGIN_NAME)


class TrackMetricsReporter:
Expand Down Expand Up @@ -122,7 +118,7 @@ def track_metric(
"""

reporter: Union[TrackMetricsReporter, None] = pytestconfig.pluginmanager.get_plugin(
_TRACK_METRICS_NAME
_TRACK_METRICS_PLUGIN_NAME
)

if reporter:
Expand Down
13 changes: 5 additions & 8 deletions qa/tools/apex_algorithm_qa_tools/pytest_upload_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,24 @@

_log = logging.getLogger(__name__)

_PLUGIN_NAME = "upload_assets"
_UPLOAD_ASSETS_PLUGIN_NAME = "upload_assets"


def pytest_addoption(parser):
def pytest_addoption(parser: pytest.Parser):
# TODO: option to always upload (also on success).
parser.addoption(
"--upload-assets-run-id",
metavar="ID",
action="store",
metavar="RUNID",
help="The run ID to use for building the S3 key.",
)
parser.addoption(
"--upload-assets-endpoint-url",
metavar="URL",
action="store",
help="The S3 endpoint URL to upload to.",
)
parser.addoption(
"--upload-assets-bucket",
metavar="BUCKET",
action="store",
help="The S3 bucket to upload to.",
)

Expand All @@ -58,7 +55,7 @@ def pytest_configure(config: pytest.Config):
)
config.pluginmanager.register(
S3UploadPlugin(run_id=run_id, s3_client=s3_client, bucket=bucket),
name=_PLUGIN_NAME,
name=_UPLOAD_ASSETS_PLUGIN_NAME,
)


Expand Down Expand Up @@ -120,7 +117,7 @@ def upload_assets(pytestconfig, tmp_path) -> Callable[[Path], None]:
Fixture to register a file (under `tmp_path`) for S3 upload
after the test failed.
"""
uploader = pytestconfig.pluginmanager.get_plugin(_PLUGIN_NAME)
uploader = pytestconfig.pluginmanager.get_plugin(_UPLOAD_ASSETS_PLUGIN_NAME)

def collect(*paths: Path):
for path in paths:
Expand Down

0 comments on commit aa36c17

Please sign in to comment.