зеркало из https://github.com/mozilla/bedrock.git
Convert l10n_update command to use new git util
This commit is contained in:
Родитель
78617d956c
Коммит
fc53c771af
|
@ -1,62 +1,18 @@
|
|||
# This Source Code Form is subject to the terms of the Mozilla Public
|
||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
from __future__ import print_function, unicode_literals
|
||||
|
||||
import os
|
||||
from shutil import rmtree
|
||||
from subprocess import check_output, STDOUT
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
|
||||
GIT = getattr(settings, 'GIT_BIN', 'git')
|
||||
|
||||
|
||||
def git(*args):
|
||||
return check_output((GIT,) + args, stderr=STDOUT)
|
||||
from bedrock.utils.git import GitRepo
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Clones or updates l10n info from github'
|
||||
args = ''
|
||||
locales_repo = settings.LOCALES_REPO
|
||||
locales_path = settings.LOCALES_PATH
|
||||
locales_path_str = str(locales_path)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if self.locales_path.is_dir():
|
||||
if not self.locales_path.joinpath('.git').is_dir():
|
||||
rmtree(self.locales_path_str, ignore_errors=True)
|
||||
self.clone_repo()
|
||||
else:
|
||||
self.update_repo()
|
||||
else:
|
||||
self.clone_repo()
|
||||
|
||||
@property
|
||||
def remote_name(self):
|
||||
return 'l10n-dev' if settings.DEV else 'l10n-prod'
|
||||
|
||||
@property
|
||||
def branch_name(self):
|
||||
return '{}/master'.format(self.remote_name)
|
||||
|
||||
def has_remote(self):
|
||||
return self.remote_name in git('remote')
|
||||
|
||||
def add_remote(self):
|
||||
print('adding remote {}'.format(self.remote_name))
|
||||
git('remote', 'add', self.remote_name, self.locales_repo)
|
||||
|
||||
def update_repo(self):
|
||||
os.chdir(self.locales_path_str)
|
||||
if not self.has_remote():
|
||||
self.add_remote()
|
||||
|
||||
git('fetch', self.remote_name)
|
||||
git('checkout', '-f', self.branch_name)
|
||||
|
||||
def clone_repo(self):
|
||||
git('clone', '--origin', self.remote_name, '--depth', '1',
|
||||
self.locales_repo, self.locales_path_str)
|
||||
repo = GitRepo(settings.LOCALES_PATH, settings.LOCALES_REPO)
|
||||
repo.update()
|
||||
|
|
|
@ -13,7 +13,7 @@ from textwrap import dedent
|
|||
from django.conf import settings
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from mock import ANY, MagicMock, Mock, patch, call
|
||||
from mock import ANY, MagicMock, Mock, patch
|
||||
|
||||
from lib.l10n_utils.gettext import _append_to_lang_file, merge_lang_files
|
||||
from lib.l10n_utils.management.commands.l10n_check import (
|
||||
|
@ -23,7 +23,6 @@ from lib.l10n_utils.management.commands.l10n_check import (
|
|||
update_templates,
|
||||
)
|
||||
from lib.l10n_utils.management.commands.l10n_extract import extract_from_files
|
||||
from lib.l10n_utils.management.commands import l10n_update
|
||||
from lib.l10n_utils.tests import capture_stdio
|
||||
|
||||
|
||||
|
@ -41,63 +40,6 @@ TRUE_MOCK = Mock()
|
|||
TRUE_MOCK.return_value = True
|
||||
|
||||
|
||||
@patch.object(l10n_update, 'git')
|
||||
class TestL10nUpdate(TestCase):
|
||||
def setUp(self):
|
||||
self.cmd = l10n_update.Command()
|
||||
|
||||
@override_settings(DEV=True)
|
||||
def test_clone_if_no_locales(self, git_mock):
|
||||
self.cmd.locales_path = Mock()
|
||||
self.cmd.locales_path.is_dir.return_value = False
|
||||
self.cmd.handle()
|
||||
git_mock.assert_called_once_with('clone', '--origin', 'l10n-dev', '--depth', '1',
|
||||
self.cmd.locales_repo, self.cmd.locales_path_str)
|
||||
|
||||
@override_settings(DEV=True)
|
||||
@patch.object(l10n_update.os, 'chdir', Mock())
|
||||
def test_update_if_locales(self, git_mock):
|
||||
self.cmd.locales_path = Mock()
|
||||
self.cmd.locales_path.is_dir.return_value = True
|
||||
self.cmd.locales_path.joinpath.return_value.is_dir.return_value = True
|
||||
git_mock.return_value = 'l10n-dev'
|
||||
self.cmd.handle()
|
||||
git_mock.assert_has_calls([
|
||||
call('remote'),
|
||||
call('fetch', 'l10n-dev'),
|
||||
call('checkout', '-f', 'l10n-dev/master'),
|
||||
])
|
||||
|
||||
@override_settings(DEV=False)
|
||||
@patch.object(l10n_update.os, 'chdir', Mock())
|
||||
def test_update_adds_remote_if_absent(self, git_mock):
|
||||
# should be prod remote
|
||||
self.cmd.locales_path = Mock()
|
||||
self.cmd.locales_path.is_dir.return_value = True
|
||||
self.cmd.locales_path.joinpath.return_value.is_dir.return_value = True
|
||||
git_mock.return_value = 'not the remote'
|
||||
self.cmd.handle()
|
||||
git_mock.assert_has_calls([
|
||||
call('remote'),
|
||||
call('remote', 'add', 'l10n-prod', self.cmd.locales_repo),
|
||||
call('fetch', 'l10n-prod'),
|
||||
call('checkout', '-f', 'l10n-prod/master'),
|
||||
])
|
||||
|
||||
@override_settings(DEV=True)
|
||||
@patch.object(l10n_update, 'rmtree')
|
||||
def test_error_if_locales_not_repo(self, rmtree_mock, git_mock):
|
||||
self.cmd.locales_path = Mock()
|
||||
self.cmd.locales_path.is_dir.return_value = True
|
||||
self.cmd.locales_path.joinpath.return_value.is_dir.return_value = False
|
||||
self.cmd.clone_repo = Mock()
|
||||
self.cmd.update_repo = Mock()
|
||||
self.cmd.handle()
|
||||
rmtree_mock.assert_called_with(self.cmd.locales_path_str, ignore_errors=True)
|
||||
self.cmd.clone_repo.assert_called()
|
||||
self.cmd.update_repo.assert_not_called()
|
||||
|
||||
|
||||
@override_settings(ROOT=ROOT)
|
||||
class TestL10nExtract(TestCase):
|
||||
def test_extract_from_files(self):
|
||||
|
|
Загрузка…
Ссылка в новой задаче