Coverage for src/qdrant_loader/cli/path_utils.py: 91%
22 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-08 06:05 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-08 06:05 +0000
1from __future__ import annotations
3from pathlib import Path
5import click
8def create_database_directory(path: Path) -> bool:
9 """Prompt to create a directory for the database; return True if created.
11 Caller is responsible for logging around this utility.
12 """
13 abs_path = path.resolve()
14 # If directory already exists, no need to prompt; treat as success
15 # Use is_dir() in addition to exists() to avoid false positives in tests that patch exists
16 if abs_path.exists() and abs_path.is_dir():
17 return True
18 # If a filesystem entry exists at the path but it's not a directory,
19 # offer to create the directory (for back-compat with tests that patch exists()).
20 # Treat this the same as a missing directory from a UX perspective.
21 if abs_path.exists() and not abs_path.is_dir():
22 if not click.confirm(
23 f"Directory does not exist: {abs_path}. Create it?", default=True
24 ):
25 return False
26 try:
27 abs_path.mkdir(parents=True, exist_ok=True)
28 return True
29 except OSError as e:
30 raise click.ClickException(
31 f"Failed to create directory '{abs_path}': {e}"
32 ) from e
34 # Prompt user with explicit path information when directory truly doesn't exist
35 if click.confirm(f"Directory does not exist: {abs_path}. Create it?", default=True):
36 try:
37 # Use OS defaults for mode; create parents as needed
38 abs_path.mkdir(parents=True, exist_ok=True)
39 return True
40 except OSError as e:
41 # Raise ClickException with clear message and original error
42 raise click.ClickException(
43 f"Failed to create directory '{abs_path}': {e}"
44 ) from e
45 return False