Skip to content

Commit

Permalink
fix: ensure tracks created from files contain required tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpavlock committed Nov 6, 2022
1 parent 72a9d52 commit bf215ed
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
44 changes: 34 additions & 10 deletions moe/library/track.py
Expand Up @@ -120,7 +120,6 @@ def read_custom_tags(
album_fields["label"] = audio_file.label
album_fields["media"] = audio_file.media
album_fields["original_date"] = audio_file.original_date
album_fields["path"] = track_path.parent
album_fields["title"] = audio_file.album
album_fields["track_total"] = audio_file.tracktotal

Expand All @@ -130,7 +129,6 @@ def read_custom_tags(
track_fields["disc"] = audio_file.disc
if audio_file.genres is not None:
track_fields["genres"] = set(audio_file.genres)
track_fields["path"] = track_path
track_fields["title"] = audio_file.title
track_fields["track_num"] = audio_file.track

Expand Down Expand Up @@ -431,6 +429,7 @@ def from_file(cls: type[T], track_path: Path, album: Optional[Album] = None) ->
Raises:
TrackError: Given ``path`` does not correspond to a track file.
ValueError: Track is missing required fields.
"""
log.debug(f"Creating track from path. [path={track_path}, {album=}]")

Expand All @@ -447,21 +446,46 @@ def from_file(cls: type[T], track_path: Path, album: Optional[Album] = None) ->
config.CONFIG.pm.hook.read_custom_tags(
track_path=track_path, album_fields=album_fields, track_fields=track_fields
)

title = track_fields.pop("title")
track_num = track_fields.pop("track_num")

album_artist = album_fields.pop("artist")
album_title = album_fields.pop("title")
date = album_fields.pop("date")
disc_total = album_fields.pop("disc_total") or 1

missing_reqd_fields = []
if not title:
missing_reqd_fields.append("title")
if not track_num:
missing_reqd_fields.append("track_num")
if not album_artist and not album:
missing_reqd_fields.append("album_artist")
if not album_title and not album:
missing_reqd_fields.append("album_title")
if not date and not album:
missing_reqd_fields.append("date")
if missing_reqd_fields:
raise ValueError(
f"Track is missing required fields. [{missing_reqd_fields=!r}]"
)

if not album:
album = Album(
path=album_fields.pop("path"),
artist=album_fields.pop("artist"),
title=album_fields.pop("title"),
date=album_fields.pop("date"),
disc_total=album_fields.pop("disc_total"),
path=track_path.parent,
artist=album_artist,
title=album_title,
date=date,
disc_total=disc_total,
**album_fields,
)

return cls(
album=album,
path=track_fields.pop("path"),
title=track_fields.pop("title"),
track_num=track_fields.pop("track_num"),
path=track_path,
title=title,
track_num=track_num,
**track_fields,
)

Expand Down
60 changes: 60 additions & 0 deletions tests/library/test_track.py
Expand Up @@ -174,6 +174,66 @@ def test_read_track_fields(self, tmp_config):

assert new_track.title == "custom track title"

def test_missing_artist(self, tmp_config):
"""Raise ValueError if track is missing both an artist and albumartist."""
tmp_config()
track = track_factory(exists=True)
track.album_obj.artist = None
track.artist = None
moe_write.write_tags(track)

with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)

def test_missing_album_title(self, tmp_config):
"""Raise ValueError if track is missing the album title."""
tmp_config()
track = track_factory(exists=True)
track.album_obj.title = None
moe_write.write_tags(track)

with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)

def test_missing_date(self, tmp_config):
"""Raise ValueError if track is missing the date."""
tmp_config()
track = track_factory(exists=True)
track.album_obj.date = None
moe_write.write_tags(track)

with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)

def test_missing_title(self, tmp_config):
"""Raise ValueError if track is missing the title."""
tmp_config()
track = track_factory(exists=True)
track.title = None
moe_write.write_tags(track)

with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)

def test_missing_track_num(self, tmp_config):
"""Raise ValueError if track is missing the track number."""
tmp_config()
track = track_factory(exists=True)
track.track_num = None
moe_write.write_tags(track)

with pytest.raises(ValueError, match="missing"):
Track.from_file(track.path)

def test_missing_disc_total(self, tmp_config):
"""Use the default disc total if missing from tags."""
tmp_config()
track = track_factory(exists=True)
track.album_obj.disc_total = None
moe_write.write_tags(track)

Track.from_file(track.path).album_obj.disc_total = 1


class TestEquality:
"""Test equality of tracks."""
Expand Down

0 comments on commit bf215ed

Please sign in to comment.