diff --git a/README.md b/README.md index 91da9a7..0deb45d 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,15 @@ python -m pytest test_task.py ## Configuration -Copy `config.yaml.example` to `~/.config/task-cli/config.yaml` and customize. +The CLI reads configuration from `~/.config/task-cli/config.yaml`. + +If the file does not exist, the CLI creates it with default settings: + +```yaml +settings: + format: text + max_tasks: 1000 +``` + +You can also copy `config.yaml.example` to `~/.config/task-cli/config.yaml` +and customize it before running commands. diff --git a/task.py b/task.py index 53cc8ed..200f04f 100644 --- a/task.py +++ b/task.py @@ -9,11 +9,20 @@ from commands.list import list_tasks from commands.done import mark_done +DEFAULT_CONFIG = """# Task CLI configuration +settings: + format: text + max_tasks: 1000 +""" + def load_config(): """Load configuration from file.""" config_path = Path.home() / ".config" / "task-cli" / "config.yaml" - # NOTE: This will crash if config doesn't exist - known bug for bounty testing + if not config_path.exists(): + config_path.parent.mkdir(parents=True, exist_ok=True) + config_path.write_text(DEFAULT_CONFIG) + with open(config_path) as f: return f.read() diff --git a/test_task.py b/test_task.py index ba98e43..37c8b95 100644 --- a/test_task.py +++ b/test_task.py @@ -5,6 +5,7 @@ from pathlib import Path from commands.add import add_task, validate_description from commands.done import validate_task_id +from task import DEFAULT_CONFIG, load_config def test_validate_description(): @@ -28,3 +29,15 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_load_config_creates_default_when_missing(tmp_path, monkeypatch): + """Missing config file should be created with sensible defaults.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + + config = load_config() + config_path = tmp_path / ".config" / "task-cli" / "config.yaml" + + assert config == DEFAULT_CONFIG + assert config_path.exists() + assert config_path.read_text() == DEFAULT_CONFIG