From 0f29e2ea3a8845c3fe03212e113d9461312a282d Mon Sep 17 00:00:00 2001 From: Vsevolod Fedorov Date: Tue, 4 Jul 2023 11:28:07 +0300 Subject: [PATCH] Expand axis params before enumerating values Change-Id: Ib51c00ae6a000f8c87483fcdc4433a2d0d055942 --- jenkins_jobs/dimensions.py | 5 ++- jenkins_jobs/expander.py | 2 +- .../yamlparser/job_fixtures/include-param.xml | 40 +++++++++++++++++++ .../job_fixtures/include-param.yaml | 12 ++++++ .../job_fixtures/include-param.yaml.inc | 2 + tests/yamlparser/test_dimensions.py | 10 ++++- 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 tests/yamlparser/job_fixtures/include-param.xml create mode 100644 tests/yamlparser/job_fixtures/include-param.yaml create mode 100644 tests/yamlparser/job_fixtures/include-param.yaml.inc diff --git a/jenkins_jobs/dimensions.py b/jenkins_jobs/dimensions.py index 58060a06f..cd3b865bf 100644 --- a/jenkins_jobs/dimensions.py +++ b/jenkins_jobs/dimensions.py @@ -14,6 +14,7 @@ import itertools from .errors import Context, JenkinsJobsException from .loc_loader import LocList, LocDict +from jenkins_jobs.expander import Expander def _decode_axis_value(axis, value, key_pos, value_pos): @@ -43,6 +44,7 @@ def _decode_axis_value(axis, value, key_pos, value_pos): def enum_dimensions_params(axes, params, defaults): + expander = Expander() if not axes: # No axes - instantiate one job/view. yield {} @@ -56,7 +58,8 @@ def enum_dimensions_params(axes, params, defaults): value = defaults[axis] except KeyError: continue # May be, value would be received from an another axis values. - value = list(_decode_axis_value(axis, value, key_pos, value_pos)) + expanded_value = expander.expand(value, params) + value = list(_decode_axis_value(axis, expanded_value, key_pos, value_pos)) dim_values.append(value) for values in itertools.product(*dim_values): yield LocDict.merge(*values) diff --git a/jenkins_jobs/expander.py b/jenkins_jobs/expander.py index 3af85460f..f0420f3bc 100644 --- a/jenkins_jobs/expander.py +++ b/jenkins_jobs/expander.py @@ -106,7 +106,7 @@ deprecated_yaml_tags = [ # Does not expand string formats. Used in jobs and macros without parameters. class Expander: - def __init__(self, config): + def __init__(self, config=None): _yaml_object_expanders = { cls: partial(call_expand, self) for cls in yaml_classes_list } diff --git a/tests/yamlparser/job_fixtures/include-param.xml b/tests/yamlparser/job_fixtures/include-param.xml new file mode 100644 index 000000000..e560b295c --- /dev/null +++ b/tests/yamlparser/job_fixtures/include-param.xml @@ -0,0 +1,40 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + echo "param=bar" + + + + + + + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + echo "param=foo" + + + + + + diff --git a/tests/yamlparser/job_fixtures/include-param.yaml b/tests/yamlparser/job_fixtures/include-param.yaml new file mode 100644 index 000000000..e5db96e56 --- /dev/null +++ b/tests/yamlparser/job_fixtures/include-param.yaml @@ -0,0 +1,12 @@ +- project: + name: pj + param: + !include: include-param.yaml.inc + jobs: + - a-job-{param} + +- job-template: + name: a-job-{param} + builders: + - shell: | + echo "param={param}" diff --git a/tests/yamlparser/job_fixtures/include-param.yaml.inc b/tests/yamlparser/job_fixtures/include-param.yaml.inc new file mode 100644 index 000000000..59121da8a --- /dev/null +++ b/tests/yamlparser/job_fixtures/include-param.yaml.inc @@ -0,0 +1,2 @@ +- foo +- bar diff --git a/tests/yamlparser/test_dimensions.py b/tests/yamlparser/test_dimensions.py index 900d47ada..9ba4a36ff 100644 --- a/tests/yamlparser/test_dimensions.py +++ b/tests/yamlparser/test_dimensions.py @@ -196,11 +196,19 @@ cases = [ ] +def wrap_with_location(value): + if type(value) is dict: + return LocDict({key: wrap_with_location(value) for key, value in value.items()}) + if type(value) is list: + return LocList([wrap_with_location(item) for item in value]) + return value + + @pytest.mark.parametrize("axes,params,exclude,expected_dimension_params", cases) def test_dimensions(axes, params, exclude, expected_dimension_params): dimension_params = [ p - for p in enum_dimensions_params(axes, LocDict(params), defaults={}) + for p in enum_dimensions_params(axes, wrap_with_location(params), defaults={}) if is_point_included(LocList(exclude), p) ] assert dimension_params == expected_dimension_params