diff --git a/src/hypercorn/__main__.py b/src/hypercorn/__main__.py index b3980941..b36cf69a 100644 --- a/src/hypercorn/__main__.py +++ b/src/hypercorn/__main__.py @@ -182,7 +182,7 @@ def main(sys_args: list[str] | None = None) -> int: parser.add_argument( "--statsd-host", help="The host:port of the statsd server", default=sentinel ) - parser.add_argument("--statsd-prefix", help="Prefix for all statsd messages", default="") + parser.add_argument("--statsd-prefix", help="Prefix for all statsd messages", default=sentinel) parser.add_argument( "-m", "--umask", diff --git a/tests/test___main__.py b/tests/test___main__.py index f24b3462..e114b2eb 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -83,3 +83,28 @@ def test_verify_mode_conversion(monkeypatch: MonkeyPatch) -> None: hypercorn.__main__.main(["--verify-mode", "CERT_REQUIRED", "asgi:App"]) run_multiple.assert_called() + + +def test_statsd_prefix_preserves_config_file(tmp_path, monkeypatch: MonkeyPatch) -> None: + """CLI default must not wipe statsd_prefix loaded from a config file (#382).""" + run_multiple = Mock() + monkeypatch.setattr(hypercorn.__main__, "run", run_multiple) + cfg = tmp_path / "statsd_config.py" + cfg.write_text('statsd_prefix = "myapp."\n') + hypercorn.__main__.main(["--config", f"file:{cfg}", "asgi:App"]) + run_multiple.assert_called() + config = run_multiple.call_args_list[0][0][0] + assert config.statsd_prefix == "myapp." + + +def test_statsd_prefix_cli_overrides_config(tmp_path, monkeypatch: MonkeyPatch) -> None: + run_multiple = Mock() + monkeypatch.setattr(hypercorn.__main__, "run", run_multiple) + cfg = tmp_path / "statsd_config.py" + cfg.write_text('statsd_prefix = "myapp."\n') + hypercorn.__main__.main( + ["--config", f"file:{cfg}", "--statsd-prefix", "cli.", "asgi:App"] + ) + run_multiple.assert_called() + config = run_multiple.call_args_list[0][0][0] + assert config.statsd_prefix == "cli."