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
10 changes: 8 additions & 2 deletions commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ 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:
if json_output:
print("[]")
return
print("No tasks yet!")
return

tasks = json.loads(tasks_file.read_text())

if json_output:
print(json.dumps(tasks, indent=2))
return

if not tasks:
print("No tasks yet!")
return
Expand Down
3 changes: 2 additions & 1 deletion task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def main():

# 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")
Expand All @@ -38,7 +39,7 @@ def main():
if args.command == "add":
add_task(args.description)
elif args.command == "list":
list_tasks()
list_tasks(json_output=args.json)
elif args.command == "done":
mark_done(args.task_id)
else:
Expand Down
14 changes: 14 additions & 0 deletions test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from commands.add import add_task, validate_description
from commands.done import validate_task_id
from commands.list import list_tasks


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

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


def test_list_tasks_json_output(tmp_path, monkeypatch, capsys):
"""Test JSON list output."""
tasks_file = tmp_path / ".local" / "share" / "task-cli" / "tasks.json"
tasks_file.parent.mkdir(parents=True)
tasks_file.write_text(json.dumps([{"id": 1, "description": "test", "done": False}]))
monkeypatch.setattr(Path, "home", lambda: tmp_path)

list_tasks(json_output=True)

output = capsys.readouterr().out
assert json.loads(output) == [{"id": 1, "description": "test", "done": False}]