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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.pyc
.pytest_cache/
18 changes: 2 additions & 16 deletions commands/add.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
"""Add task command."""

import json
from pathlib import Path


def get_tasks_file():
"""Get path to tasks file."""
return Path.home() / ".local" / "share" / "task-cli" / "tasks.json"


def validate_description(description):
"""Validate task description."""
# NOTE: Validation logic scattered here - should be in utils (refactor bounty)
if not description:
raise ValueError("Description cannot be empty")
if len(description) > 200:
raise ValueError("Description too long (max 200 chars)")
return description.strip()
from utils.paths import get_tasks_file
from utils.validation import validate_description


def add_task(description):
Expand Down
16 changes: 2 additions & 14 deletions commands/done.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
"""Mark task done command."""

import json
from pathlib import Path


def get_tasks_file():
"""Get path to tasks file."""
return Path.home() / ".local" / "share" / "task-cli" / "tasks.json"


def validate_task_id(tasks, task_id):
"""Validate task ID exists."""
# NOTE: Validation logic scattered here - should be in utils (refactor bounty)
if task_id < 1 or task_id > len(tasks):
raise ValueError(f"Invalid task ID: {task_id}")
return task_id
from utils.paths import get_tasks_file
from utils.validation import validate_task_id


def mark_done(task_id):
Expand Down
20 changes: 3 additions & 17 deletions commands/list.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
"""List tasks command."""

import json
from pathlib import Path


def get_tasks_file():
"""Get path to tasks file."""
return Path.home() / ".local" / "share" / "task-cli" / "tasks.json"


def validate_task_file():
"""Validate tasks file exists."""
# NOTE: Validation logic scattered here - should be in utils (refactor bounty)
tasks_file = get_tasks_file()
if not tasks_file.exists():
return []
return tasks_file
from utils.paths import get_tasks_file
from utils.validation import validate_task_file


def list_tasks():
"""List all tasks."""
# NOTE: No --json flag support yet (feature bounty)
tasks_file = validate_task_file()
tasks_file = validate_task_file(get_tasks_file())
if not tasks_file:
print("No tasks yet!")
return
Expand Down
15 changes: 7 additions & 8 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
from commands.add import add_task
from commands.list import list_tasks
from commands.done import mark_done


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:
return f.read()
from utils.paths import load_config


def main():
Expand All @@ -35,6 +28,12 @@ def main():

args = parser.parse_args()

# Load config gracefully - don't crash if missing
config = load_config()
if config is None:
print("Warning: No config file found at ~/.config/task-cli/config.yaml")
print("Using default settings. Create a config file to customize behavior.")

if args.command == "add":
add_task(args.description)
elif args.command == "list":
Expand Down
37 changes: 35 additions & 2 deletions test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import json
import pytest
from pathlib import Path
from commands.add import add_task, validate_description
from commands.done import validate_task_id
from utils.validation import validate_description, validate_task_id
from utils.paths import get_tasks_file, get_config_path, load_config


def test_validate_description():
Expand All @@ -28,3 +28,36 @@ def test_validate_task_id():

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


def test_get_tasks_file():
"""Test tasks file path generation."""
path = get_tasks_file()
assert "task-cli" in str(path)
assert path.name == "tasks.json"


def test_get_config_path():
"""Test config path generation."""
path = get_config_path()
assert "task-cli" in str(path)
assert path.name == "config.yaml"


def test_load_config_missing():
"""Test load_config returns None when config doesn't exist."""
# Temporarily clear config if it exists
config_path = get_config_path()
backup_exists = config_path.exists()
backup_content = None
if backup_exists:
backup_content = config_path.read_text()
config_path.unlink()

try:
result = load_config()
assert result is None
finally:
if backup_exists and backup_content:
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(backup_content)
Empty file added utils/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions utils/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Shared path helpers for task CLI."""

from pathlib import Path


def get_tasks_file():
"""Get path to tasks file."""
return Path.home() / ".local" / "share" / "task-cli" / "tasks.json"


def get_config_path():
"""Get path to config file."""
return Path.home() / ".config" / "task-cli" / "config.yaml"


def load_config():
"""Load configuration from file.

Returns config content as string, or None if config doesn't exist.
"""
config_path = get_config_path()
if not config_path.exists():
return None
return config_path.read_text()
27 changes: 27 additions & 0 deletions utils/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Validation utilities for task CLI."""


def validate_description(description):
"""Validate task description."""
if not description:
raise ValueError("Description cannot be empty")
if len(description) > 200:
raise ValueError("Description too long (max 200 chars)")
return description.strip()


def validate_task_id(tasks, task_id):
"""Validate task ID exists."""
if task_id < 1 or task_id > len(tasks):
raise ValueError(f"Invalid task ID: {task_id}")
return task_id


def validate_task_file(tasks_file):
"""Validate tasks file exists.

Returns parsed task list or None if file doesn't exist.
"""
if not tasks_file.exists():
return None
return tasks_file