diff --git a/__pycache__/task.cpython-313.pyc b/__pycache__/task.cpython-313.pyc new file mode 100644 index 0000000..933326a Binary files /dev/null and b/__pycache__/task.cpython-313.pyc differ diff --git a/commands/__pycache__/__init__.cpython-313.pyc b/commands/__pycache__/__init__.cpython-313.pyc new file mode 100644 index 0000000..f3192a7 Binary files /dev/null and b/commands/__pycache__/__init__.cpython-313.pyc differ diff --git a/commands/__pycache__/add.cpython-313.pyc b/commands/__pycache__/add.cpython-313.pyc new file mode 100644 index 0000000..db228b5 Binary files /dev/null and b/commands/__pycache__/add.cpython-313.pyc differ diff --git a/commands/__pycache__/done.cpython-313.pyc b/commands/__pycache__/done.cpython-313.pyc new file mode 100644 index 0000000..5d3a685 Binary files /dev/null and b/commands/__pycache__/done.cpython-313.pyc differ diff --git a/commands/__pycache__/list.cpython-313.pyc b/commands/__pycache__/list.cpython-313.pyc new file mode 100644 index 0000000..f883956 Binary files /dev/null and b/commands/__pycache__/list.cpython-313.pyc differ diff --git a/task.py b/task.py index 53cc8ed..9b84424 100644 --- a/task.py +++ b/task.py @@ -3,19 +3,30 @@ import argparse import sys +import json from pathlib import Path from commands.add import add_task from commands.list import list_tasks from commands.done import mark_done +DEFAULT_CONFIG = {"tasks_file": "tasks.json", "verbose": False} + def load_config(): - """Load configuration from file.""" + """Load configuration from file, creating default if missing.""" 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 not config_path.exists(): + config_path.parent.mkdir(parents=True, exist_ok=True) + config_path.write_text(json.dumps(DEFAULT_CONFIG, indent=2)) + print(f"Created default config at {config_path}", file=sys.stderr) + return DEFAULT_CONFIG + try: + with open(config_path) as f: + return json.load(f) or DEFAULT_CONFIG + except (json.JSONDecodeError, OSError) as e: + print(f"Error reading config: {e}. Using defaults.", file=sys.stderr) + return DEFAULT_CONFIG def main(): diff --git a/test_task.py b/test_task.py index ba98e43..1ce7a97 100644 --- a/test_task.py +++ b/test_task.py @@ -1,10 +1,12 @@ """Basic tests for task CLI.""" import json +import os 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 +30,24 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_load_config_missing_file(tmp_path, monkeypatch): + """Config file missing: auto-create default without crashing.""" + fake_home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: fake_home) + config = load_config() + assert config == {"tasks_file": "tasks.json", "verbose": False} + config_file = fake_home / ".config" / "task-cli" / "config.yaml" + assert config_file.exists() + + +def test_load_config_corrupt_file(tmp_path, monkeypatch): + """Malformed config file: return defaults without crashing.""" + fake_home = tmp_path / "home" + config_dir = fake_home / ".config" / "task-cli" + config_dir.mkdir(parents=True) + (config_dir / "config.yaml").write_text("{{{not valid json") + monkeypatch.setattr(Path, "home", lambda: fake_home) + config = load_config() + assert config == {"tasks_file": "tasks.json", "verbose": False}