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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ python -m pytest test_task.py
## Configuration

Copy `config.yaml.example` to `~/.config/task-cli/config.yaml` and customize.
If the config file is missing, a sensible default is created automatically on first run.
27 changes: 24 additions & 3 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,30 @@


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
"""Load configuration from file.

Returns the config contents as a string, or a sensible default config
if the file does not exist yet. A default config file is also created
on disk so future runs pick it up automatically.
"""
config_dir = Path.home() / ".config" / "task-cli"
config_path = config_dir / "config.yaml"

if not config_path.exists():
# Create a sensible default config
default_config = (
"# Task CLI configuration\n"
"storage:\n"
" format: json\n"
" max_tasks: 1000\n"
"display:\n"
" color: true\n"
" unicode: true\n"
)
config_dir.mkdir(parents=True, exist_ok=True)
config_path.write_text(default_config)
return default_config

with open(config_path) as f:
return f.read()

Expand Down
40 changes: 40 additions & 0 deletions test_task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Basic tests for task CLI."""

import json
import os
import shutil
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():
Expand All @@ -28,3 +31,40 @@ def test_validate_task_id():

with pytest.raises(ValueError):
validate_task_id(tasks, 99)


def test_load_config_creates_default_when_missing(tmp_path, monkeypatch):
"""Test that load_config() creates a default config when the file is missing."""
fake_home = tmp_path / "home"
fake_home.mkdir()
monkeypatch.setattr(Path, "home", staticmethod(lambda: fake_home))

config_dir = fake_home / ".config" / "task-cli"
config_path = config_dir / "config.yaml"

# Ensure config does not exist
assert not config_path.exists()

# load_config should NOT crash and should return default content
result = load_config()
assert result is not None
assert "storage:" in result
assert "display:" in result

# It should have created the file on disk
assert config_path.exists()
assert config_path.read_text() == result


def test_load_config_reads_existing_file(tmp_path, monkeypatch):
"""Test that load_config() reads an existing config file."""
fake_home = tmp_path / "home"
config_dir = fake_home / ".config" / "task-cli"
config_dir.mkdir(parents=True)
config_path = config_dir / "config.yaml"
config_path.write_text("custom: value\n")

monkeypatch.setattr(Path, "home", staticmethod(lambda: fake_home))

result = load_config()
assert "custom: value" in result