feat: atlas pull plugins translation

This commit is contained in:
Omar Al-Ithawi
2023-12-13 22:45:28 +03:00
parent 5c58eb47bd
commit 867eeff993
14 changed files with 308 additions and 36 deletions

View File

@@ -0,0 +1,49 @@
"""
Tests for the plugins.i18n_api Django commands module.
"""
from unittest.mock import patch
from django.core.management import call_command
def test_pull_plugin_translations_command(settings, tmp_path):
"""
Test the `pull_plugin_translations` Django command.
"""
plugins_locale_root = tmp_path / 'conf/plugins-locale/plugins'
plugins_locale_root.mkdir(parents=True)
settings.REPO_ROOT = tmp_path
with patch('subprocess.run') as mock_run:
call_command(
'pull_plugin_translations',
verbose=True,
filter='ar,es_ES',
repository='custom_repo',
)
assert mock_run.call_count == 1, 'Expected to call `subprocess.run` once'
call_kwargs = mock_run.call_args.kwargs
assert call_kwargs['check'] is True
assert call_kwargs['cwd'] == plugins_locale_root
assert call_kwargs['args'][:8] == [
'atlas', 'pull', '--expand-glob',
'--filter', 'ar,es_ES',
'--repository', 'custom_repo',
'--verbose'
], 'Pass arguments to atlas pull correctly'
assert 'translations/*/edx_proctoring/conf/locale:edx_proctoring' in call_kwargs['args'], (
'Pull edx-proctoring translations by Python module name using the "--expand-glob" option'
)
def test_compile_plugin_translations_command(settings):
"""
Test the `compile_plugin_translations` Django command.
"""
with patch('openedx.core.djangoapps.plugins.i18n_api.compile_po_files') as mock_compile_po_files:
call_command('compile_plugin_translations')
mock_compile_po_files.assert_called_once_with(settings.REPO_ROOT / 'conf/plugins-locale/plugins')

View File

@@ -1,12 +1,17 @@
"""
Tests for the plugins.i18n_api module.
"""
from unittest.mock import patch
import pytest
from django.core.management import CommandError
from ..i18n_api import (
ArgparseArgument,
BaseAtlasPullCommand,
atlas_pull_by_modules,
compile_po_files,
get_installed_plugins_module_names,
)
@@ -50,3 +55,61 @@ def test_atlas_pull_by_modules():
check=True,
cwd=locale_root,
)
def test_compile_po_files(tmp_path):
"""
Test the compile_po_files recursive call to `msgfmt`.
"""
locale_root = tmp_path / 'locale'
locale_root.mkdir()
po_file_path = locale_root / 'test.po'
with open(po_file_path, 'w'):
# Creates an empty po file
pass
with patch('subprocess.run') as mock_run:
compile_po_files(locale_root)
mock_run.assert_called_once_with(
args=[
'msgfmt', '--check-format',
'-o', str(po_file_path.with_suffix('.mo')),
str(po_file_path),
],
check=True,
)
def test_base_atlas_pull_command(tmp_path):
"""
Test the BaseAtlasPullCommand's methods.
"""
command = BaseAtlasPullCommand()
assert command.ensure_empty_directory(tmp_path) is None, 'Should not raise an exception if the directory is empty'
with pytest.raises(CommandError):
with open(tmp_path / 'test.txt', 'w'):
# Directory is not empty anymore
pass
command.ensure_empty_directory(tmp_path)
assert command.get_atlas_pull_options(
filter='ar,jp_JP',
revision='custom_branch',
repository='my_org/custom_repo',
verbose=False,
) == [
'--filter', 'ar,jp_JP', '--repository', 'my_org/custom_repo', '--revision', 'custom_branch', '--silent',
], 'Flatten out the options into a list of arguments for atlas pull'
def test_get_installed_plugins_module_names():
"""
Test the get_installed_plugins_module_names helper.
"""
plugins = get_installed_plugins_module_names()
assert 'drag_and_drop_v2' not in plugins, 'XBlocks have their own translation process'
assert 'edx_proctoring' in plugins, 'edx-proctoring should be included'
assert 'lms' not in plugins, 'lms and cms plugins are translated as part of the edx-platform itself'