Skip to content

Commit

Permalink
Ensure that a :suite.rc is added to the template language when crea…
Browse files Browse the repository at this point in the history
…ting defines.
  • Loading branch information
wxtim committed Oct 9, 2023
1 parent 7697d23 commit 98b3129
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ones in. -->
[#250](https://github.com/cylc/cylc-rose/pull/250) - Prevent project
name being manually set to an empty string.

[#248](https://github.com/cylc/cylc-rose/pull/248) - Make sure that
rose stem sets variables in `[jinja2:suite.rc]` not `[jinja2]`.

## __cylc-rose-1.3.0 (<span actions:bind='release-date'>Released 2023-07-21</span>)__

### Fixes
Expand Down
5 changes: 3 additions & 2 deletions cylc/rose/stem.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
)

from cylc.rose.entry_points import get_rose_vars
from cylc.rose.utilities import id_templating_section

import metomi.rose.config
from metomi.rose.fs_util import FileSystemUtil
Expand Down Expand Up @@ -452,10 +453,10 @@ def process(self):
self.opts.project.append(project)

if i == 0:
# Get the name of the template section to be used:
template_type = get_rose_vars(
Path(url) / "rose-stem")["templating_detected"]
self.template_section = f'[{template_type}]'
self.template_section = id_templating_section(
template_type, with_brackets=True)

# Versions of variables with hostname prepended for working copies
url_host = self._prepend_localhost(url)
Expand Down
22 changes: 18 additions & 4 deletions cylc/rose/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pathlib import Path
import re
import shlex
from typing import TYPE_CHECKING, Any, List, Tuple, Union
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union

from cylc.flow.hostuserutil import get_host
from cylc.flow import LOG
Expand Down Expand Up @@ -192,13 +192,27 @@ def identify_templating_section(config_node):
"You should not define more than one templating section. "
f"You defined:\n\t{'; '.join(defined_sections)}"
)
elif 'jinja2:suite.rc' in defined_sections:
elif defined_sections:
return id_templating_section(defined_sections.pop())
else:
return id_templating_section('')


def id_templating_section(
section: Optional[str] = None,
with_brackets: bool = False
) -> str:
"""Return a full template section string."""
templating = None
if section and 'jinja2' in section:
templating = 'jinja2:suite.rc'
elif 'empy:suite.rc' in defined_sections:
elif section and 'empy' in section:
templating = 'empy:suite.rc'
else:

if not templating:
templating = 'template variables'

templating = f'[{templating}]' if with_brackets else templating
return templating


Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_config_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
deprecation_warnings,
dump_rose_log,
identify_templating_section,
id_templating_section,
MultipleTemplatingEnginesError
)

Expand Down Expand Up @@ -252,6 +253,20 @@ def test_identify_templating_section(node_, expect, raises):
identify_templating_section(node)


@pytest.mark.parametrize(
'input_, expect',
(
([None], 'template variables'),
(['jinja2'], 'jinja2:suite.rc'),
(['jinja2:suite.rc'], 'jinja2:suite.rc'),
([None, True], '[template variables]'),
(['jinja2', True], '[jinja2:suite.rc]'),
)
)
def test_id_templating_section(input_, expect):
assert id_templating_section(*input_) == expect


@pytest.fixture
def node_with_ROSE_ORIG_HOST():
def _inner(comment=''):
Expand Down
39 changes: 38 additions & 1 deletion tests/unit/test_rose_stem_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
from typing import Any, Tuple

from cylc.rose.stem import (
_get_rose_stem_opts,
ProjectNotFoundException,
RoseStemVersionException,
RoseSuiteConfNotFoundException,
StemRunner,
get_source_opt_from_args
get_source_opt_from_args,
)

from metomi.rose.reporter import Reporter
Expand Down Expand Up @@ -337,3 +338,39 @@ def test_ascertain_project_if_name_supplied(
ProjectNotFoundException, match='is not a working copy'
):
stemrunner._ascertain_project(item)


@pytest.mark.parametrize(
'language, expect',
(
('empy', '[empy:suite.rc]'),
('jinja2', '[jinja2:suite.rc]'),
('template variables', '[template variables]'),
)
)
def test_process_template_engine_set_correctly(monkeypatch, language, expect):
"""Defines are correctly assigned a [<template language>:suite.rc]
section.
https://github.com/cylc/cylc-rose/issues/246
"""
# Mimic expected result from get_rose_vars method:
monkeypatch.setattr(
'cylc.rose.stem.get_rose_vars',
lambda _: {'templating_detected': language}
)
monkeypatch.setattr(
'sys.argv',
['foo', 'bar']
)

# We are not interested in these checks, just in the defines
# created by the process method.
stemrunner = StemRunner(_get_rose_stem_opts()[1])
stemrunner._ascertain_project = lambda _: ['', '', '', '', '']
stemrunner._this_suite = lambda: '.'
stemrunner._check_suite_version = lambda _: '1'
stemrunner.process()

for define in stemrunner.opts.defines:
assert define.startswith(expect)

0 comments on commit 98b3129

Please sign in to comment.