Skip to content

Commit

Permalink
feat: add -d option to rm command to delete files
Browse files Browse the repository at this point in the history
Use the `-d` or `--delete` option to remove the underlying files when removing an item from the library.
  • Loading branch information
Moe bot authored and jtpavlock committed Dec 20, 2022
1 parent fac29a1 commit 8246eb8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Removes music from your library.

.. code-block:: bash
moe remove [-h] [-a | -e] query
moe remove [-h] [-a | -e] [-d] query
Positional Arguments
--------------------
Expand All @@ -144,3 +144,5 @@ Optional Arguments
Query for matching albums instead of tracks.
``-e, --extra``
Query for matching extras instead of tracks.
``-d, --delete``
Delete the items from the filesystem.
13 changes: 13 additions & 0 deletions moe/remove/rm_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import argparse
import logging
import shutil

from sqlalchemy.orm.session import Session

Expand All @@ -29,6 +30,12 @@ def add_command(cmd_parsers: argparse._SubParsersAction):
help="remove music from the library",
parents=[query_parser],
)
rm_parser.add_argument(
"-d",
"--delete",
action="store_true",
help="delete the items from the filesystem",
)
rm_parser.set_defaults(func=_parse_args)


Expand All @@ -46,3 +53,9 @@ def _parse_args(session: Session, args: argparse.Namespace):

for item in items:
moe_rm.remove_item(session, item)

if args.delete:
if item.path.is_file():
item.path.unlink()
else:
shutil.rmtree(item.path)
32 changes: 31 additions & 1 deletion tests/remove/test_rm_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def mock_query() -> Iterator[FunctionType]:
@pytest.fixture
def _tmp_rm_config(tmp_config):
"""A temporary config for the edit plugin with the cli."""
tmp_config('default_plugins = ["cli", "remove"]')
tmp_config('default_plugins = ["cli", "remove", "write"]')


@pytest.mark.usefixtures("_tmp_rm_config")
Expand Down Expand Up @@ -85,6 +85,36 @@ def test_multiple_items(self, mock_query, mock_rm):
mock_rm.assert_any_call(ANY, track)
assert mock_rm.call_count == 2

def test_delete_album(self, mock_query, mock_rm):
"""Delete albums if `-d` passed."""
cli_args = ["remove", "--delete", "*"]
album = album_factory(exists=True)
mock_query.return_value = [album]

moe.cli.main(cli_args)

assert not album.path.exists()

def test_delete_extra(self, mock_query, mock_rm):
"""Delete extras if `-d` passed."""
cli_args = ["remove", "-d", "*"]
extra = extra_factory(exists=True)
mock_query.return_value = [extra]

moe.cli.main(cli_args)

assert not extra.path.exists()

def test_delete_track(self, mock_query, mock_rm):
"""Delete tracks if `-d` passed."""
cli_args = ["remove", "-d", "*"]
track = track_factory(exists=True)
mock_query.return_value = [track]

moe.cli.main(cli_args)

assert not track.path.exists()


class TestPluginRegistration:
"""Test the `plugin_registration` hook implementation."""
Expand Down

0 comments on commit 8246eb8

Please sign in to comment.