diff --git a/task.py b/task.py index 53cc8ed..4ea1fd8 100644 --- a/task.py +++ b/task.py @@ -2,6 +2,7 @@ """Simple task manager CLI.""" import argparse +import os import sys from pathlib import Path @@ -9,12 +10,40 @@ from commands.list import list_tasks from commands.done import mark_done +# Module-level config paths (can be overridden for testing) +CONFIG_DIR = Path.home() / ".config" / "task-cli" +CONFIG_PATH = CONFIG_DIR / "config.yaml" + +DEFAULT_CONFIG = """# Default configuration for task CLI +# Auto-created if not present + +# Task storage settings +storage: + format: json + max_tasks: 1000 + +# Display settings +display: + color: true + unicode: true +""" + 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 - with open(config_path) as f: + """Load configuration from file. Creates default if missing.""" + global CONFIG_PATH, CONFIG_DIR + + if not CONFIG_PATH.exists(): + # Create default config directory and file + try: + CONFIG_DIR.mkdir(parents=True, exist_ok=True) + CONFIG_PATH.write_text(DEFAULT_CONFIG) + print(f"Created default config at {CONFIG_PATH}", file=sys.stderr) + except OSError as e: + print(f"Error: Could not create config file at {CONFIG_PATH}: {e}", file=sys.stderr) + sys.exit(1) + + with open(CONFIG_PATH) as f: return f.read() diff --git a/test_task.py b/test_task.py index ba98e43..206aa71 100644 --- a/test_task.py +++ b/test_task.py @@ -1,6 +1,8 @@ """Basic tests for task CLI.""" import json +import os +import tempfile import pytest from pathlib import Path from commands.add import add_task, validate_description @@ -28,3 +30,38 @@ def test_validate_task_id(): with pytest.raises(ValueError): validate_task_id(tasks, 99) + + +def test_load_config_creates_default_when_missing(): + """Test that load_config creates default config when missing (BUG FIX).""" + import sys + sys.path.insert(0, str(Path(__file__).parent)) + import task + + # Use temp directory for testing + with tempfile.TemporaryDirectory() as tmpdir: + tmp_config_dir = Path(tmpdir) / ".config" / "task-cli" + tmp_config_path = tmp_config_dir / "config.yaml" + + # Ensure config doesn't exist + assert not tmp_config_path.exists() + + # Temporarily override paths + original_config_dir = task.config_dir + original_config_path = task.config_path + task.config_dir = tmp_config_dir + task.config_path = tmp_config_path + + try: + # Call load_config which should create the default + task.load_config() + + # Verify config was created + assert tmp_config_path.exists(), "Config file should be created" + content = tmp_config_path.read_text() + assert "storage:" in content, "Default config should have storage section" + assert "display:" in content, "Default config should have display section" + finally: + # Restore original paths + task.config_dir = original_config_dir + task.config_path = original_config_path