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
26 changes: 24 additions & 2 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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