Skip to content

Commit 2396972

Browse files
authored
Fix track number 0 not displaying in chat announcements (#1205)
1 parent e9202c9 commit 2396972

File tree

7 files changed

+79
-13
lines changed

7 files changed

+79
-13
lines changed

nowplaying/kick/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ async def run_chat(self, oauth_handler: nowplaying.kick.oauth2.KickOAuth2) -> No
239239
@staticmethod
240240
def _finalize(variable: Any) -> str:
241241
''' helper routine to avoid NoneType exceptions '''
242-
if variable:
242+
if variable is not None:
243243
return variable
244244
return ''
245245

nowplaying/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def _got_tag(self, tag: object) -> None:
582582
'composer', 'disc', 'disc_total', 'duration', 'genre', 'lang', 'publisher', 'title',
583583
'track', 'track_total', 'label'
584584
]:
585-
if key not in self.metadata and hasattr(tag, key) and getattr(tag, key):
585+
if key not in self.metadata and hasattr(tag, key) and getattr(tag, key) is not None:
586586
self.metadata[key] = str(getattr(tag, key))
587587

588588
# Handle the 'key' field separately to decode JSON if needed

nowplaying/twitch/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ async def handle_request(self, command, params, username): # pylint: disable=un
324324
@staticmethod
325325
def _finalize(variable):
326326
''' helper routine to avoid NoneType exceptions '''
327-
if variable:
327+
if variable is not None:
328328
return variable
329329
return ''
330330

nowplaying/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __init__(self, filename=None):
116116
@staticmethod
117117
def _finalize(variable):
118118
''' helper routine to avoid NoneType exceptions '''
119-
if variable:
119+
if variable is not None:
120120
return variable
121121
return ''
122122

tests/templates/tracktest.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% if track %}Track: {{ track }}{% endif %}
2+
{% if disc %}Disc: {{ disc }}{% endif %}

tests/test_metadata.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import logging
66
import multiprocessing
77
import sqlite3
8+
import types
89

910
import pytest
1011
import pytest_asyncio
1112

1213
import nowplaying.bootstrap # pylint: disable=import-error
1314
import nowplaying.metadata # pylint: disable=import-error
1415
import nowplaying.upgrade # pylint: disable=import-error
15-
import nowplaying.imagecache # pylint: disable=import-error
16+
import nowplaying.imagecache # pylint: disable=import-error,no-member
1617

1718

1819
@pytest_asyncio.fixture
@@ -24,7 +25,7 @@ async def get_imagecache(bootstrap):
2425
dbdir.mkdir()
2526
logpath = config.testdir.joinpath('debug.log')
2627
stopevent = multiprocessing.Event()
27-
imagecache = nowplaying.imagecache.ImageCache(cachedir=dbdir, stopevent=stopevent)
28+
imagecache = nowplaying.imagecache.ImageCache(cachedir=dbdir, stopevent=stopevent) # pylint: disable=no-member
2829
icprocess = multiprocessing.Process(target=imagecache.queue_process,
2930
name='ICProcess',
3031
args=(
@@ -767,3 +768,27 @@ def test_decode_musical_key(input_value, expected_output):
767768
"""Test the _decode_musical_key method handles various key formats."""
768769
result = nowplaying.metadata.TinyTagRunner._decode_musical_key(input_value) # pylint: disable=protected-access
769770
assert result == expected_output
771+
772+
773+
@pytest.mark.parametrize(
774+
"test_value,expected_result",
775+
[
776+
(0, True), # 0 should be processed (our fix)
777+
(1, True), # 1 should be processed (normal case)
778+
(None, False), # None should be ignored
779+
('0', True), # String '0' should be processed
780+
('', True), # Empty string should be processed (only None is filtered)
781+
])
782+
def test_metadata_handles_zero_values(test_value, expected_result):
783+
"""Test that metadata processing handles 0 values correctly."""
784+
# Create a minimal mock tag object that focuses on testing the specific condition
785+
mock_tag = types.SimpleNamespace(track=test_value)
786+
787+
# Test the specific condition that was problematic
788+
has_attr = hasattr(mock_tag, 'track')
789+
value_check = getattr(mock_tag, 'track') is not None # Our fix: was getattr(mock_tag, 'track')
790+
791+
should_process = has_attr and value_check
792+
793+
assert should_process == expected_result, \
794+
f"Value {test_value} should {'be processed' if expected_result else 'be ignored'}"

tests/test_templatehandler.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
import nowplaying.utils # pylint: disable=import-error
10-
import nowplaying.textoutput # pylint: disable=import-error
10+
import nowplaying.textoutput # pylint: disable=import-error,no-member
1111

1212

1313
@pytest.fixture
@@ -33,7 +33,7 @@ def test_writingmeta(gettemplatehandler): # pylint: disable=redefined-outer-nam
3333
'title': 'this is the title',
3434
}
3535

36-
nowplaying.textoutput.writetxttrack(filename=filename,
36+
nowplaying.textoutput.writetxttrack(filename=filename, # pylint: disable=no-member
3737
templatehandler=gettemplatehandler,
3838
metadata=metadata)
3939
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
@@ -51,7 +51,7 @@ def test_missingmeta(gettemplatehandler): # pylint: disable=redefined-outer-nam
5151

5252
metadata = {}
5353

54-
nowplaying.textoutput.writetxttrack(filename=filename,
54+
nowplaying.textoutput.writetxttrack(filename=filename, # pylint: disable=no-member
5555
templatehandler=gettemplatehandler,
5656
metadata=metadata)
5757
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
@@ -71,7 +71,7 @@ def test_missingtemplate(gettemplatehandler): # pylint: disable=redefined-outer
7171
'title': 'this is the title',
7272
}
7373

74-
nowplaying.textoutput.writetxttrack(filename=filename,
74+
nowplaying.textoutput.writetxttrack(filename=filename, # pylint: disable=no-member
7575
templatehandler=gettemplatehandler,
7676
metadata=metadata)
7777
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
@@ -90,7 +90,7 @@ def test_missingfilename(gettemplatehandler): # pylint: disable=redefined-outer
9090
'title': 'this is the title',
9191
}
9292

93-
nowplaying.textoutput.writetxttrack(filename=filename,
93+
nowplaying.textoutput.writetxttrack(filename=filename, # pylint: disable=no-member
9494
templatehandler=gettemplatehandler,
9595
metadata=metadata)
9696
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
@@ -103,7 +103,7 @@ def test_cleartemplate(): # pylint: disable=redefined-outer-name
103103
''' try writing a text '''
104104
with tempfile.TemporaryDirectory() as newpath:
105105
filename = os.path.join(newpath, 'test.txt')
106-
nowplaying.textoutput.writetxttrack(filename=filename, clear=True)
106+
nowplaying.textoutput.writetxttrack(filename=filename, clear=True) # pylint: disable=no-member
107107
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
108108
content = tempfn.readlines()
109109

@@ -114,8 +114,47 @@ def test_justafile(): # pylint: disable=redefined-outer-name
114114
''' try writing a text '''
115115
with tempfile.TemporaryDirectory() as newpath:
116116
filename = os.path.join(newpath, 'test.txt')
117-
nowplaying.textoutput.writetxttrack(filename=filename)
117+
nowplaying.textoutput.writetxttrack(filename=filename) # pylint: disable=no-member
118118
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
119119
content = tempfn.readlines()
120120

121121
assert content[0] == '{{ artist }} - {{ title }}'
122+
123+
124+
@pytest.mark.templatesettings(template='tracktest.txt')
125+
@pytest.mark.parametrize(
126+
"track_value,disc_value,expected_track,expected_disc",
127+
[
128+
('0', '0', True, True), # Track 0 and disc 0 should show
129+
('1', '1', True, True), # Track 1 and disc 1 should show
130+
('', '', False, False), # Empty strings should not show
131+
(None, None, False, False), # None values should not show
132+
])
133+
def test_track_disc_handling( # pylint: disable=redefined-outer-name
134+
gettemplatehandler, track_value, disc_value, expected_track,
135+
expected_disc):
136+
''' test track and disc number handling with various values '''
137+
with tempfile.TemporaryDirectory() as newpath:
138+
filename = os.path.join(newpath, 'test.txt')
139+
140+
metadata = {}
141+
if track_value is not None:
142+
metadata['track'] = track_value
143+
if disc_value is not None:
144+
metadata['disc'] = disc_value
145+
146+
nowplaying.textoutput.writetxttrack(filename=filename, # pylint: disable=no-member
147+
templatehandler=gettemplatehandler,
148+
metadata=metadata)
149+
with open(filename) as tempfn: # pylint: disable=unspecified-encoding
150+
content = tempfn.read()
151+
152+
if expected_track:
153+
assert f'Track: {track_value}' in content
154+
else:
155+
assert 'Track:' not in content
156+
157+
if expected_disc:
158+
assert f'Disc: {disc_value}' in content
159+
else:
160+
assert 'Disc:' not in content

0 commit comments

Comments
 (0)