Skip to content

Commit 0e01727

Browse files
committed
Add yadm.filename variable to default template processor
Similar to yadm.source but reflects the current file also in included files (#520).
1 parent 2ac90b0 commit 0e01727

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

test/test_unit_template_default.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141

142142
INCLUDE_BASIC = "basic\n"
143143
INCLUDE_VARIABLES = """\
144-
included <{{ yadm.class }}> file
144+
included <{{ yadm.class }}> file ({{yadm.filename}})
145145
146146
empty line above
147147
"""
@@ -151,8 +151,8 @@
151151
The first line
152152
{% include empty %}
153153
An empty file removes the line above
154-
{%include basic%}
155-
{% include "./variables.{{ yadm.os }}" %}
154+
{%include ./basic%}
155+
{% include "variables.{{ yadm.os }}" %}
156156
{% include dir/nested %}
157157
Include basic again:
158158
{% include basic %}
@@ -161,7 +161,7 @@
161161
The first line
162162
An empty file removes the line above
163163
basic
164-
included <{LOCAL_CLASS}> file
164+
included <{LOCAL_CLASS}> file (VARIABLES_FILENAME)
165165
166166
empty line above
167167
no newline at the end
@@ -280,6 +280,8 @@ def test_include(runner, yadm, tmpdir):
280280
input_file.chmod(FILE_MODE)
281281
output_file = tmpdir.join("output")
282282

283+
expected = EXPECTED_INCLUDE.replace("VARIABLES_FILENAME", str(variables_file))
284+
283285
script = f"""
284286
YADM_TEST=1 source {yadm}
285287
set_awk
@@ -290,7 +292,7 @@ def test_include(runner, yadm, tmpdir):
290292
run = runner(command=["bash"], inp=script)
291293
assert run.success
292294
assert run.err == ""
293-
assert output_file.read() == EXPECTED_INCLUDE
295+
assert output_file.read() == expected
294296
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
295297

296298

yadm

+4-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ BEGIN {
379379
yadm["user"] = user
380380
yadm["distro"] = distro
381381
yadm["distro_family"] = distro_family
382-
yadm["source"] = source
382+
yadm["source"] = ARGV[1]
383383
384384
VARIABLE = "(env|yadm)\\.[a-zA-Z0-9_]+"
385385
@@ -469,6 +469,9 @@ function replace_vars(input) {
469469
if (fields[1] == "env") {
470470
output = output ENVIRON[fields[2]]
471471
}
472+
else if (fields[2] == "filename") {
473+
output = output filename[current]
474+
}
472475
else {
473476
output = output yadm[fields[2]]
474477
}
@@ -485,7 +488,6 @@ EOF
485488
-v user="$local_user" \
486489
-v distro="$local_distro" \
487490
-v distro_family="$local_distro_family" \
488-
-v source="$input" \
489491
-v source_dir="$(builtin_dirname "$input")" \
490492
"$awk_pgm" \
491493
"$input" "${local_classes[@]}"

yadm.1

+1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ During processing, the following variables are available in the template:
705705
yadm.classes YADM_CLASSES All classes
706706
yadm.distro YADM_DISTRO lsb_release \-si
707707
yadm.distro_family YADM_DISTRO_FAMILY ID_LIKE from /etc/os-release
708+
yadm.filename Filename for the current file
708709
yadm.hostname YADM_HOSTNAME uname \-n (without domain)
709710
yadm.os YADM_OS uname \-s
710711
yadm.source YADM_SOURCE Template filename

0 commit comments

Comments
 (0)