Skip to content

Commit

Permalink
Try to create cache directory on each CLI call (#87)
Browse files Browse the repository at this point in the history
Try to create cache directory on each CLI call.
  • Loading branch information
CasperWA authored Feb 13, 2024
1 parent 3a91cba commit 3a980c5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
17 changes: 15 additions & 2 deletions entities_service/cli/_utils/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
) from exc

from entities_service import __version__
from entities_service.cli._utils.generics import print
from entities_service.cli._utils.generics import CACHE_DIRECTORY, ERROR_CONSOLE, print
from entities_service.service.config import CONFIG

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -69,6 +69,19 @@ def global_options(
rich_help_panel="Global options",
),
) -> None:
"""Global options for the CLI."""
"""Global options for the CLI.
This function is also used to run initial setup for the CLI.
"""
if dotenv_path:
CONTEXT["dotenv_path"] = dotenv_path

# Initialize the cache directory
try:
CACHE_DIRECTORY.mkdir(parents=True, exist_ok=True)
except PermissionError as exc:
ERROR_CONSOLE.print(
f"[bold red]Error[/bold red]: {CACHE_DIRECTORY} is not writable. "
"Please check your permissions."
)
raise typer.Exit(1) from exc
49 changes: 49 additions & 0 deletions tests/cli/_utils/test_global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,52 @@ def test_dotenv_path(
assert not result.stderr

assert CONTEXT["dotenv_path"] == dotenv_path


def test_cache_dir_creation(cli: CliRunner, tmp_cache_dir: Path) -> None:
"""Ensure the cache directiory is created if it does not already exist.
Note, the callback to `global_settings` is not done if invoking the CLI without any
arguments. It will instead go straight to outputting the help.
However, when calling any command, `global_settings` is called.
Hence, `upload` is called here, which will simply return the help for that command.
"""
from entities_service.cli.main import APP

# tmp_cache_dir should not yet exist
assert not tmp_cache_dir.exists()

result = cli.invoke(APP, "upload")
assert result.exit_code == 0, CLI_RESULT_FAIL_MESSAGE.format(
stdout=result.stdout, stderr=result.stderr
)
assert not result.stderr

# tmp_cache_dir should now exist
assert tmp_cache_dir.exists()


def test_cache_dir_permissionerror(cli: CliRunner, tmp_path: Path) -> None:
"""Ensure a PermissionError is raised and handled if the cache dir cannot be
created.
Note, the callback to `global_settings` is not done if invoking the CLI without any
arguments. It will instead go straight to outputting the help.
However, when calling any command, `global_settings` is called.
Hence, `upload` is called here, which will simply return the help for that command.
"""
from entities_service.cli.main import APP

org_mode = tmp_path.stat().st_mode
tmp_path.chmod(0x555)

result = cli.invoke(APP, "upload")
assert result.exit_code != 0, CLI_RESULT_FAIL_MESSAGE.format(
stdout=result.stdout, stderr=result.stderr
)
assert not result.stdout

assert "Error: " in result.stderr
assert "is not writable. Please check your permissions." in result.stderr

tmp_path.chmod(org_mode)
5 changes: 2 additions & 3 deletions tests/cli/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def config_app() -> Typer:
@pytest.fixture()
def tmp_cache_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Create a temporary cache directory."""
from entities_service.cli._utils import generics
from entities_service.cli._utils import generics, global_settings

cache_dir = tmp_path / ".cache" / "entities-service"
cache_dir.mkdir(parents=True)

monkeypatch.setattr(generics, "CACHE_DIRECTORY", cache_dir)
monkeypatch.setattr(global_settings, "CACHE_DIRECTORY", cache_dir)

return cache_dir

Expand All @@ -67,7 +67,6 @@ def _function_specific_cli_cache_dir(
from entities_service.cli._utils import generics

cache = JsonTokenFileCache(str(tmp_cache_file))
cache.clear()

monkeypatch.setattr(generics.OAuth2, "token_cache", cache)

Expand Down

0 comments on commit 3a980c5

Please sign in to comment.