Skip to content
Myr

✅ Fix flaky `app_dir` and `launch` tutorial tests under `xdist` (#1951)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yurii Motov · committed by GitHub · parent 82b8395 · Browse files · 4 files+41 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+41 added−37 removed

  • tests:+41 added−37 removed

4 files+41 added−37 removed

modified:tests/test_tutorial/test_app_dir/test_tutorial001.py+3 added-17 removed
1import subprocess2import sys3from pathlib import Path45import pytest6import typer5from typer.testing import CliRunner67from docs_src.app_dir import tutorial001_py310 as mod89runner = CliRunner()101114@pytest.fixture(name="config_file")15def create_config_file():16 app_dir = Path(typer.get_app_dir("my-super-cli-app"))17 app_dir.mkdir(parents=True, exist_ok=True)18 config_path = app_dir / "config.json"19 config_path.touch(exist_ok=True)2021 yield config_path2223 config_path.unlink()24 app_dir.rmdir()252627def test_cli_config_doesnt_exist():12def test_cli_config_doesnt_exist(app_dir: Path):13 result = runner.invoke(mod.app)14 assert result.exit_code == 015 assert "Config file doesn't exist yet" in result.output161733def test_cli_config_exists(config_file: Path):18def test_cli_config_exists(app_dir: Path):19 (app_dir / "config.json").touch()20 result = runner.invoke(mod.app)21 assert result.exit_code == 022 assert "Config file doesn't exist yet" not in result.output23
8 unmodified lines
modified:tests/test_tutorial/test_launch/test_tutorial002.py-19 removed
1 unmodified line
2import sys3from pathlib import Path4from unittest.mock import patch56import pytest7import typer6from typer.testing import CliRunner78from docs_src.launch import tutorial002_py310 as mod910runner = CliRunner()111215@pytest.fixture(name="app_dir")16def app_dir():17 app_dir = Path(typer.get_app_dir("my-super-cli-app"))18 if app_dir.exists(): # pragma: no cover19 for item in app_dir.iterdir():20 if item.is_file():21 item.unlink()2223 yield app_dir2425 if app_dir.exists():26 for item in app_dir.iterdir():27 if item.is_file():28 item.unlink()29 app_dir.rmdir()303113def test_cli(app_dir: Path):14 with patch("typer.launch") as launch_mock:15 result = runner.invoke(mod.app)16
12 unmodified lines
added:tests/conftest.py+10 added
1from pathlib import Path23import pytest4import typer567@pytest.fixture(name="app_dir")8def patch_app_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:9 monkeypatch.setattr(typer, "get_app_dir", lambda _app_name: str(tmp_path))10 return tmp_path
modified:tests/test_types_file.py+28 added-1 removed
7 unmodified lines
8from typer._click._compat import get_best_encoding, should_strip_ansi9from typer._click.utils import PacifyFlushWrapper10from typer.testing import CliRunner1112from tests.utils import needs_linux, needs_windows12from tests.utils import needs_linux, needs_macos, needs_windows1314app = typer.Typer()1516
284 unmodified lines
301302 assert typer.get_app_dir("My App", force_posix=True) == "/home/tester/.my-app"303304305@needs_linux306def test_app_dir_linux_xdg_config_home(monkeypatch: pytest.MonkeyPatch) -> None:307 monkeypatch.setenv("XDG_CONFIG_HOME", "/home/tester/config")308309 assert typer.get_app_dir("My App") == "/home/tester/config/my-app"310311312@needs_linux313def test_app_dir_linux_default(monkeypatch: pytest.MonkeyPatch) -> None:314 monkeypatch.delenv("XDG_CONFIG_HOME", raising=False)315 monkeypatch.setattr("os.path.expanduser", lambda _path: "/home/tester/.config")316317 assert typer.get_app_dir("My App") == "/home/tester/.config/my-app"318319320@needs_macos321def test_app_dir_macos(monkeypatch: pytest.MonkeyPatch) -> None:322 monkeypatch.setattr(323 "os.path.expanduser", lambda _path: "/Users/tester/Library/Application Support"324 )325326 assert (327 typer.get_app_dir("My App")328 == "/Users/tester/Library/Application Support/My App"329 )330331332def test_text_stream_binary_buffer(monkeypatch) -> None:333 class TextStdinWithBinaryBuffer:334 def __init__(self, data: bytes) -> None:335 self.buffer = BytesIO(data)
82 unmodified lines