Browse Source

symuid-sync: log when adding play count tag

Fabian Peter Hammerle 4 years ago
parent
commit
bff413b86c
2 changed files with 33 additions and 2 deletions
  1. 6 2
      symuid/sync.py
  2. 27 0
      tests/test_sync.py

+ 6 - 2
symuid/sync.py

@@ -8,7 +8,9 @@ import symuid
 from symuid._uuid import generate_uuid4_bytes
 
 
-def _log_path(track_path, msg, stream=sys.stdout):
+def _log_path(track_path, msg, stream=None):
+    if not stream: # pytest capsys
+        stream = sys.stdout
     stream.write("{!r}: {}\n".format(track_path, msg))
 
 
@@ -78,4 +80,6 @@ def _init_argparser():
 def _main():
     argparser = _init_argparser()
     args = argparser.parse_args()
-    sync(**vars(args))
+    sync(**vars(args),
+         play_count_added_cb=lambda track, tag:
+         _log_path(track.path, 'added play count tag {!r}'.format(tag)))

+ 27 - 0
tests/test_sync.py

@@ -133,3 +133,30 @@ def test_sync_play_count_callback(tmpdir, tracks_dir_path):
          play_count_added_cb=play_count_added_cb)
     play_count_added_cb.assert_called_once_with(
         track_a2, ('symuid:pcnt:cmus:lib1:0', ['21']))
+
+
+def test_sync_play_count_callback_main(capsys, tmpdir, tracks_dir_path):
+    shutil.copyfile(
+        src=os.path.join(tracks_dir_path, 'id3v2.4-empty.mp3'),
+        dst=tmpdir.join('a1.mp3'),
+    )
+    shutil.copyfile(
+        src=os.path.join(tracks_dir_path, 'ogg-vorbis-empty.ogg'),
+        dst=tmpdir.join('a2.ogg'),
+    )
+    uuid_a = generate_uuid4_bytes()
+    track_a1 = Track(tmpdir.join('a1.mp3'))
+    track_a1.assign_uuid(uuid_a)
+    track_a2 = Track(tmpdir.join('a2.ogg'))
+    track_a2.assign_uuid(uuid_a)
+    track_a1.register_play_count(PlayCount(
+        player='cmus',
+        library_id='lib1',
+        register_dt=unix_epoch_time_to_datetime_utc(0),
+        count=21,
+    ))
+    with unittest.mock.patch('sys.argv', ['', tmpdir.strpath]):
+        _main()
+    out, _ = capsys.readouterr()
+    assert out == "{!r}: added play count tag ('symuid:pcnt:cmus:lib1:0', ['21'])\n" \
+        .format(track_a2.path)