diff --git a/tests/unit/parsec/test_rose_utils.py b/tests/unit/parsec/test_rose_utils.py index 3dae9d1894e..799fb9cecbf 100644 --- a/tests/unit/parsec/test_rose_utils.py +++ b/tests/unit/parsec/test_rose_utils.py @@ -15,14 +15,17 @@ # along with this program. If not, see . 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: . @@ -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 @@ -46,8 +42,8 @@ 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' @@ -55,38 +51,57 @@ def test_get_rosedirs(tmp_path): 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}' }