Commit b20a5355 authored by Benjamin Tissoires's avatar Benjamin Tissoires
Browse files

ci-fairy: add an alias for ci_fairy variable



This is definitively something that we were missing in the first
place.
Signed-off-by: Benjamin Tissoires's avatarBenjamin Tissoires <benjamin.tissoires@gmail.com>
parent 8b073098
......@@ -166,7 +166,7 @@ ci-fairy images:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
FDO_DISTRIBUTION_TAG: sha256-c953306ebd442ff7a5a15f7c59d337a58dd435e7efc103329ea279178ef46680
.ci-fairy-local-image:
extends:
......@@ -355,7 +355,7 @@ test published images:
stage: test published images
script:
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:buildah-2021-03-18.0
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-c953306ebd442ff7a5a15f7c59d337a58dd435e7efc103329ea279178ef46680
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-base-2021-03-18.0
- skopeo inspect docker://quay.io/freedesktop.org/ci-templates:qemu-mkosi-base-2021-03-18.0
rules:
......
......@@ -23,7 +23,7 @@ ci-fairy-base-image:
.ci-fairy-tag:
variables:
FDO_DISTRIBUTION_TAG: sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
FDO_DISTRIBUTION_TAG: sha256-c953306ebd442ff7a5a15f7c59d337a58dd435e7efc103329ea279178ef46680
# The actual ci-fairy image with ci-fairy installed
# This image uses the sha of the ci-fairy script itself as tag.
......
......@@ -256,6 +256,21 @@ allows for the selection of the root node. For example: ::
.. note:: Root nodes in the config files must not be named ``ci_fairy``
to avoid namespace clashes.
For convenience, users are encouraged to use the unicode character ``🧚`` as
an alias for ``ci_fairy``. This makes the template clearer that this object
is *not* part of the config file.
Example:
.. code-block::
variables:
FEDORA_TAG: {{
(🧚.hashfiles('.gitlab-ci/config.yml',
'.gitlab-ci/ci.template',
'.gitlab-ci/fedora-install.sh'))[0:12]
}}'
.. _ci-fairy-commit-checks:
Checking commits
......
......@@ -31,6 +31,6 @@
# Variables provided by this template should be considered read-only.
#
.fdo.ci-fairy:
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
image: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-c953306ebd442ff7a5a15f7c59d337a58dd435e7efc103329ea279178ef46680
variables:
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-7030f1564de61cd75e7560da4fa694f603279b34bec1a5d7c5bce097a98323b5
\ No newline at end of file
FDO_DISTRIBUTION_IMAGE: quay.io/freedesktop.org/ci-templates:ci-fairy-sha256-c953306ebd442ff7a5a15f7c59d337a58dd435e7efc103329ea279178ef46680
\ No newline at end of file
......@@ -892,6 +892,12 @@ def lint(ctx, filename):
logger.error(e)
class ci_fairyFileSystemLoader(jinja2.FileSystemLoader):
def get_source(self, environment, template):
source, filename, uptodate = super().get_source(environment, template)
return source.replace('🧚', 'ci_fairy'), filename, uptodate
@ci_fairy.command()
@click.option('--config', help='YAML configuration file', required=False, multiple=True)
@click.option('--root', help='YAML node to use as root node', type=str, multiple=True)
......@@ -918,6 +924,11 @@ def generate_template(ctx, config, root, template, output_file, verify):
Where the --verify option is given, the output file is not overwritten.
Instead, ci-fairy compares the newly generated to the existing output
file and exits with an error if the two differs.
The command adds a special variable `ci_fairy` to the available jinja2
variables.
The unicode character 🧚 can be used in the template if needed instead
of typing `ci_fairy`.
'''
if len(root) > 0 and len(root) != len(config):
raise click.UsageError('--root must be given for each --config')
......@@ -970,10 +981,17 @@ def generate_template(ctx, config, root, template, output_file, verify):
_env_ci_fairy.nodes = dict(data)
templatedir = templatefile.parent
env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.fspath(templatedir)),
env = jinja2.Environment(loader=ci_fairyFileSystemLoader(os.fspath(templatedir)),
trim_blocks=True, lstrip_blocks=True,
extensions=['jinja2.ext.do'])
env.globals["ci_fairy"] = _env_ci_fairy
# note: the following is for documentation purpose mostly
# jinja2 doesn't accept random unicode as variable names,
# so we have to tweak the loader to 'fix' that outrageous
# situation
env.globals['🧚'] = _env_ci_fairy
template = env.get_template(templatefile.name)
if output_file is None:
......
......@@ -565,7 +565,9 @@ def test_template_multiple_config_with_root():
assert result.exit_code == 2
def test_template_ci_fairy_hashfiles():
@pytest.mark.parametrize('fairy',
['ci_fairy', '🧚'])
def test_template_ci_fairy_hashfiles(fairy):
runner = CliRunner()
with runner.isolated_filesystem():
with open('test.yml', 'w') as fd:
......@@ -573,13 +575,13 @@ def test_template_ci_fairy_hashfiles():
with open('filea.txt', 'w') as fd:
fd.write('abc\n')
with open('test.tmpl', 'w') as fd:
fd.write('file1: {{ ci_fairy.hashfiles("filea.txt") }}\n')
fd.write('file2: {{ ci_fairy.hashfiles("./filea.txt") }}\n')
fd.write('file3: {{ ci_fairy.hashfiles("filea.txt", "filea.txt") }}\n')
fd.write('file4: {{ ci_fairy.hashfiles("test.tmpl") }}\n')
fd.write('file5: {{ ci_fairy.hashfiles("./test.tmpl") }}\n')
fd.write('file6: {{ ci_fairy.hashfiles("test.tmpl", "test.tmpl") }}\n')
fd.write('file7: {{ ci_fairy.hashfiles("filea.txt", "test.tmpl") }}\n')
fd.write(f'file1: {{{{ { fairy }.hashfiles("filea.txt") }}}}\n')
fd.write(f'file2: {{{{ { fairy }.hashfiles("./filea.txt") }}}}\n')
fd.write(f'file3: {{{{ { fairy }.hashfiles("filea.txt", "filea.txt") }}}}\n')
fd.write(f'file4: {{{{ { fairy }.hashfiles("test.tmpl") }}}}\n')
fd.write(f'file5: {{{{ { fairy }.hashfiles("./test.tmpl") }}}}\n')
fd.write(f'file6: {{{{ { fairy }.hashfiles("test.tmpl", "test.tmpl") }}}}\n')
fd.write(f'file7: {{{{ { fairy }.hashfiles("filea.txt", "test.tmpl") }}}}\n')
args = ['generate-template', '--config', 'test.yml',
'--root', '/fruit/orange', 'test.tmpl']
......@@ -587,16 +589,27 @@ def test_template_ci_fairy_hashfiles():
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert result.stdout == 'file1: 14b3d22e38217fb8835c1fd799454d20836f99cbd822cf5949f24ff5e3bd2bd5\n' + \
'file2: a8992d7bc5335366e4acba105047e956e10cfb9d7bd836eeffbb7e1fcc7fd809\n' + \
'file3: c17f78cc120f554d3ed42e80c3c9f2eeaaf7418b0f1d963c3e15ebcbdf2365aa\n' + \
'file4: 4a4c1b3cd68939cb8b7934f58bb7c411b10856ad778403f935672d42e44ea3d0\n' + \
'file5: cf578ac3b0bb8de869eb8aec17873e3e4ce28be4df6151af6abbb202b386dc34\n' + \
'file6: e4ab45e5961c17493e504896d35945619bed9c5183aae9b9cd9f028c74320906\n' + \
'file7: dbc23bd9d9e6d52829aefdca4da566620337c2136b7942af85b6e21f664f7684'
expected_stdout = 'file1: 14b3d22e38217fb8835c1fd799454d20836f99cbd822cf5949f24ff5e3bd2bd5\n' + \
'file2: a8992d7bc5335366e4acba105047e956e10cfb9d7bd836eeffbb7e1fcc7fd809\n' + \
'file3: c17f78cc120f554d3ed42e80c3c9f2eeaaf7418b0f1d963c3e15ebcbdf2365aa\n'
if fairy == 'ci_fairy':
expected_stdout += 'file4: 4a4c1b3cd68939cb8b7934f58bb7c411b10856ad778403f935672d42e44ea3d0\n' + \
'file5: cf578ac3b0bb8de869eb8aec17873e3e4ce28be4df6151af6abbb202b386dc34\n' + \
'file6: e4ab45e5961c17493e504896d35945619bed9c5183aae9b9cd9f028c74320906\n' + \
'file7: dbc23bd9d9e6d52829aefdca4da566620337c2136b7942af85b6e21f664f7684'
else:
expected_stdout += 'file4: 7adb8b5752d1db679351981ee78a436e58ebeb959991c7cecca9ae023962237f\n' + \
'file5: 3f56301c71db500ef96ae3f32538f95d40b6c8ed09f00261c588e85de9e0db76\n' + \
'file6: 5b79358b35b772960ddf8c94efd854300b71f3896f7fa23aa0fecab77276b928\n' + \
'file7: 3627bdca680bb5f490301d5bdf52dfdc17b983b290eba0ba4dd36216f02ca4a0'
assert result.stdout == expected_stdout
def test_template_ci_fairy_sha256sum():
@pytest.mark.parametrize('fairy',
['ci_fairy', '🧚'])
def test_template_ci_fairy_sha256sum(fairy):
runner = CliRunner()
with runner.isolated_filesystem():
with open('test.yml', 'w') as fd:
......@@ -604,10 +617,10 @@ def test_template_ci_fairy_sha256sum():
with open('filea.txt', 'w') as fd:
fd.write('abc\n')
with open('test.tmpl', 'w') as fd:
fd.write('file1: {{ ci_fairy.sha256sum("filea.txt") }}\n')
fd.write('file2: {{ ci_fairy.sha256sum("./filea.txt") }}\n')
fd.write('file3: {{ ci_fairy.sha256sum("./filea.txt", prefix=True) }}\n')
fd.write('file4: {{ ci_fairy.sha256sum("./filea.txt", prefix=False) }}\n')
fd.write(f'file1: {{{{ { fairy }.sha256sum("filea.txt") }}}}\n')
fd.write(f'file2: {{{{ { fairy }.sha256sum("./filea.txt") }}}}\n')
fd.write(f'file3: {{{{ { fairy }.sha256sum("./filea.txt", prefix=True) }}}}\n')
fd.write(f'file4: {{{{ { fairy }.sha256sum("./filea.txt", prefix=False) }}}}\n')
args = ['generate-template', '--config', 'test.yml',
'--root', '/fruit/orange', 'test.tmpl']
......@@ -622,13 +635,15 @@ def test_template_ci_fairy_sha256sum():
f'file4: {sha}'
def test_template_ci_fairy_nodes():
@pytest.mark.parametrize('fairy',
['ci_fairy', '🧚'])
def test_template_ci_fairy_nodes(fairy):
runner = CliRunner()
with runner.isolated_filesystem():
with open('test.yml', 'w') as fd:
fd.write(DEFAULT_YAML)
with open('test.tmpl', 'w') as fd:
fd.write('{% for key, item in ci_fairy.nodes.items() %}\n')
fd.write(f'{{% for key, item in { fairy }.nodes.items() %}}\n')
fd.write('{{ key }}:\n')
fd.write(' {% for node in item %}\n')
fd.write(' {{ node }}\n')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment