From 9d4042787965c0c4666f5e4a0b9ecb888be4f0b8 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 21 Sep 2023 10:18:31 +0200 Subject: [PATCH] Add test for UVICORN_APP --- scripts/check | 2 +- scripts/lint | 2 +- tests/test_cli.py | 58 ++++++++++++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/scripts/check b/scripts/check index 2cbe36662..e8567468b 100755 --- a/scripts/check +++ b/scripts/check @@ -10,7 +10,7 @@ export SOURCE_FILES="uvicorn tests" set -x ./scripts/sync-version -${PREFIX}black --check --diff --target-version=py37 $SOURCE_FILES +${PREFIX}black --check --diff --target-version=py38 $SOURCE_FILES ${PREFIX}mypy $SOURCE_FILES ${PREFIX}ruff check $SOURCE_FILES ${PREFIX}python -m tools.cli_usage --check diff --git a/scripts/lint b/scripts/lint index d162b5896..9f0cacd2c 100755 --- a/scripts/lint +++ b/scripts/lint @@ -9,6 +9,6 @@ export SOURCE_FILES="uvicorn tests" set -x -${PREFIX}black --target-version=py37 $SOURCE_FILES +${PREFIX}black --target-version=py38 $SOURCE_FILES ${PREFIX}ruff --fix $SOURCE_FILES ${PREFIX}python -m tools.cli_usage diff --git a/tests/test_cli.py b/tests/test_cli.py index ae22fef6a..b5d18cacc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,9 +1,11 @@ +import contextlib import importlib import os import platform import sys from pathlib import Path from textwrap import dedent +from typing import Iterator from unittest import mock import pytest @@ -19,6 +21,15 @@ main = importlib.import_module("uvicorn.main") +@contextlib.contextmanager +def load_env_var(key: str, value: str) -> Iterator[None]: + old_environ = dict(os.environ) + os.environ[key] = value + yield + os.environ.clear() + os.environ.update(old_environ) + + class App: pass @@ -146,29 +157,23 @@ def test_cli_event_size() -> None: assert mock_run.call_args[1]["h11_max_incomplete_event_size"] == 32768 -@pytest.fixture() -def load_env_h11_protocol(): - old_environ = dict(os.environ) - os.environ["UVICORN_HTTP"] = "h11" - yield - os.environ.clear() - os.environ.update(old_environ) - - -def test_env_variables(load_env_h11_protocol: None): - runner = CliRunner(env=os.environ) - with mock.patch.object(main, "run") as mock_run: - runner.invoke(cli, ["tests.test_cli:App"]) - _, kwargs = mock_run.call_args - assert kwargs["http"] == "h11" +@pytest.mark.parametrize("http_protocol", ["h11", "httptools"]) +def test_env_variables(http_protocol: str): + with load_env_var("UVICORN_HTTP", http_protocol): + runner = CliRunner(env=os.environ) + with mock.patch.object(main, "run") as mock_run: + runner.invoke(cli, ["tests.test_cli:App"]) + _, kwargs = mock_run.call_args + assert kwargs["http"] == http_protocol -def test_mistmatch_env_variables(load_env_h11_protocol: None): - runner = CliRunner(env=os.environ) - with mock.patch.object(main, "run") as mock_run: - runner.invoke(cli, ["tests.test_cli:App", "--http=httptools"]) - _, kwargs = mock_run.call_args - assert kwargs["http"] == "httptools" +def test_ignore_environment_variable_when_set_on_cli(): + with load_env_var("UVICORN_HTTP", "h11"): + runner = CliRunner(env=os.environ) + with mock.patch.object(main, "run") as mock_run: + runner.invoke(cli, ["tests.test_cli:App", "--http=httptools"]) + _, kwargs = mock_run.call_args + assert kwargs["http"] == "httptools" def test_app_dir(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None: @@ -191,3 +196,14 @@ async def app(scope, receive, send): assert result.exit_code == 3 mock_run.assert_called_once() assert sys.path[0] == str(app_dir) + + +def test_set_app_via_environment_variable(): + app_path = "tests.test_cli:App" + with load_env_var("UVICORN_APP", app_path): + runner = CliRunner(env=os.environ) + with mock.patch.object(main, "run") as mock_run: + result = runner.invoke(cli) + args, _ = mock_run.call_args + assert result.exit_code == 0 + assert args == (app_path,)