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
15 changes: 1 addition & 14 deletions commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
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 commands.utils import get_tasks_file, validate_description


def add_task(description):
Expand Down
14 changes: 1 addition & 13 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 commands.utils import get_tasks_file, validate_task_id


def mark_done(task_id):
Expand Down
21 changes: 4 additions & 17 deletions commands/list.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
"""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 commands.utils import get_tasks_file


def list_tasks():
"""List all tasks."""
# NOTE: No --json flag support yet (feature bounty)
tasks_file = validate_task_file()
if not tasks_file:
tasks_file = get_tasks_file()

if not tasks_file.exists():
print("No tasks yet!")
return

Expand Down
49 changes: 49 additions & 0 deletions commands/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Shared validation utilities for task CLI commands."""

from pathlib import Path


def get_tasks_file():
"""Get path to the tasks storage file.

Returns:
Path to ~/.local/share/task-cli/tasks.json
"""
return Path.home() / ".local" / "share" / "task-cli" / "tasks.json"


def validate_description(description):
"""Validate a task description.

Args:
description: Raw description string.

Returns:
Stripped description string.

Raises:
ValueError: If description is empty or exceeds 200 characters.
"""
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 that a task ID exists in the task list.

Args:
tasks: List of task dicts.
task_id: Integer task ID to validate.

Returns:
The validated task_id.

Raises:
ValueError: If task_id is out of range.
"""
if task_id < 1 or task_id > len(tasks):
raise ValueError(f"Invalid task ID: {task_id}")
return task_id
13 changes: 10 additions & 3 deletions test_task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Basic tests for task CLI."""
"""Tests for task CLI."""

import json
import pytest
from pathlib import Path
from commands.add import add_task, validate_description
from commands.done import validate_task_id

from commands.utils import validate_description, validate_task_id, get_tasks_file


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

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


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