✅ 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
Provenance
- Commit
d8ab87d- Tree
704be3a- Author
Yurii Motov <109919500+YuriiMotov@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
82b8395
4 files changed+41 added−37 removed
modified:tests/test_tutorial/test_app_dir/test_tutorial001.py+3 added-17 removed
1 import subprocess2 import sys3 from pathlib import Path4 5-import pytest6-import typer5 from typer.testing import CliRunner6 7 from docs_src.app_dir import tutorial001_py310 as mod8 9 runner = CliRunner()10 11 14-@pytest.fixture(name="config_file")15-def 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)20-21- yield config_path22-23- config_path.unlink()24- app_dir.rmdir()25-26-27-def test_cli_config_doesnt_exist():12+def 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.output16 17 33-def test_cli_config_exists(config_file: Path):18+def 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 linesmodified:tests/test_tutorial/test_launch/test_tutorial002.py-19 removed
1 unmodified line2 import sys3 from pathlib import Path4 from unittest.mock import patch5 6-import pytest7-import typer6 from typer.testing import CliRunner7 8 from docs_src.launch import tutorial002_py310 as mod9 10 runner = CliRunner()11 12 15-@pytest.fixture(name="app_dir")16-def 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()22-23- yield app_dir24-25- if app_dir.exists():26- for item in app_dir.iterdir():27- if item.is_file():28- item.unlink()29- app_dir.rmdir()30-31-13 def test_cli(app_dir: Path):14 with patch("typer.launch") as launch_mock:15 result = runner.invoke(mod.app)16 12 unmodified linesadded:tests/conftest.py+10 added
1+from pathlib import Path2+3+import pytest4+import typer5+6+7+@pytest.fixture(name="app_dir")8+def 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_pathmodified:tests/test_types_file.py+28 added-1 removed
7 unmodified lines8 from typer._click._compat import get_best_encoding, should_strip_ansi9 from typer._click.utils import PacifyFlushWrapper10 from typer.testing import CliRunner11 12-from tests.utils import needs_linux, needs_windows12+from tests.utils import needs_linux, needs_macos, needs_windows13 14 app = typer.Typer()15 16 284 unmodified lines301 302 assert typer.get_app_dir("My App", force_posix=True) == "/home/tester/.my-app"303 304 305+@needs_linux306+def test_app_dir_linux_xdg_config_home(monkeypatch: pytest.MonkeyPatch) -> None:307+ monkeypatch.setenv("XDG_CONFIG_HOME", "/home/tester/config")308+309+ assert typer.get_app_dir("My App") == "/home/tester/config/my-app"310+311+312+@needs_linux313+def 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")316+317+ assert typer.get_app_dir("My App") == "/home/tester/.config/my-app"318+319+320+@needs_macos321+def test_app_dir_macos(monkeypatch: pytest.MonkeyPatch) -> None:322+ monkeypatch.setattr(323+ "os.path.expanduser", lambda _path: "/Users/tester/Library/Application Support"324+ )325+326+ assert (327+ typer.get_app_dir("My App")328+ == "/Users/tester/Library/Application Support/My App"329+ )330+331+332 def 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