refactor: use pathlib for all path handling and normalize index paths

This commit is contained in:
randogoth 2026-10-24 21:50:43 +03:00
parent f18d0300b6
commit a9a44ccb7e
3 changed files with 112 additions and 141 deletions

View file

@ -28,7 +28,7 @@ def test_and_search_within_one_category(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi', '#snare')
o.add('b.mp3', '#lofi')
assert found(o, '#lofi', '#snare') == ['./a.mp3']
assert found(o, '#lofi', '#snare') == ['a.mp3']
def test_and_search_across_categories(tree):
@ -36,7 +36,7 @@ def test_and_search_across_categories(tree):
o.add('a.mp3', '#lofi', '@drums')
o.add('b.mp3', '#lofi')
o.add('c.mp3', '@drums')
assert found(o, '#lofi', '@drums') == ['./a.mp3']
assert found(o, '#lofi', '@drums') == ['a.mp3']
def test_or_search(tree):
@ -44,7 +44,7 @@ def test_or_search(tree):
o.add('a.mp3', '#lofi')
o.add('b.mp3', '#snare')
o.add('c.mp3', '#other')
assert found(o, '#lofi', '#snare', either=True) == ['./a.mp3', './b.mp3']
assert found(o, '#lofi', '#snare', either=True) == ['a.mp3', 'b.mp3']
def test_unprefixed_term_matches_any_category(tree):
@ -52,14 +52,14 @@ def test_unprefixed_term_matches_any_category(tree):
o.add('a.mp3', '+lofi')
o.add('b.mp3', '#lofi')
o.add('c.mp3', '#other')
assert found(o, 'lofi') == ['./a.mp3', './b.mp3']
assert found(o, 'lofi') == ['a.mp3', 'b.mp3']
def test_empty_query_lists_everything(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.add('sub/d.txt', '#doc')
assert found(o, None) == ['./a.mp3', './sub/d.txt']
assert found(o, None) == ['a.mp3', 'sub/d.txt']
def test_multi_word_note(tree):
@ -92,7 +92,7 @@ def test_index_is_rebuilt_per_list_call(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.add('sub/d.txt', '#lofi')
assert found(o, '#lofi') == ['./a.mp3', './sub/d.txt']
assert found(o, '#lofi') == ['a.mp3', 'sub/d.txt']
assert found(o, '#lofi', path='sub') == ['sub/d.txt']
@ -176,8 +176,8 @@ def test_file_names_with_spaces_and_separator_in_note(tree):
o = OrvaniTag()
o.add('My Track.flac', '#boombap', 'a', '---', 'b')
assert lines(tree) == ['My Track.flac --- #boombap a --- b']
assert found(o, '#boombap') == ['./My Track.flac']
assert o.index('')['items']['./My Track.flac']['note'] == 'a --- b'
assert found(o, '#boombap') == ['My Track.flac']
assert o.index('')['items']['My Track.flac']['note'] == 'a --- b'
def test_malformed_line_raises_and_keeps_file(tree):
@ -216,13 +216,13 @@ def test_remove_saves_once(tree, monkeypatch):
def test_cache_is_rebuilt_when_context_changes(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
assert o.list('', True, False, True, True, False, '#lofi')['found'] == ['./a.mp3']
assert o.list('', True, False, True, True, False, '#lofi')['found'] == ['a.mp3']
assert (tree / orvani.CACHE_FILE).exists()
o.add('b.mp3', '#lofi')
stat = (tree / orvani.CONTEXT_FILE).stat()
os.utime(tree / orvani.CONTEXT_FILE, ns=(stat.st_atime_ns, stat.st_mtime_ns + 10**9))
assert OrvaniTag().list('', True, False, True, True, False, '#lofi')['found'] == ['./a.mp3', './b.mp3']
assert OrvaniTag().list('', True, False, True, True, False, '#lofi')['found'] == ['a.mp3', 'b.mp3']
def test_import_untagged_files(tree):
@ -230,6 +230,7 @@ def test_import_untagged_files(tree):
o.add('a.mp3', '#lofi', 'a note')
o.add('sub/d.txt', '@docs')
o.export('context.txt')
assert (tree / 'context.txt').read_text() == 'a.mp3 --- #lofi a note\nsub/d.txt --- @docs\n'
(tree / orvani.CONTEXT_FILE).unlink()
(tree / 'sub' / orvani.CONTEXT_FILE).unlink()
@ -284,3 +285,12 @@ def test_errors_of_remove_delete_export_and_import(tree):
o.delete('a.mp3')
with pytest.raises(NoEntry):
o.export('context.txt')
def test_clean_with_a_file_path_cleans_its_folder(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.add('b.mp3', '#gone')
(tree / 'b.mp3').unlink()
assert o.clean('a.mp3', False) == 1
assert lines(tree) == ['a.mp3 --- #lofi']