diff --git a/task.py b/task.py index 53cc8ed..9638a05 100644 --- a/task.py +++ b/task.py @@ -13,7 +13,10 @@ 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(): + raise FileNotFoundError( + f"Config not found at {config_path}. Copy config.yaml.example to this location to get started." + ) with open(config_path) as f: return f.read() @@ -35,6 +38,12 @@ def main(): args = parser.parse_args() + try: + load_config() + except FileNotFoundError as e: + print(str(e), file=sys.stderr) + sys.exit(1) + if args.command == "add": add_task(args.description) elif args.command == "list": diff --git a/test_task.py b/test_task.py index ba98e43..34b30ba 100644 --- a/test_task.py +++ b/test_task.py @@ -1,10 +1,10 @@ """Basic tests for task CLI.""" -import json import pytest from pathlib import Path from commands.add import add_task, validate_description from commands.done import validate_task_id +from task import load_config def test_validate_description(): @@ -28,3 +28,20 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_load_config_missing_file(monkeypatch, tmp_path): + """Missing config should raise a friendly FileNotFoundError.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + with pytest.raises(FileNotFoundError, match="config.yaml.example"): + load_config() + + +def test_load_config_success(monkeypatch, tmp_path): + """Existing config should be loaded successfully.""" + monkeypatch.setattr(Path, "home", lambda: tmp_path) + config_dir = tmp_path / ".config" / "task-cli" + config_dir.mkdir(parents=True) + config_file = config_dir / "config.yaml" + config_file.write_text("theme: default\n") + assert load_config() == "theme: default\n"