Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 3 additions & 17 deletions tests/test_tutorial/test_app_dir/test_tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,21 @@
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

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
Expand Down
19 changes: 0 additions & 19 deletions tests/test_tutorial/test_launch/test_tutorial002.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,13 @@
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

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)
Expand Down
29 changes: 28 additions & 1 deletion tests/test_types_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Comment thread
svlandeg marked this conversation as resolved.
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:
Expand Down