Skip to content

Commit

Permalink
tidy up rose-jinja2 testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed Nov 11, 2020
1 parent 152efa7 commit 4e15e5e
Showing 1 changed file with 52 additions and 37 deletions.
89 changes: 52 additions & 37 deletions tests/unit/parsec/test_rose_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import pytest

from cylc.flow.parsec.rose_utils import get_rose_vars
from types import SimpleNamespace

from cylc.flow.parsec.rose_utils import get_rose_vars

def test_get_rosedirs(tmp_path):
"""Function returns dict of [jinja2:suite.rc] items

Also check that function does _not_ return [env] section items.
@pytest.fixture
def rose_config_jinja2(tmp_path, scope='module'):
"""Fixture which returns a tmp_path containing setup ``rose-suite.conf``
files.
Creates:
.
Expand All @@ -31,13 +34,6 @@ def test_get_rosedirs(tmp_path):
`-- opt
|-- rose-suite-gravy.conf
`-- rose-suite-chips.conf
Scenarios tested:
- Read in a basic rose-suite.conf file. Ensure we don't return env,
just jinja2.
- Get optional config name from an environment variable.
- Get optional config name from command line option.
- Get optional config name from an explicit over-ride string.
"""
with open(tmp_path / 'rose-suite.conf', 'w+') as testfh:
# The [env] section is there to make sure I don't load it with
Expand All @@ -46,47 +42,66 @@ def test_get_rosedirs(tmp_path):
"[env]\n"
"TIMS_ENV_VAR=Jelly\n"
"[jinja2:suite.rc]\n"
"TIMS_JINJA2_ENV=64\n"
"Another_Jinja2_var=IceCream\n"
"JINJA2_VAR=64\n"
"Another_Jinja2_var=Defined in config\n"
)

opt_dir = tmp_path / 'opt'
opt_dir.mkdir()
with open(opt_dir / 'rose-suite-gravy.conf', 'w+') as testfh:
testfh.write(
"[jinja2:suite.rc]\n"
"TIMS_JINJA2_ENV=42\n"
"Another_Jinja2_var=Peas\n"
"JINJA2_VAR=42\n"
"Another_Jinja2_var=Optional config picked from env var\n"
)

with open(opt_dir / 'rose-suite-chips.conf', 'w+') as testfh:
testfh.write(
"[jinja2:suite.rc]\n"
"TIMS_JINJA2_ENV=99\n"
"Another_Jinja2_var=Chips\n"
"JINJA2_VAR=99\n"
"Another_Jinja2_var=Optional config picked from CLI\n"
)
return tmp_path

assert get_rose_vars(tmp_path) == {
'Another_Jinja2_var': 'IceCream',
'TIMS_JINJA2_ENV': '64'
}

os.environ['ROSE_SUITE_OPT_CONF_KEYS'] = "gravy"
assert get_rose_vars(tmp_path) == {
'Another_Jinja2_var': 'Peas',
'TIMS_JINJA2_ENV': '42'
}
@pytest.mark.parametrize(
'override, exp_ANOTHER_JINJA2_ENV, exp_JINJA2_VAR',
[
(None, 'Defined in config', '64'),
('environment', 'Optional config picked from env var', '42'),
('CLI', 'Optional config picked from CLI', '99'),
('override', 'Variable overridden', '99')
]
)
def test_get_jinja2_basic(
rose_config_jinja2,
override,
exp_ANOTHER_JINJA2_ENV,
exp_JINJA2_VAR
):
"""Function returns dict of [jinja2:suite.rc] items
from types import SimpleNamespace
options = SimpleNamespace()
options.opt_conf_keys = ["chips"]
assert get_rose_vars(tmp_path, options) == {
'Another_Jinja2_var': 'Chips',
'TIMS_JINJA2_ENV': '99'
}
Scenarios tested:
- Read in a basic rose-suite.conf file. Ensure we don't return env,
just jinja2.
- Get optional config name from an environment variable.
- Get optional config name from command line option.
- Get optional config name from an explicit over-ride string.
"""
options = None
if override == 'environment':
os.environ['ROSE_SUITE_OPT_CONF_KEYS'] = "gravy"
elif override == 'CLI':
options = SimpleNamespace()
options.opt_conf_keys = ["chips"]
elif override == 'override':
options = SimpleNamespace()
options.opt_conf_keys = ["chips"]
options.defines = [
"[jinja2:suite.rc]Another_Jinja2_var=Variable overridden"
]

options.defines = ["[jinja2:suite.rc]TIMS_JINJA2_ENV=Curry_Sauce"]
assert get_rose_vars(tmp_path, options) == {
'Another_Jinja2_var': 'Chips',
'TIMS_JINJA2_ENV': 'Curry_Sauce'
assert get_rose_vars(rose_config_jinja2, options) == {
'Another_Jinja2_var': f'{exp_ANOTHER_JINJA2_ENV}',
'JINJA2_VAR': f'{exp_JINJA2_VAR}'
}

0 comments on commit 4e15e5e

Please sign in to comment.