diff --git a/task.py b/task.py index 53cc8ed..45ac0d1 100644 --- a/task.py +++ b/task.py @@ -10,12 +10,30 @@ from commands.done import mark_done -def load_config(): +DEFAULT_CONFIG = """# Task CLI configuration + +storage: + format: json + max_tasks: 1000 + +display: + color: true + unicode: true +""" + + +def load_config(config_path=None): """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 - with open(config_path) as f: - return f.read() + if config_path is None: + config_path = Path.home() / ".config" / "task-cli" / "config.yaml" + else: + config_path = Path(config_path) + + if not config_path.exists(): + config_path.parent.mkdir(parents=True, exist_ok=True) + config_path.write_text(DEFAULT_CONFIG) + + return config_path.read_text() def main(): @@ -35,6 +53,8 @@ def main(): args = parser.parse_args() + load_config() + if args.command == "add": add_task(args.description) elif args.command == "list": diff --git a/test_task.py b/test_task.py index ba98e43..22aa249 100644 --- a/test_task.py +++ b/test_task.py @@ -3,6 +3,7 @@ import json import pytest from pathlib import Path +from task import DEFAULT_CONFIG, load_config from commands.add import add_task, validate_description from commands.done import validate_task_id @@ -28,3 +29,20 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_load_config_creates_default_when_missing(tmp_path): + """Missing config file should be created with defaults.""" + config_path = tmp_path / ".config" / "task-cli" / "config.yaml" + + assert load_config(config_path) == DEFAULT_CONFIG + assert config_path.read_text() == DEFAULT_CONFIG + + +def test_load_config_reads_existing_file(tmp_path): + """Existing config file should be returned unchanged.""" + config_path = tmp_path / ".config" / "task-cli" / "config.yaml" + config_path.parent.mkdir(parents=True) + config_path.write_text("display:\n color: false\n") + + assert load_config(config_path) == "display:\n color: false\n"