Skip to content

Commit b552da4

Browse files
committed
Compare Terms and Messages in migrations, fixes bug 1437833
When adding Terms, we didn't add them to the list of entries to compare in MergeContext.messages_equal. When migrating to fluent files where only Terms differ from changeset to changeset, that lead to no migration being created for that changeset.
1 parent ed3d69e commit b552da4

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

fluent/migrate/context.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,25 @@ def get_source(self, path, key):
254254
return resource.get(key, None)
255255

256256
def messages_equal(self, res1, res2):
257-
"""Compare messages of two FTL resources.
257+
"""Compare messages and terms of two FTL resources.
258258
259-
Uses FTL.BaseNode.equals to compare all messages in two FTL resources.
259+
Uses FTL.BaseNode.equals to compare all messages/terms
260+
in two FTL resources.
260261
If the order or number of messages differ, the result is also False.
261262
"""
262263
def message_id(message):
263264
"Return the message's identifer name for sorting purposes."
264265
return message.id.name
265266

266267
messages1 = sorted(
267-
(entry for entry in res1.body if isinstance(entry, FTL.Message)),
268+
(entry for entry in res1.body
269+
if isinstance(entry, FTL.Message)
270+
or isinstance(entry, FTL.Term)),
268271
key=message_id)
269272
messages2 = sorted(
270-
(entry for entry in res2.body if isinstance(entry, FTL.Message)),
273+
(entry for entry in res2.body
274+
if isinstance(entry, FTL.Message)
275+
or isinstance(entry, FTL.Term)),
271276
key=message_id)
272277
for msg1, msg2 in zip_longest(messages1, messages2):
273278
if msg1 is None or msg2 is None:

tests/migrate/test_context.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,132 @@ def test_add_ftl(self):
659659
)
660660

661661
ctx.maybe_add_localization('privacy.ftl')
662+
663+
664+
class TestMessagesEqual(unittest.TestCase):
665+
maxDiff = None
666+
667+
def setUp(self):
668+
# Silence all logging.
669+
logging.disable(logging.CRITICAL)
670+
671+
self.ctx = MergeContext(
672+
lang='pl',
673+
reference_dir=here('fixtures/en-US'),
674+
localization_dir=here('fixtures/pl')
675+
)
676+
677+
def tearDown(self):
678+
# Resume logging.
679+
logging.disable(logging.NOTSET)
680+
681+
def test_messages_equal(self):
682+
first = FTL.Resource([
683+
FTL.Message(
684+
id=FTL.Identifier('bar'),
685+
value=FTL.Pattern([
686+
FTL.TextElement('Hardcoded Value')
687+
])
688+
),
689+
])
690+
second = FTL.Resource([
691+
FTL.Message(
692+
id=FTL.Identifier('bar'),
693+
value=FTL.Pattern([
694+
FTL.TextElement('Hardcoded Value')
695+
])
696+
),
697+
])
698+
self.assertTrue(self.ctx.messages_equal(first, second))
699+
700+
def test_messages_different_attributes(self):
701+
first = FTL.Resource([
702+
FTL.Message(
703+
id=FTL.Identifier('bar'),
704+
value=FTL.Pattern([
705+
FTL.TextElement('Hardcoded Value')
706+
])
707+
),
708+
])
709+
second = FTL.Resource([
710+
FTL.Message(
711+
id=FTL.Identifier('bar'),
712+
value=FTL.Pattern([
713+
FTL.TextElement('Hardcoded Value')
714+
]),
715+
attributes=[
716+
FTL.Attribute(
717+
FTL.Identifier('one'),
718+
value=FTL.Pattern([
719+
FTL.TextElement('Attribute Value')
720+
])
721+
),
722+
]
723+
),
724+
])
725+
self.assertFalse(self.ctx.messages_equal(first, second))
726+
727+
def test_terms_equal(self):
728+
first = FTL.Resource([
729+
FTL.Term(
730+
id=FTL.Identifier('-bar'),
731+
value=FTL.Pattern([
732+
FTL.TextElement('Hardcoded Value')
733+
])
734+
),
735+
])
736+
second = FTL.Resource([
737+
FTL.Term(
738+
id=FTL.Identifier('-bar'),
739+
value=FTL.Pattern([
740+
FTL.TextElement('Hardcoded Value')
741+
])
742+
),
743+
])
744+
self.assertTrue(self.ctx.messages_equal(first, second))
745+
746+
def test_terms_different_attributes(self):
747+
first = FTL.Resource([
748+
FTL.Term(
749+
id=FTL.Identifier('-bar'),
750+
value=FTL.Pattern([
751+
FTL.TextElement('Hardcoded Value')
752+
])
753+
),
754+
])
755+
second = FTL.Resource([
756+
FTL.Term(
757+
id=FTL.Identifier('-bar'),
758+
value=FTL.Pattern([
759+
FTL.TextElement('Hardcoded Value')
760+
]),
761+
attributes=[
762+
FTL.Attribute(
763+
FTL.Identifier('one'),
764+
value=FTL.Pattern([
765+
FTL.TextElement('Attribute Value')
766+
])
767+
),
768+
]
769+
),
770+
])
771+
self.assertFalse(self.ctx.messages_equal(first, second))
772+
773+
def test_term_and_message(self):
774+
first = FTL.Resource([
775+
FTL.Term(
776+
id=FTL.Identifier('-bar'),
777+
value=FTL.Pattern([
778+
FTL.TextElement('Hardcoded Value')
779+
])
780+
),
781+
])
782+
second = FTL.Resource([
783+
FTL.Message(
784+
id=FTL.Identifier('bar'),
785+
value=FTL.Pattern([
786+
FTL.TextElement('Hardcoded Value')
787+
])
788+
),
789+
])
790+
self.assertFalse(self.ctx.messages_equal(first, second))

0 commit comments

Comments
 (0)