-
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 #135 from Zeit-Labs/shadinaif/general-refactor
feat: refactor extract command with its tests
- Loading branch information
Showing
2 changed files
with
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
from datetime import datetime, timedelta | ||
import os | ||
from datetime import datetime, timedelta | ||
from functools import wraps | ||
|
||
import polib | ||
from path import Path | ||
|
||
from i18n import extract, config | ||
from path import Path | ||
|
||
from . import I18nToolTestCase, MOCK_DJANGO_APP_DIR | ||
|
||
|
||
def perform_extract(): | ||
""" | ||
Decorator for test methods in TestExtract class. | ||
""" | ||
def decorator(test_method): | ||
""" | ||
The decorator itself | ||
""" | ||
@wraps(test_method) | ||
def wrapped(self): | ||
""" | ||
The wrapper function | ||
""" | ||
extract.main( | ||
verbosity=0, | ||
config=self.configuration._filename, | ||
root_dir=MOCK_DJANGO_APP_DIR, | ||
) | ||
test_method(self) | ||
return wrapped | ||
return decorator | ||
|
||
|
||
class TestExtract(I18nToolTestCase): | ||
""" | ||
Tests functionality of i18n/extract.py | ||
""" | ||
generated_files = ('django-partial.po', 'djangojs-partial.po', 'mako.po') | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
|
@@ -31,22 +52,36 @@ def setUp(self): | |
) | ||
self.configuration = config.Configuration(root_dir=MOCK_DJANGO_APP_DIR) | ||
|
||
# Run extraction script | ||
extract.main(verbosity=0, config=self.configuration._filename, root_dir=MOCK_DJANGO_APP_DIR) | ||
@property | ||
def django_po(self): | ||
""" | ||
Returns the name of the generated django file | ||
""" | ||
return 'django-partial.po' | ||
|
||
@property | ||
def djangojs_po(self): | ||
""" | ||
Returns the name of the generated djangojs file | ||
""" | ||
return 'djangojs-partial.po' | ||
|
||
def get_files(self): | ||
""" | ||
This is a generator. | ||
Returns the fully expanded filenames for all extracted files | ||
Fails assertion if one of the files doesn't exist. | ||
""" | ||
for filename in self.generated_files: | ||
generated_files = ('mako.po', self.django_po, self.djangojs_po,) | ||
|
||
for filename in generated_files: | ||
path = Path.joinpath(self.configuration.source_messages_dir, filename) | ||
exists = Path.exists(path) | ||
self.assertTrue(exists, msg='Missing file: %s' % path) | ||
if exists: | ||
yield path | ||
|
||
yield path | ||
|
||
@perform_extract() | ||
def test_files(self): | ||
""" | ||
Asserts that each auto-generated file has been modified since 'extract' was launched. | ||
|
@@ -56,6 +91,7 @@ def test_files(self): | |
self.assertTrue(datetime.fromtimestamp(os.path.getmtime(path)) > self.start_time, | ||
msg='File not recently modified: %s' % path) | ||
|
||
@perform_extract() | ||
def test_is_keystring(self): | ||
""" | ||
Verifies is_keystring predicate | ||
|
@@ -67,6 +103,7 @@ def test_is_keystring(self): | |
self.assertTrue(extract.is_key_string(entry1.msgid)) | ||
self.assertFalse(extract.is_key_string(entry2.msgid)) | ||
|
||
@perform_extract() | ||
def test_headers(self): | ||
""" | ||
Verify all headers have been modified | ||
|
@@ -79,6 +116,7 @@ def test_headers(self): | |
msg='Missing header in %s:\n"%s"' % (path, header) | ||
) | ||
|
||
@perform_extract() | ||
def test_metadata(self): | ||
""" | ||
Verify all metadata has been modified | ||
|
@@ -90,6 +128,7 @@ def test_metadata(self): | |
expected = '[email protected]' | ||
self.assertEquals(expected, value) | ||
|
||
@perform_extract() | ||
def test_metadata_no_create_date(self): | ||
""" | ||
Verify `POT-Creation-Date` metadata has been removed | ||
|