From 98b31298b51f0ff071a6b12332c3ea1d11aa3cbc Mon Sep 17 00:00:00 2001
From: Tim Pillinger <26465611+wxtim@users.noreply.github.com>
Date: Tue, 22 Aug 2023 15:26:19 +0100
Subject: [PATCH] Ensure that a `:suite.rc` is added to the template language
when creating defines.
---
CHANGES.md | 3 +++
cylc/rose/stem.py | 5 ++--
cylc/rose/utilities.py | 22 ++++++++++++++---
tests/unit/test_config_node.py | 15 ++++++++++++
tests/unit/test_rose_stem_units.py | 39 +++++++++++++++++++++++++++++-
5 files changed, 77 insertions(+), 7 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index d5fe9cfd..aa700476 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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 (Released 2023-07-21)__
### Fixes
diff --git a/cylc/rose/stem.py b/cylc/rose/stem.py
index 49b05efb..f061f9c9 100644
--- a/cylc/rose/stem.py
+++ b/cylc/rose/stem.py
@@ -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
@@ -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)
diff --git a/cylc/rose/utilities.py b/cylc/rose/utilities.py
index da367b47..47f3b1ad 100644
--- a/cylc/rose/utilities.py
+++ b/cylc/rose/utilities.py
@@ -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
@@ -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
diff --git a/tests/unit/test_config_node.py b/tests/unit/test_config_node.py
index 86bf8232..bcd8d422 100644
--- a/tests/unit/test_config_node.py
+++ b/tests/unit/test_config_node.py
@@ -32,6 +32,7 @@
deprecation_warnings,
dump_rose_log,
identify_templating_section,
+ id_templating_section,
MultipleTemplatingEnginesError
)
@@ -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=''):
diff --git a/tests/unit/test_rose_stem_units.py b/tests/unit/test_rose_stem_units.py
index d88bbdec..0df9edd2 100644
--- a/tests/unit/test_rose_stem_units.py
+++ b/tests/unit/test_rose_stem_units.py
@@ -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
@@ -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 [: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)