Skip to content

Commit 95747f1

Browse files
committed
Address Pike's feedback
1 parent e38a550 commit 95747f1

File tree

8 files changed

+79
-48
lines changed

8 files changed

+79
-48
lines changed

fluent/migrate/context.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ def in_changeset(ident):
256256
self, reference, current, transforms, in_changeset
257257
)
258258

259-
# If none of the transforms is in the given changeset, the merged
260-
# snapshot is identical to the current translation. We compare
261-
# JSON trees rather then use filtering by `in_changeset` to account
262-
# for translations removed from `reference`.
259+
# Skip this path if the merged snapshot is identical to the current
260+
# state of the localization file. This may happen when:
261+
#
262+
# - none of the transforms is in the changset, or
263+
# - all messages which would be migrated by the context's
264+
# transforms already exist in the current state.
265+
#
266+
# We compare JSON trees rather then use filtering by `in_changeset`
267+
# to account for translations removed from `reference`.
263268
if snapshot.to_json() == current.to_json():
264269
continue
265270

fluent/migrate/merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def merge_resource(ctx, reference, current, transforms, in_changeset):
1212
1313
Use the `reference` FTL AST as a template. For each en-US string in the
1414
reference, first check for an existing translation in the current FTL
15-
`localization` and return early if it's present; then if the string has
15+
`localization` and use it if it's present; then if the string has
1616
a transform defined in the migration specification and if it's in the
1717
currently processed changeset, evaluate the transform.
1818
"""

tests/migrate/fixtures/pl/existing.ftl

Whitespace-only changes.

tests/migrate/test_context.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,24 @@ def test_existing_target_ftl_existing_string(self):
397397
expected
398398
)
399399

400+
def test_existing_target_ftl_with_all_messages(self):
401+
self.ctx.add_transforms('privacy.ftl', 'privacy.ftl', [
402+
FTL.Message(
403+
id=FTL.Identifier('dnt-description'),
404+
value=COPY(
405+
'privacy.dtd',
406+
'doNotTrack.description'
407+
)
408+
),
409+
])
410+
411+
# All migrated messages are already in the target FTL and the result of
412+
# merge_changeset is an empty iterator.
413+
self.assertDictEqual(
414+
to_json(self.ctx.merge_changeset()),
415+
{}
416+
)
417+
400418

401419
class TestNotSupportedError(unittest.TestCase):
402420
def test_add_ftl(self):

tests/migrate/test_copy.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@
99
except ImportError:
1010
PropertiesParser = DTDParser = None
1111

12-
from fluent.migrate.errors import NotSupportedError
1312
from fluent.migrate.util import parse, ftl_message_to_json
1413
from fluent.migrate.transforms import evaluate, COPY
1514

1615

17-
class TestNotSupportedError(unittest.TestCase):
18-
def test_fluent(self):
19-
pattern = ('Migrating translations from Fluent files is not supported')
20-
with self.assertRaisesRegexp(NotSupportedError, pattern):
21-
FTL.Message(
22-
FTL.Identifier('foo'),
23-
value=COPY('test.ftl', 'foo')
24-
)
25-
26-
2716
class MockContext(unittest.TestCase):
2817
def get_source(self, path, key):
2918
# Ignore path (test.properties) and get translations from self.strings

tests/migrate/test_plural.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,11 @@
99
except ImportError:
1010
PropertiesParser = None
1111

12-
from fluent.migrate.errors import NotSupportedError
1312
from fluent.migrate.util import parse, ftl_message_to_json
1413
from fluent.migrate.helpers import EXTERNAL_ARGUMENT
1514
from fluent.migrate.transforms import evaluate, PLURALS, REPLACE_IN_TEXT
1615

1716

18-
class TestNotSupportedError(unittest.TestCase):
19-
def test_fluent(self):
20-
pattern = ('Migrating translations from Fluent files is not supported')
21-
with self.assertRaisesRegexp(NotSupportedError, pattern):
22-
FTL.Message(
23-
FTL.Identifier('delete-all'),
24-
value=PLURALS(
25-
'test.ftl',
26-
'deleteAll',
27-
EXTERNAL_ARGUMENT('num')
28-
)
29-
)
30-
31-
3217
class MockContext(unittest.TestCase):
3318
# Static categories corresponding to en-US.
3419
plural_categories = ('one', 'other')

tests/migrate/test_replace.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,11 @@
99
except ImportError:
1010
PropertiesParser = None
1111

12-
from fluent.migrate.errors import NotSupportedError
1312
from fluent.migrate.util import parse, ftl_message_to_json
1413
from fluent.migrate.helpers import EXTERNAL_ARGUMENT
1514
from fluent.migrate.transforms import evaluate, REPLACE
1615

1716

18-
class TestNotSupportedError(unittest.TestCase):
19-
def test_fluent(self):
20-
pattern = ('Migrating translations from Fluent files is not supported')
21-
with self.assertRaisesRegexp(NotSupportedError, pattern):
22-
FTL.Message(
23-
FTL.Identifier(u'hello'),
24-
value=REPLACE(
25-
'test.ftl',
26-
'hello',
27-
{
28-
'#1': EXTERNAL_ARGUMENT('username')
29-
}
30-
)
31-
)
32-
33-
3417
class MockContext(unittest.TestCase):
3518
def get_source(self, path, key):
3619
# Ignore path (test.properties) and get translations from self.strings

tests/migrate/test_source.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# coding=utf8
2+
from __future__ import unicode_literals
3+
4+
import unittest
5+
6+
import fluent.syntax.ast as FTL
7+
8+
from fluent.migrate.errors import NotSupportedError
9+
from fluent.migrate.transforms import Source, COPY, PLURALS, REPLACE
10+
from fluent.migrate.helpers import EXTERNAL_ARGUMENT
11+
12+
13+
class TestNotSupportedError(unittest.TestCase):
14+
def test_source(self):
15+
pattern = ('Migrating translations from Fluent files is not supported')
16+
with self.assertRaisesRegexp(NotSupportedError, pattern):
17+
Source('test.ftl', 'foo')
18+
19+
def test_copy(self):
20+
pattern = ('Migrating translations from Fluent files is not supported')
21+
with self.assertRaisesRegexp(NotSupportedError, pattern):
22+
FTL.Message(
23+
FTL.Identifier('foo'),
24+
value=COPY('test.ftl', 'foo')
25+
)
26+
27+
def test_plurals(self):
28+
pattern = ('Migrating translations from Fluent files is not supported')
29+
with self.assertRaisesRegexp(NotSupportedError, pattern):
30+
FTL.Message(
31+
FTL.Identifier('delete-all'),
32+
value=PLURALS(
33+
'test.ftl',
34+
'deleteAll',
35+
EXTERNAL_ARGUMENT('num')
36+
)
37+
)
38+
39+
def test_replace(self):
40+
pattern = ('Migrating translations from Fluent files is not supported')
41+
with self.assertRaisesRegexp(NotSupportedError, pattern):
42+
FTL.Message(
43+
FTL.Identifier(u'hello'),
44+
value=REPLACE(
45+
'test.ftl',
46+
'hello',
47+
{
48+
'#1': EXTERNAL_ARGUMENT('username')
49+
}
50+
)
51+
)

0 commit comments

Comments
 (0)