-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from collective/issue-5-fixture-generate-mo
Add fixture to compile translation files during tests
- Loading branch information
Showing
6 changed files
with
82 additions
and
5 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Add fixture `generate_mo` to compile translation files during tests [@ericof] |
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,23 @@ | ||
"""Environment fixtures.""" | ||
import os | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def generate_mo(): | ||
"""Compile translation files and generate .mo files. | ||
This is a session fixture, as it needs to compile files just once. | ||
""" | ||
# Set environment variable | ||
key = "zope_i18n_compile_mo_files" | ||
current_value = os.getenv(key, None) | ||
os.environ[key] = "1" | ||
try: | ||
yield | ||
finally: | ||
# Revert to previous state | ||
if current_value is None: | ||
os.environ.pop(key) | ||
else: | ||
os.environ[key] = current_value |
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,30 @@ | ||
TEST_FILES = { | ||
"test_generate_mo_result_no_env.py": """ | ||
import os | ||
def test_generate_mo_result(generate_mo): | ||
assert os.getenv("zope_i18n_compile_mo_files") == "1" | ||
""", | ||
"test_generate_mo_result_with_env.py": """ | ||
import pytest | ||
import os | ||
@pytest.fixture(scope="session", autouse=True) | ||
def set_i18n_var(): | ||
os.environ["zope_i18n_compile_mo_files"] = "2" | ||
def test_generate_mo_result(): | ||
assert os.getenv("zope_i18n_compile_mo_files") == "2" | ||
""", | ||
} | ||
|
||
|
||
def test_generate_mo(testdir): | ||
"""Test generate_mo adds environment variable.""" | ||
testdir.makepyfile(**TEST_FILES) | ||
# run all tests with pytest | ||
result = testdir.runpytest() | ||
# check that all tests passed | ||
result.assert_outcomes(passed=len(TEST_FILES)) |
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