Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,48 @@
"""Simple task manager CLI."""

import argparse
import os
import sys
from pathlib import Path

from commands.add import add_task
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()


Expand Down
37 changes: 37 additions & 0 deletions test_task.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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