From 706ccd26c0b7432afd045b1297310e57d129d4ab Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Wed, 2 Sep 2026 21:38:29 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=85=20Fix=20flaky=20app=5Fdir=20and?= =?UTF-8?q?=20launch=20tutorial=20tests=20under=20xdist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/conftest.py | 10 ++++++++++ .../test_app_dir/test_tutorial001.py | 20 +++---------------- .../test_launch/test_tutorial002.py | 19 ------------------ 3 files changed, 13 insertions(+), 36 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000000..702f0f753f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +from pathlib import Path + +import pytest +import typer + + +@pytest.fixture(name="app_dir") +def patch_app_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + monkeypatch.setattr(typer, "get_app_dir", lambda _app_name: str(tmp_path)) + return tmp_path diff --git a/tests/test_tutorial/test_app_dir/test_tutorial001.py b/tests/test_tutorial/test_app_dir/test_tutorial001.py index a0f5e001dc..ced17191b7 100644 --- a/tests/test_tutorial/test_app_dir/test_tutorial001.py +++ b/tests/test_tutorial/test_app_dir/test_tutorial001.py @@ -2,8 +2,6 @@ import sys from pathlib import Path -import pytest -import typer from typer.testing import CliRunner from docs_src.app_dir import tutorial001_py310 as mod @@ -11,26 +9,14 @@ runner = CliRunner() -@pytest.fixture(name="config_file") -def create_config_file(): - app_dir = Path(typer.get_app_dir("my-super-cli-app")) - app_dir.mkdir(parents=True, exist_ok=True) - config_path = app_dir / "config.json" - config_path.touch(exist_ok=True) - - yield config_path - - config_path.unlink() - app_dir.rmdir() - - -def test_cli_config_doesnt_exist(): +def test_cli_config_doesnt_exist(app_dir: Path): result = runner.invoke(mod.app) assert result.exit_code == 0 assert "Config file doesn't exist yet" in result.output -def test_cli_config_exists(config_file: Path): +def test_cli_config_exists(app_dir: Path): + (app_dir / "config.json").touch() result = runner.invoke(mod.app) assert result.exit_code == 0 assert "Config file doesn't exist yet" not in result.output diff --git a/tests/test_tutorial/test_launch/test_tutorial002.py b/tests/test_tutorial/test_launch/test_tutorial002.py index cc1b4e7598..3866fab11a 100644 --- a/tests/test_tutorial/test_launch/test_tutorial002.py +++ b/tests/test_tutorial/test_launch/test_tutorial002.py @@ -3,8 +3,6 @@ from pathlib import Path from unittest.mock import patch -import pytest -import typer from typer.testing import CliRunner from docs_src.launch import tutorial002_py310 as mod @@ -12,23 +10,6 @@ runner = CliRunner() -@pytest.fixture(name="app_dir") -def app_dir(): - app_dir = Path(typer.get_app_dir("my-super-cli-app")) - if app_dir.exists(): # pragma: no cover - for item in app_dir.iterdir(): - if item.is_file(): - item.unlink() - - yield app_dir - - if app_dir.exists(): - for item in app_dir.iterdir(): - if item.is_file(): - item.unlink() - app_dir.rmdir() - - def test_cli(app_dir: Path): with patch("typer.launch") as launch_mock: result = runner.invoke(mod.app) From 834aa11c42e879c66bcd50bb72181939d963f6aa Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Thu, 3 Sep 2026 10:52:15 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20`get=5Fapp?= =?UTF-8?q?=5Fdir`=20on=20Linux=20and=20macOS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_types_file.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_types_file.py b/tests/test_types_file.py index 3fb7d82f69..49dfcd6226 100644 --- a/tests/test_types_file.py +++ b/tests/test_types_file.py @@ -9,7 +9,7 @@ from typer._click.utils import PacifyFlushWrapper from typer.testing import CliRunner -from tests.utils import needs_linux, needs_windows +from tests.utils import needs_linux, needs_macos, needs_windows app = typer.Typer() @@ -302,6 +302,33 @@ def test_app_dir_force_posix(monkeypatch: pytest.MonkeyPatch) -> None: assert typer.get_app_dir("My App", force_posix=True) == "/home/tester/.my-app" +@needs_linux +def test_app_dir_linux_xdg_config_home(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("XDG_CONFIG_HOME", "/home/tester/config") + + assert typer.get_app_dir("My App") == "/home/tester/config/my-app" + + +@needs_linux +def test_app_dir_linux_default(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + monkeypatch.setattr("os.path.expanduser", lambda _path: "/home/tester/.config") + + assert typer.get_app_dir("My App") == "/home/tester/.config/my-app" + + +@needs_macos +def test_app_dir_macos(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr( + "os.path.expanduser", lambda _path: "/Users/tester/Library/Application Support" + ) + + assert ( + typer.get_app_dir("My App") + == "/Users/tester/Library/Application Support/My App" + ) + + def test_text_stream_binary_buffer(monkeypatch) -> None: class TextStdinWithBinaryBuffer: def __init__(self, data: bytes) -> None: