-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from edx/iivic/SOL-2053
[SOL-2053] Added chaged command which checks if the translation source files are up-to-date
- Loading branch information
Showing
4 changed files
with
131 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Determine if the source translation files are up-to-date. | ||
""" | ||
from __future__ import print_function | ||
from subprocess import CalledProcessError | ||
|
||
from i18n import Runner | ||
from i18n.execute import execute | ||
|
||
|
||
class Changed(Runner): | ||
""" | ||
Class used to check if the source translation files are up-to-date | ||
""" | ||
def run(self, args): | ||
""" | ||
Main entry point of script | ||
""" | ||
changes_detected = self.detect_changes() | ||
message = self.get_message(changes_detected) | ||
print(message) | ||
return int(changes_detected) | ||
|
||
def detect_changes(self): | ||
""" | ||
Detect if changes have been made to the msgid or msgstr lines in the translation files. | ||
Returns: | ||
boolean: True, if changes detected; otherwise, False. | ||
Note: | ||
This method requires ``git`` to be installed on the executing machine. | ||
""" | ||
try: | ||
execute('git diff --exit-code -G "^(msgid|msgstr)"') | ||
return False | ||
except CalledProcessError: | ||
return True | ||
|
||
def get_message(self, changes_detected): | ||
""" | ||
Returns message depending on source translation files status. | ||
""" | ||
msg = 'Source translation files are current.' | ||
|
||
if changes_detected: | ||
msg = 'Source translations are out-of-date! Please update them.' | ||
|
||
return msg | ||
|
||
main = Changed() # pylint: disable=invalid-name | ||
|
||
if __name__ == '__main__': | ||
main() |
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,67 @@ | ||
from os import remove | ||
from shutil import copyfile | ||
from unittest import TestCase | ||
import ddt | ||
import mock | ||
|
||
from i18n.changed import Changed | ||
|
||
|
||
@ddt.ddt | ||
class TestChanged(TestCase): | ||
""" | ||
Tests functionality of i18n/changed.py | ||
""" | ||
def setUp(self): | ||
self.changed = Changed() | ||
|
||
def test_detect_changes(self): | ||
""" | ||
Verifies the detect_changes method can detect changes in translation source files. | ||
""" | ||
file_name = 'conf/locale/fake2/LC_MESSAGES/mako.po' | ||
copy = 'conf/locale/fake2/LC_MESSAGES/mako_copy.po' | ||
|
||
self.assertFalse(self.changed.detect_changes()) | ||
|
||
copyfile(file_name, copy) # Copy the .po file | ||
remove(file_name) # Make changes to the .po file | ||
self.assertTrue(self.changed.detect_changes()) # Detect changes made to the .po file | ||
copyfile(copy, file_name) # Return .po file to its previous state | ||
remove(copy) # Delete copy of .po file | ||
|
||
def test_do_not_detect_changes(self): | ||
""" | ||
Verifies the detect_changes method doesn't detect changes in rows that do not start with msgid or msgstr. | ||
""" | ||
file_name = 'test_requirements.txt' | ||
copy = 'test_requirements_copy.txt' | ||
|
||
copyfile(file_name, copy) # Copy the .txt file | ||
remove(file_name) # Make changes to the .txt file | ||
self.assertFalse(self.changed.detect_changes()) # Do not detect changes made to the .txt file | ||
copyfile(copy, file_name) # Return .txt file to its previous state | ||
remove(copy) # Delete copy of .txt file | ||
|
||
@ddt.data( | ||
(False, 'Source translation files are current.'), | ||
(True, 'Source translations are out-of-date! Please update them.') | ||
) | ||
@ddt.unpack | ||
def test_get_message(self, changes_detected, msg): | ||
""" | ||
Verifies that get_message method returns the correct message. | ||
""" | ||
self.assertEqual(self.changed.get_message(changes_detected), msg) | ||
|
||
@ddt.data( | ||
(True, 1), | ||
(False, 0) | ||
) | ||
@ddt.unpack | ||
def test_run(self, return_value, value): | ||
""" | ||
Verifies that run method returns the correct value. | ||
""" | ||
with mock.patch('i18n.changed.Changed.detect_changes', mock.Mock(return_value=return_value)): | ||
self.assertEqual(self.changed.run(''), value) |