Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: 94e227f0 by wurstsalat at 2023-02-14T21:45:26+01:00 fix: GetRelativeTimeTest: Fix comparison of translated strings and off-by timezone errors
- - - - - 0618ab18 by wurstsalat at 2023-02-14T21:45:26+01:00 cfix: Settings values: Make additional_uri_schemes a string setting
- - - - - 1526a6db by wurstsalat at 2023-02-14T21:45:26+01:00 imprv: Tests: Use in-memory settings database
- - - - - c1225720 by wurstsalat at 2023-02-14T21:45:26+01:00 imprv: Improve check for message highlight and add tests
- - - - -
6 changed files:
- gajim/common/helpers.py - gajim/common/setting_values.py - test/common/__init__.py - test/common/test_get_uf_relative_time.py - + test/common/test_message_needs_highlight.py - test/common/test_styling.py
Changes:
===================================== gajim/common/helpers.py ===================================== @@ -562,35 +562,44 @@ def get_os_info() -> str:
def message_needs_highlight(text: str, nickname: str, own_jid: str) -> bool: ''' - Check text to see whether any of the words in (muc_highlight_words and - nick) appear + Check whether 'text' contains 'nickname', 'own_jid', or any string of the + 'muc_highlight_words' setting. '''
- uri = parse_uri(text) - if uri.type != URIType.INVALID: - # Don't highlight message if it's an URI - return False + search_strings = app.settings.get('muc_highlight_words').split(';') + search_strings.append(nickname) + search_strings.append(own_jid)
- special_words = app.settings.get('muc_highlight_words').split(';') - special_words.append(nickname) - special_words.append(own_jid) - # Strip empties: ''.split(';') == [''] and would highlight everything. - # Also lowercase everything for case insensitive compare. - special_words = [word.lower() for word in special_words if word] + search_strings = [word.lower() for word in search_strings if word] text = text.lower()
- for special_word in special_words: - found_here = text.find(special_word) - while found_here > -1: - end_here = found_here + len(special_word) - if ((found_here == 0 or not text[found_here - 1].isalpha()) and - (end_here == len(text) or not text[end_here].isalpha())): - # It is beginning of text or char before is not alpha AND - # it is end of text or char after is not alpha + for search_string in search_strings: + match = text.find(search_string) + + while match > -1: + search_end = match + len(search_string) + + if match == 0 and search_end == len(text): + # Text contains search_string only (exact match) + return True + + char_before_allowed = bool( + not text[match - 1].isalpha() and + text[match - 1] not in ('/', '-')) + + if char_before_allowed and search_end == len(text): + # search_string found at the end of text and + # char before search_string is allowed. + return True + + if char_before_allowed and not text[search_end].isalpha(): + # char_before search_string is allowed and + # char_after search_string is not alpha. return True - # continue searching - start = found_here + 1 - found_here = text.find(special_word, start) + + start = match + 1 + match = text.find(search_string, start) + return False
===================================== gajim/common/setting_values.py ===================================== @@ -36,7 +36,6 @@ class _ACCOUNTDEFAULT:
BoolSettings = Literal[ - 'additional_uri_schemes', 'allow_open_file_uris', 'always_english_wikipedia', 'always_english_wiktionary', @@ -129,6 +128,7 @@ class _ACCOUNTDEFAULT:
StringSettings = Literal[ 'action_on_close', + 'additional_uri_schemes', 'audio_input_device', 'audio_output_device', 'autoaway_message',
===================================== test/common/__init__.py ===================================== @@ -10,5 +10,10 @@ def require_versions(): 'GObject': '2.0', 'Pango': '1.0'})
- require_versions() + +from gajim.common import app +from gajim.common.settings import Settings + +app.settings = Settings(in_memory=True) +app.settings.init()
===================================== test/common/test_get_uf_relative_time.py ===================================== @@ -6,9 +6,10 @@
from gajim.common import app from gajim.common.helpers import get_uf_relative_time -from gajim.common.settings import Settings +from gajim.common.i18n import _ +from gajim.common.i18n import ngettext
-app.settings = Settings(in_memory=True) +local_timezone = datetime.now(timezone.utc).astimezone().tzinfo
class GetRelativeTimeTest(unittest.TestCase): @@ -16,30 +17,41 @@ class GetRelativeTimeTest(unittest.TestCase):
def test_sub_1_minute(self): '''Test timedelta less than 1 minute''' - timenow = datetime(2023, 1, 2, 3, 4, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 2, 3, 4, 0, tzinfo=local_timezone) timestamp1 = timenow - timedelta(seconds=30) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), - timenow.timestamp()), 'Just now') + timenow.timestamp()), + _('Just now'))
def test_sub_15_minutes(self): '''Test timedelta less than 15 minutes and more than 1 minute ago''' - timenow = datetime(2023, 1, 2, 3, 4, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 2, 3, 4, 0, tzinfo=local_timezone) timestamp1 = timenow - timedelta(minutes=3) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), - '3 mins ago') + ngettext( + '%i min ago', + '%i mins ago', + 3, + 3, + 3))
def test_sub_15_minutes_next_day(self): '''Test timedelta less than 15 minutes and it is the next day''' - timenow = datetime(2023, 1, 1, 0, 5, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 1, 0, 5, 0, tzinfo=local_timezone) timestamp1 = timenow - timedelta(minutes=10) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), - '10 mins ago') + ngettext( + '%i min ago', + '%i mins ago', + 10, + 10, + 10))
def test_today(self): '''Test today: same day and more than 15 minutes ago''' - timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=local_timezone) timestamp1 = timenow - timedelta(hours=4) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), @@ -47,40 +59,41 @@ def test_today(self):
def test_yesterday_less_than_24h(self): '''Test yesterday and less than 24h ago''' - timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=timezone.utc) - timestamp1 = datetime(2023, 1, 1, 14, 0, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=local_timezone) + timestamp1 = datetime(2023, 1, 1, 14, 0, 0, tzinfo=local_timezone) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), - timenow.timestamp()), 'Yesterday') + timenow.timestamp()), + _('Yesterday'))
def test_yesterday_more_than_24h(self): '''Test yesterday and more than 24h ago''' - timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=timezone.utc) - timestamp1 = datetime(2023, 1, 1, 10, 0, 0, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 2, 12, 0, 0, tzinfo=local_timezone) + timestamp1 = datetime(2023, 1, 1, 10, 0, 0, tzinfo=local_timezone) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), - 'Yesterday') + _('Yesterday'))
def test_weekday(self): '''Test weekday: timestamp older than yesterday and less than 7 days ago, should return the weekday, i.e. 'Sun' for Sunday''' - timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=timezone.utc) - timestamp1 = datetime(2023, 1, 1, 4, 5, 6, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=local_timezone) + timestamp1 = datetime(2023, 1, 1, 4, 5, 6, tzinfo=local_timezone) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), timestamp1.strftime('%a'))
def test_month_day(self): '''Test month_day: timestamp more than 7 days ago but less than 365''' - timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=timezone.utc) - timestamp1 = datetime(2022, 11, 15, 4, 5, 6, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=local_timezone) + timestamp1 = datetime(2022, 11, 15, 4, 5, 6, tzinfo=local_timezone) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), timestamp1.strftime('%b %d'))
def test_year(self): '''Test year: timestamp more than 365 days ago''' - timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=timezone.utc) - timestamp1 = datetime(2022, 1, 1, 4, 5, 6, tzinfo=timezone.utc) + timenow = datetime(2023, 1, 5, 1, 2, 3, tzinfo=local_timezone) + timestamp1 = datetime(2022, 1, 1, 4, 5, 6, tzinfo=local_timezone) self.assertEqual(get_uf_relative_time(timestamp1.timestamp(), timenow.timestamp()), '2022')
===================================== test/common/test_message_needs_highlight.py ===================================== @@ -0,0 +1,45 @@ +import unittest + +from gajim.common import app +from gajim.common.helpers import message_needs_highlight + +app.settings.set('muc_highlight_words', 'test;gajim') +JID = 'romemo@xmppserver' +NICK = 'Romeo' + + +class HighlightTest(unittest.TestCase): + '''Tests for message_needs_highlight''' + + def test_highlight(self): + t_text1 = 'Romeo: Does this work?' + t_text2 = 'Romeo:Does this work?' + t_text3 = 'Romeo Does this work?' + t_text4 = 'Does this work, romeo?' + t_text5 = 'Does this work,Romeo?' + t_text6 = 'Are you using Gajim?' + t_text7 = 'Did you test this?' + t_text8 = 'Hi romeo' + t_text9 = 'My address is romeo@xmppserver' + + f_text1 = 'RomeoDoes this work?' + f_text2 = '' + f_text3 = 'https://romeo.tld' + f_text4 = 'https://romeo.tld message' + f_text5 = 'https://test.tld/where-is-romeo' + + self.assertTrue(message_needs_highlight(t_text1, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text2, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text3, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text4, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text5, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text6, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text7, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text8, NICK, JID)) + self.assertTrue(message_needs_highlight(t_text9, NICK, JID)) + + self.assertFalse(message_needs_highlight(f_text1, NICK, JID)) + self.assertFalse(message_needs_highlight(f_text2, NICK, JID)) + self.assertFalse(message_needs_highlight(f_text3, NICK, JID)) + self.assertFalse(message_needs_highlight(f_text4, NICK, JID)) + self.assertFalse(message_needs_highlight(f_text5, NICK, JID))
===================================== test/common/test_styling.py ===================================== @@ -1,5 +1,4 @@ import unittest -from unittest.mock import MagicMock
from gajim.common import app # Avoids circular imports from common.helpers from gajim.common import styling @@ -14,9 +13,8 @@ from gajim.common.styling import StrongSpan from gajim.common.util.text import escape_iri_query
-app.settings = MagicMock() -# additional_uri_schemes -app.settings.get = MagicMock(return_value='a a- a. scheme') +app.settings.set('additional_uri_schemes', 'a a- a. scheme') +
STYLING = { 'pre cannot have children': {
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/63a0e85b1e0510a98a1984861ceb6743...