From 1ef921d01c7a4e35166082e806169250fb1c8e5b Mon Sep 17 00:00:00 2001 From: njg7194 Date: Sun, 1 Feb 2026 15:07:30 +0900 Subject: [PATCH] fix: handle missing config file gracefully - Add DEFAULT_CONFIG constant with sensible defaults - Create default config file when missing instead of crashing - Create parent directories if they don't exist - Inform user when default config is created - Add comprehensive tests for missing and existing config scenarios Closes #2 --- task.py | 26 ++++++++++++++++++++++++-- test_task.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/task.py b/task.py index 53cc8ed..f0b6c7c 100644 --- a/task.py +++ b/task.py @@ -10,10 +10,32 @@ from commands.done import mark_done +DEFAULT_CONFIG = """# Default configuration for task CLI +# Customize at ~/.config/task-cli/config.yaml + +# Task storage settings +storage: + format: json + max_tasks: 1000 + +# Display settings +display: + color: true + unicode: true +""" + + 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 + + if not config_path.exists(): + # Create config directory if it doesn't exist + config_path.parent.mkdir(parents=True, exist_ok=True) + # Write default config + config_path.write_text(DEFAULT_CONFIG) + print(f"Created default config at: {config_path}") + with open(config_path) as f: return f.read() diff --git a/test_task.py b/test_task.py index ba98e43..5ed7c42 100644 --- a/test_task.py +++ b/test_task.py @@ -2,9 +2,13 @@ import json import pytest +import tempfile +import os from pathlib import Path +from unittest.mock import patch from commands.add import add_task, validate_description from commands.done import validate_task_id +from task import load_config, DEFAULT_CONFIG def test_validate_description(): @@ -28,3 +32,40 @@ 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 file is missing.""" + with tempfile.TemporaryDirectory() as tmpdir: + # Mock Path.home() to return our temp directory + fake_home = Path(tmpdir) + config_path = fake_home / ".config" / "task-cli" / "config.yaml" + + with patch.object(Path, 'home', return_value=fake_home): + # Config should not exist initially + assert not config_path.exists() + + # Load config - should create default + result = load_config() + + # Config file should now exist + assert config_path.exists() + + # Content should match default config + assert result == DEFAULT_CONFIG + + +def test_load_config_reads_existing_config(): + """Test that load_config reads existing config file.""" + with tempfile.TemporaryDirectory() as tmpdir: + fake_home = Path(tmpdir) + config_path = fake_home / ".config" / "task-cli" / "config.yaml" + + # Create config directory and file + config_path.parent.mkdir(parents=True, exist_ok=True) + custom_config = "custom: config\n" + config_path.write_text(custom_config) + + with patch.object(Path, 'home', return_value=fake_home): + result = load_config() + assert result == custom_config