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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ python task.py --help
# Add a task
python task.py add "Buy groceries"

# Add a task and print machine-readable JSON
python task.py add "Buy groceries" --json
# {"task": {"id": 1, "description": "Buy groceries", "done": false}, "message": "Added task 1"}

# List tasks
python task.py list

# List tasks as JSON
python task.py list --json
# {"tasks": [{"id": 1, "description": "Buy groceries", "done": false}], "count": 1}

# Complete a task
python task.py done 1

# Complete a task and print JSON
python task.py done 1 --json
# {"task": {"id": 1, "description": "Buy groceries", "done": true}, "message": "Marked task 1 as done"}
```

## Testing
Expand Down
11 changes: 8 additions & 3 deletions commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def validate_description(description):
return description.strip()


def add_task(description):
def add_task(description, json_output=False):
"""Add a new task."""
description = validate_description(description)

Expand All @@ -31,7 +31,12 @@ def add_task(description):
tasks = json.loads(tasks_file.read_text())

task_id = len(tasks) + 1
tasks.append({"id": task_id, "description": description, "done": False})
task = {"id": task_id, "description": description, "done": False}
tasks.append(task)

tasks_file.write_text(json.dumps(tasks, indent=2))
print(f"Added task {task_id}: {description}")

if json_output:
print(json.dumps({"task": task, "message": f"Added task {task_id}"}))
else:
print(f"Added task {task_id}: {description}")
17 changes: 13 additions & 4 deletions commands/done.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ def validate_task_id(tasks, task_id):
return task_id


def mark_done(task_id):
def mark_done(task_id, json_output=False):
"""Mark a task as complete."""
tasks_file = get_tasks_file()
if not tasks_file.exists():
print("No tasks found!")
if json_output:
print(json.dumps({"task": None, "message": "No tasks found"}))
else:
print("No tasks found!")
return

tasks = json.loads(tasks_file.read_text())
Expand All @@ -31,7 +34,13 @@ def mark_done(task_id):
if task["id"] == task_id:
task["done"] = True
tasks_file.write_text(json.dumps(tasks, indent=2))
print(f"Marked task {task_id} as done: {task['description']}")
if json_output:
print(json.dumps({"task": task, "message": f"Marked task {task_id} as done"}))
else:
print(f"Marked task {task_id} as done: {task['description']}")
return

print(f"Task {task_id} not found")
if json_output:
print(json.dumps({"task": None, "message": f"Task {task_id} not found"}))
else:
print(f"Task {task_id} not found")
12 changes: 7 additions & 5 deletions commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ def validate_task_file():
return tasks_file


def list_tasks():
def list_tasks(json_output=False):
"""List all tasks."""
# NOTE: No --json flag support yet (feature bounty)
tasks_file = validate_task_file()
if not tasks_file:
print("No tasks yet!")
return
tasks = []
else:
tasks = json.loads(tasks_file.read_text())

tasks = json.loads(tasks_file.read_text())
if json_output:
print(json.dumps({"tasks": tasks, "count": len(tasks)}))
return

if not tasks:
print("No tasks yet!")
Expand Down
10 changes: 6 additions & 4 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Simple task manager CLI."""

import argparse
import sys
from pathlib import Path

from commands.add import add_task
Expand All @@ -25,22 +24,25 @@ def main():
# Add command
add_parser = subparsers.add_parser("add", help="Add a new task")
add_parser.add_argument("description", help="Task description")
add_parser.add_argument("--json", action="store_true", help="Output result as JSON")

# List command
list_parser = subparsers.add_parser("list", help="List all tasks")
list_parser.add_argument("--json", action="store_true", help="Output tasks as JSON")

# Done command
done_parser = subparsers.add_parser("done", help="Mark task as complete")
done_parser.add_argument("task_id", type=int, help="Task ID to mark done")
done_parser.add_argument("--json", action="store_true", help="Output result as JSON")

args = parser.parse_args()

if args.command == "add":
add_task(args.description)
add_task(args.description, json_output=args.json)
elif args.command == "list":
list_tasks()
list_tasks(json_output=args.json)
elif args.command == "done":
mark_done(args.task_id)
mark_done(args.task_id, json_output=args.json)
else:
parser.print_help()

Expand Down
60 changes: 59 additions & 1 deletion test_task.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Basic tests for task CLI."""

import json
import os
import subprocess
import sys

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

Expand All @@ -28,3 +31,58 @@ def test_validate_task_id():

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


def run_task_cli(tmp_path, *args):
"""Run the task CLI with an isolated HOME directory."""
env = os.environ.copy()
env["HOME"] = str(tmp_path)
result = subprocess.run(
[sys.executable, "task.py", *args],
capture_output=True,
check=True,
env=env,
text=True,
)
return result.stdout.strip()


def test_add_json_output(tmp_path):
"""Add command prints parseable JSON when --json is present."""
output = run_task_cli(tmp_path, "add", "Write tests", "--json")
payload = json.loads(output)

assert payload["task"] == {"id": 1, "description": "Write tests", "done": False}
assert payload["message"] == "Added task 1"


def test_list_json_output(tmp_path):
"""List command prints all tasks as parseable JSON."""
run_task_cli(tmp_path, "add", "First task")
run_task_cli(tmp_path, "add", "Second task")

output = run_task_cli(tmp_path, "list", "--json")
payload = json.loads(output)

assert payload["count"] == 2
assert payload["tasks"] == [
{"id": 1, "description": "First task", "done": False},
{"id": 2, "description": "Second task", "done": False},
]


def test_done_json_output(tmp_path):
"""Done command prints the completed task as parseable JSON."""
run_task_cli(tmp_path, "add", "Finish bounty")

output = run_task_cli(tmp_path, "done", "1", "--json")
payload = json.loads(output)

assert payload["task"] == {"id": 1, "description": "Finish bounty", "done": True}
assert payload["message"] == "Marked task 1 as done"


def test_list_json_output_when_empty(tmp_path):
"""List command returns an empty task array as JSON with no task file."""
output = run_task_cli(tmp_path, "list", "--json")
assert json.loads(output) == {"tasks": [], "count": 0}