Skip to content

Commit 0766832

Browse files
authored
Add meta and comments for new resources (mozilla#4167)
1 parent 4c7f672 commit 0766832

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

pontoon/sync/core/entities.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,13 @@ def add_resources(
299299
) -> tuple[int, set[str]]:
300300

301301
new_resources = [
302-
Resource(project=project, path=db_path, format=get_res_format(res))
302+
Resource(
303+
project=project,
304+
path=db_path,
305+
format=get_res_format(res),
306+
comment=res.comment,
307+
meta=[[m.key, m.value] for m in res.meta],
308+
)
303309
for db_path, res in updates.items()
304310
if next(res.all_entries(), None) and db_path not in changed_paths
305311
]

pontoon/sync/tests/test_entities.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,70 @@ def test_add_resource():
299299
}
300300

301301

302+
@pytest.mark.django_db
303+
def test_add_resource_with_comments():
304+
with TemporaryDirectory() as root:
305+
# Database setup
306+
settings.MEDIA_ROOT = root
307+
locale = LocaleFactory.create(code="fr-Test")
308+
locale_map = {locale.code: locale}
309+
repo = RepositoryFactory(url="http://example.com/repo")
310+
project = ProjectFactory.create(
311+
name="test-add-comments", locales=[locale], repositories=[repo]
312+
)
313+
314+
# Filesystem setup: FTL file with a license header, a resource comment (###)
315+
# and a group comment (##).
316+
c_ftl = dedent(
317+
"""\
318+
# This Source Code Form is subject to the terms of the Mozilla Public
319+
# License, v. 2.0.
320+
321+
### Resource-level comment for this file.
322+
323+
## Group comment for the first section.
324+
325+
key-1 = Message 1
326+
key-2 = Message 2
327+
"""
328+
)
329+
makedirs(repo.checkout_path)
330+
build_file_tree(
331+
repo.checkout_path,
332+
{
333+
"en-US": {"c.ftl": c_ftl},
334+
"fr-Test": {},
335+
},
336+
)
337+
338+
# Paths setup
339+
mock_checkout = Mock(
340+
Checkout,
341+
path=repo.checkout_path,
342+
changed=[join("en-US", "c.ftl")],
343+
removed=[],
344+
renamed=[],
345+
)
346+
paths = find_paths(project, Checkouts(mock_checkout, mock_checkout))
347+
348+
# Test
349+
assert sync_resources_from_repo(
350+
project, locale_map, mock_checkout, paths, now
351+
) == (2, {"c.ftl"}, set())
352+
res_c = project.resources.get(path="c.ftl")
353+
assert res_c.comment == "Resource-level comment for this file."
354+
assert res_c.meta == [
355+
[
356+
"info",
357+
"This Source Code Form is subject to the terms of the Mozilla Public\n"
358+
"License, v. 2.0.",
359+
]
360+
]
361+
sections = list(res_c.sections.all())
362+
assert len(sections) == 1
363+
assert sections[0].comment == "Group comment for the first section."
364+
365+
302366
@pytest.mark.django_db
303367
def test_update_resource():
304368
with TemporaryDirectory() as root:

0 commit comments

Comments
 (0)