refactor: rebuild the CLI with argparse subcommands and one handler per command

This commit is contained in:
randogoth 2026-10-24 21:59:26 +03:00
parent d879c6f871
commit e4fc6c0a59
3 changed files with 211 additions and 142 deletions

59
tests/test_cmd.py Normal file
View file

@ -0,0 +1,59 @@
import sys
import pytest
import orvanitag.orvani as orvani
from orvanitag.cmd import main
@pytest.fixture
def otag(tmp_path, monkeypatch, capsys):
monkeypatch.chdir(tmp_path)
def run(*argv):
monkeypatch.setattr(sys, 'argv', ['otag', *argv])
main()
return capsys.readouterr().out
return run
def test_no_command_prints_help(otag):
out = otag()
for command in ['add', 'remove', 'list', 'copy', 'move', 'del', 'clean', 'purge', 'scan', 'write', 'export', 'import']:
assert command in out
def test_each_command_has_its_own_help(otag):
with pytest.raises(SystemExit) as exit:
otag('ls', '-h')
assert exit.value.code == 0
def test_copy_move_and_delete_with_file(otag, tmp_path):
(tmp_path / 'a.mp3').touch()
(tmp_path / 'sub').mkdir()
otag('a', 'a.mp3', '#lofi')
otag('cp', 'a.mp3', 'b.mp3')
otag('mv', '-f', 'a.mp3', 'sub')
assert (tmp_path / 'sub' / 'a.mp3').exists() and not (tmp_path / 'a.mp3').exists()
assert (tmp_path / orvani.CONTEXT_FILE).read_text() == 'b.mp3 --- #lofi\n'
otag('rm', '--with-file', 'sub/a.mp3')
assert not (tmp_path / 'sub' / 'a.mp3').exists()
assert not (tmp_path / 'sub' / orvani.CONTEXT_FILE).exists()
def test_add_needs_tags(otag, tmp_path):
(tmp_path / 'a.mp3').touch()
with pytest.raises(SystemExit) as exit:
otag('a', 'a.mp3')
assert exit.value.code == 2
def test_purge_keeps_config(otag, tmp_path):
(tmp_path / 'a.mp3').touch()
(tmp_path / orvani.CONFIG_FILE).write_text('plugins = []\n')
otag('a', 'a.mp3', '#lofi')
otag('p')
assert not (tmp_path / orvani.CONTEXT_FILE).exists()
assert (tmp_path / orvani.CONFIG_FILE).exists()