-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
239 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from pathlib import Path | ||
from unittest import TestCase, mock | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
|
||
from scripts.compile_locales import compile_locales, process_po_file | ||
from tests import override_env | ||
|
||
|
||
@pytest.mark.needs_locales_compilation | ||
class TestCompileLocales(TestCase): | ||
def setUp(self): | ||
self.home_dir = Path(tempfile.mkdtemp()) | ||
self.locale_dir = (self.home_dir / 'locale').mkdir() | ||
|
||
@patch.dict(sys.modules, {'dennis': None}) | ||
def test_dennis_not_installed(self): | ||
"""Test that the script raises when dennis is not installed""" | ||
self.assertRaises(ImportError, compile_locales) | ||
|
||
@patch.dict(sys.modules, {'dennis': Mock()}) | ||
@patch('scripts.compile_locales.ThreadPoolExecutor') | ||
def test_process_po_file(self, mock_executor): | ||
"""Test that the script processes po files""" | ||
# Create po files | ||
django_po = self.home_dir / 'locale' / 'django.po' | ||
django_po.touch() | ||
djangojs_po = self.home_dir / 'locale' / 'djangojs.po' | ||
djangojs_po.touch() | ||
|
||
# Setup ThreadPoolExecutor mock | ||
mock_executor_instance = Mock() | ||
mock_executor.return_value.__enter__.return_value = mock_executor_instance | ||
|
||
with override_env(HOME=self.home_dir.as_posix()): | ||
compile_locales() | ||
|
||
# Get the actual arguments passed to map | ||
actual_args = mock_executor_instance.map.call_args[0] | ||
self.assertEqual(actual_args[0], process_po_file) | ||
self.assertEqual(list(actual_args[1]), [django_po, djangojs_po]) | ||
|
||
|
||
class TestProcessPoFile(TestCase): | ||
def setUp(self): | ||
self.pofile = Path(tempfile.mkdtemp()) / 'django.po' | ||
|
||
mock_subprocess = patch('scripts.compile_locales.subprocess.run') | ||
self.mock_subprocess = mock_subprocess.start() | ||
self.addCleanup(mock_subprocess.stop) | ||
|
||
def test_process_po_file(self): | ||
process_po_file(self.pofile.as_posix()) | ||
self.assertTrue(self.pofile.with_suffix('.mo').exists()) | ||
|
||
assert self.mock_subprocess.call_args_list == [ | ||
mock.call( | ||
['dennis-cmd', 'lint', '--errorsonly', self.pofile.as_posix()], | ||
capture_output=True, | ||
check=False, | ||
), | ||
mock.call( | ||
[ | ||
'msgfmt', | ||
'-o', | ||
self.pofile.with_suffix('.mo'), | ||
self.pofile.as_posix(), | ||
], | ||
check=True, | ||
), | ||
] | ||
|
||
def test_process_po_file_retries(self): | ||
self.mock_subprocess.side_effect = subprocess.CalledProcessError( | ||
returncode=1, | ||
cmd=['dennis-cmd', 'lint', '--errorsonly', self.pofile.as_posix()], | ||
) | ||
|
||
with self.assertRaises(subprocess.CalledProcessError): | ||
process_po_file(self.pofile.as_posix()) | ||
|
||
self.assertTrue(self.pofile.with_suffix('.mo').exists()) | ||
|
||
# We expect 3 attempts to process the file | ||
self.assertEqual(self.mock_subprocess.call_count, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import tempfile | ||
from pathlib import Path | ||
from unittest import TestCase, mock | ||
|
||
from scripts.sync_host_files import sync_host_files | ||
from tests import override_env | ||
|
||
|
||
@mock.patch('scripts.sync_host_files.subprocess.run') | ||
class TestSyncHostFiles(TestCase): | ||
def test_sync_host_files(self, mock_subprocess): | ||
sync_host_files() | ||
|
||
mock_subprocess.assert_has_calls( | ||
[ | ||
mock.call(['make', 'update_deps'], check=True), | ||
# mock.call(['make', 'compile_locales'], check=True), | ||
# mock.call(['make', 'update_assets'], check=True), | ||
] | ||
) | ||
|
||
def test_sync_host_files_production(self, mock_subprocess): | ||
mock_build = Path(tempfile.mktemp()) | ||
mock_build.write_text(json.dumps({'target': 'production'})) | ||
|
||
with override_env(BUILD_INFO=mock_build.as_posix()): | ||
sync_host_files() | ||
|
||
mock_subprocess.assert_has_calls( | ||
[ | ||
mock.call(['make', 'update_deps'], check=True), | ||
mock.call(['make', 'compile_locales'], check=True), | ||
mock.call(['make', 'update_assets'], check=True), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import tempfile | ||
from pathlib import Path | ||
from unittest import TestCase, mock | ||
|
||
from scripts.update_assets import clean_static_dirs, update_assets | ||
from tests import override_env | ||
|
||
|
||
class TestUpdateAssets(TestCase): | ||
def setUp(self): | ||
self.mocks = {} | ||
for name in ['clean_static_dirs', 'subprocess.run']: | ||
patch = mock.patch(f'scripts.update_assets.{name}') | ||
self.mocks[name] = patch.start() | ||
self.addCleanup(patch.stop) | ||
|
||
def test_update_assets(self): | ||
update_assets() | ||
|
||
assert self.mocks['clean_static_dirs'].call_count == 1 | ||
|
||
assert self.mocks['subprocess.run'].call_args_list == [ | ||
mock.call( | ||
['python3', 'manage.py', 'compress_assets'], check=True, env=mock.ANY | ||
), | ||
mock.call( | ||
['python3', 'manage.py', 'generate_jsi18n_files'], | ||
check=True, | ||
env=mock.ANY, | ||
), | ||
mock.call( | ||
['python3', 'manage.py', 'collectstatic', '--noinput'], | ||
check=True, | ||
env=mock.ANY, | ||
), | ||
] | ||
|
||
for call in self.mocks['subprocess.run'].call_args_list: | ||
assert ( | ||
call.kwargs['env']['DJANGO_SETTINGS_MODULE'] | ||
== 'olympia.lib.settings_base' | ||
) | ||
|
||
def test_update_assets_with_verbose(self): | ||
update_assets(verbose=True) | ||
|
||
assert self.mocks['clean_static_dirs'].call_args_list == [ | ||
mock.call(True), | ||
] | ||
|
||
|
||
class TestCleanStaticDirs(TestCase): | ||
def setUp(self): | ||
self.home = Path(tempfile.mkdtemp()) | ||
|
||
def _run_clean_static_dirs(self, verbose=False): | ||
with override_env(HOME=self.home.as_posix()): | ||
clean_static_dirs(verbose=verbose) | ||
|
||
def test_creates_dirs(self): | ||
self._run_clean_static_dirs() | ||
|
||
assert self.home.joinpath('static-build').exists() | ||
assert self.home.joinpath('site-static').exists() | ||
|
||
def test_empties_dirs(self): | ||
self.home.joinpath('static-build').mkdir() | ||
(self.home / 'static-build' / 'test.txt').touch() | ||
|
||
self.home.joinpath('site-static').mkdir() | ||
(self.home / 'site-static' / 'test.txt').touch() | ||
|
||
self._run_clean_static_dirs() | ||
|
||
assert not (self.home / 'static-build' / 'test.txt').exists() | ||
assert not (self.home / 'site-static' / 'test.txt').exists() |