Skip to content

Commit

Permalink
Migrate list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Nov 29, 2023
1 parent c0eb767 commit 4b2223d
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 41 deletions.
97 changes: 56 additions & 41 deletions tests/integration/test_lists.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,86 @@
import pytest
from toot import cli

from tests.integration.conftest import register_account

pytest.skip("TODO", allow_module_level=True)


def test_lists_empty(run):
out = run("lists")
assert out == "You have no lists defined."
result = run(cli.lists)
assert result.exit_code == 0
assert result.stdout.strip() == "You have no lists defined."


def test_list_create_delete(run):
out = run("list_create", "banana")
assert out == '✓ List "banana" created.'
result = run(cli.list_create, "banana")
assert result.exit_code == 0
assert result.stdout.strip() == '✓ List "banana" created.'

out = run("lists")
assert "banana" in out
result = run(cli.lists)
assert result.exit_code == 0
assert "banana" in result.stdout

out = run("list_create", "mango")
assert out == '✓ List "mango" created.'
result = run(cli.list_create, "mango")
assert result.exit_code == 0
assert result.stdout.strip() == '✓ List "mango" created.'

out = run("lists")
assert "banana" in out
assert "mango" in out
result = run(cli.lists)
assert result.exit_code == 0
assert "banana" in result.stdout
assert "mango" in result.stdout

out = run("list_delete", "banana")
assert out == '✓ List "banana" deleted.'
result = run(cli.list_delete, "banana")
assert result.exit_code == 0
assert result.stdout.strip() == '✓ List "banana" deleted.'

out = run("lists")
assert "banana" not in out
assert "mango" in out
result = run(cli.lists)
assert result.exit_code == 0
assert "banana" not in result.stdout
assert "mango" in result.stdout

out = run("list_delete", "mango")
assert out == '✓ List "mango" deleted.'
result = run(cli.list_delete, "mango")
assert result.exit_code == 0
assert result.stdout.strip() == '✓ List "mango" deleted.'

out = run("lists")
assert out == "You have no lists defined."
result = run(cli.lists)
assert result.exit_code == 0
assert result.stdout.strip() == "You have no lists defined."

out = run("list_delete", "mango")
assert out == "List not found"
result = run(cli.list_delete, "mango")
assert result.exit_code == 1
assert result.stderr.strip() == "Error: List not found"


def test_list_add_remove(run, app):
acc = register_account(app)
run("list_create", "foo")
run(cli.list_create, "foo")

out = run("list_add", "foo", acc.username)
assert out == f"You must follow @{acc.username} before adding this account to a list."
result = run(cli.list_add, "foo", acc.username)
assert result.exit_code == 1
assert result.stderr.strip() == f"Error: You must follow @{acc.username} before adding this account to a list."

run("follow", acc.username)
run(cli.follow, acc.username)

out = run("list_add", "foo", acc.username)
assert out == f'✓ Added account "{acc.username}"'
result = run(cli.list_add, "foo", acc.username)
assert result.exit_code == 0
assert result.stdout.strip() == f'✓ Added account "{acc.username}"'

out = run("list_accounts", "foo")
assert acc.username in out
result = run(cli.list_accounts, "foo")
assert result.exit_code == 0
assert acc.username in result.stdout

# Account doesn't exist
out = run("list_add", "foo", "does_not_exist")
assert out == "Account not found"
result = run(cli.list_add, "foo", "does_not_exist")
assert result.exit_code == 1
assert result.stderr.strip() == "Error: Account not found"

# List doesn't exist
out = run("list_add", "does_not_exist", acc.username)
assert out == "List not found"
result = run(cli.list_add, "does_not_exist", acc.username)
assert result.exit_code == 1
assert result.stderr.strip() == "Error: List not found"

out = run("list_remove", "foo", acc.username)
assert out == f'✓ Removed account "{acc.username}"'
result = run(cli.list_remove, "foo", acc.username)
assert result.exit_code == 0
assert result.stdout.strip() == f'✓ Removed account "{acc.username}"'

out = run("list_accounts", "foo")
assert out == "This list has no accounts."
result = run(cli.list_accounts, "foo")
assert result.exit_code == 0
assert result.stdout.strip() == "This list has no accounts."
1 change: 1 addition & 0 deletions toot/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from toot.cli.base import cli, Context # noqa

from toot.cli.accounts import *
from toot.cli.lists import *
from toot.cli.post import *
from toot.cli.read import *
from toot.cli.statuses import *
Expand Down
104 changes: 104 additions & 0 deletions toot/cli/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import click

from toot import api
from toot.cli.base import Context, cli, pass_context
from toot.output import print_list_accounts, print_lists


@cli.command()
@pass_context
def lists(ctx: Context):
"""List all lists"""
lists = api.get_lists(ctx.app, ctx.user)

if lists:
print_lists(lists)
else:
click.echo("You have no lists defined.")


@cli.command()
@click.argument("title", required=False)
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_accounts(ctx: Context, title: str, id: str):
"""List the accounts in a list"""
list_id = _get_list_id(ctx, title, id)
response = api.get_list_accounts(ctx.app, ctx.user, list_id)
print_list_accounts(response)


@cli.command()
@click.argument("title")
@click.option(
"--replies-policy",
type=click.Choice(["followed", "list", "none"]),
default="none",
help="Replies policy"
)
@pass_context
def list_create(ctx: Context, title, replies_policy):
"""Create a list"""
api.create_list(ctx.app, ctx.user, title=title, replies_policy=replies_policy)
click.secho(f"✓ List \"{title}\" created.", fg="green")


@cli.command()
@click.argument("title", required=False)
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_delete(ctx: Context, title, id):
"""Delete a list"""
list_id = _get_list_id(ctx, title, id)
api.delete_list(ctx.app, ctx.user, list_id)
click.secho(f"✓ List \"{title if title else id}\" deleted.", fg="green")


@cli.command()
@click.argument("title", required=False)
@click.argument("account")
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_add(ctx: Context, title, account, id):
"""Add an account to a list"""
list_id = _get_list_id(ctx, title, id)
account = api.find_account(ctx.app, ctx.user, account)

try:
api.add_accounts_to_list(ctx.app, ctx.user, list_id, [account["id"]])
except Exception:
# if we failed to add the account, try to give a
# more specific error message than "record not found"
my_accounts = api.followers(ctx.app, ctx.user, account["id"])
found = False
if my_accounts:
for my_account in my_accounts:
if my_account["id"] == account["id"]:
found = True
break
if found is False:
raise click.ClickException(f"You must follow @{account['acct']} before adding this account to a list.")
raise

click.secho(f"✓ Added account \"{account['acct']}\"", fg="green")


@cli.command()
@click.argument("title", required=False)
@click.argument("account")
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_remove(ctx: Context, title, account, id):
"""Remove an account from a list"""
list_id = _get_list_id(ctx, title, id)
account = api.find_account(ctx.app, ctx.user, account)
api.remove_accounts_from_list(ctx.app, ctx.user, list_id, [account["id"]])
click.secho(f"✓ Removed account \"{account['acct']}\"", fg="green")


def _get_list_id(ctx: Context, title, list_id):
if not list_id:
list_id = api.find_list_id(ctx.app, ctx.user, title)
if not list_id:
raise click.ClickException("List not found")
return list_id

0 comments on commit 4b2223d

Please sign in to comment.