1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2026-03-02 03:49:29 +00:00

Refactor template handling

Move common template logic out to a new template() function that calls one of
the existing template processors and then handles writing the result and
copying permissions.
This commit is contained in:
Erik Flodin
2024-12-15 15:10:02 +01:00
parent eb819782ee
commit 119d1ddbaa
8 changed files with 120 additions and 151 deletions

View File

@@ -1,4 +1,4 @@
"""Unit tests: choose_template_cmd"""
"""Unit tests: choose_template_processor"""
import pytest
@@ -8,7 +8,7 @@ import pytest
def test_kind_default(runner, yadm, awk, label):
"""Test kind: default"""
expected = "template_default"
expected = "default"
awk_avail = "true"
if not awk:
@@ -21,7 +21,7 @@ def test_kind_default(runner, yadm, awk, label):
script = f"""
YADM_TEST=1 source {yadm}
function awk_available {{ {awk_avail}; }}
template="$(choose_template_cmd "{label}")"
template="$(choose_template_processor "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=["bash"], inp=script)
@@ -43,9 +43,9 @@ def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
j2cli_avail = "true" if j2cli else "false"
if label in ("j2cli", "j2") and j2cli:
expected = "template_j2cli"
expected = "j2cli"
elif label in ("envtpl", "j2") and envtpl:
expected = "template_envtpl"
expected = "envtpl"
else:
expected = ""
@@ -53,7 +53,7 @@ def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
YADM_TEST=1 source {yadm}
function envtpl_available {{ {envtpl_avail}; }}
function j2cli_available {{ {j2cli_avail}; }}
template="$(choose_template_cmd "{label}")"
template="$(choose_template_processor "{label}")"
echo "TEMPLATE:$template"
"""
run = runner(command=["bash"], inp=script)

View File

@@ -8,7 +8,7 @@ OCTAL = "7654"
NON_OCTAL = "9876"
@pytest.mark.parametrize("stat_broken", [True, False], ids=["normal", "stat broken"])
@pytest.mark.parametrize("stat_broken", [False, True], ids=["normal", "stat broken"])
def test_copy_perms(runner, yadm, tmpdir, stat_broken):
"""Test function copy_perms"""
src_mode = 0o754

View File

@@ -11,7 +11,7 @@ INIT_VARS = """
alt_scores=()
alt_targets=()
alt_sources=()
alt_template_cmds=()
alt_template_processors=()
"""
REPORT_RESULTS = """
@@ -19,7 +19,7 @@ REPORT_RESULTS = """
echo "SCORES:${alt_scores[@]}"
echo "TARGETS:${alt_targets[@]}"
echo "SOURCES:${alt_sources[@]}"
echo "TEMPLATE_CMDS:${alt_template_cmds[@]}"
echo "TEMPLATE_PROCESSORS:${alt_template_processors[@]}"
"""
@@ -39,7 +39,7 @@ def test_dont_record_zeros(runner, yadm):
assert "SCORES:\n" in run.out
assert "TARGETS:\n" in run.out
assert "SOURCES:\n" in run.out
assert "TEMPLATE_CMDS:\n" in run.out
assert "TEMPLATE_PROCESSORS:\n" in run.out
def test_new_scores(runner, yadm):
@@ -60,7 +60,7 @@ def test_new_scores(runner, yadm):
assert "SCORES:1 2 4\n" in run.out
assert "TARGETS:tgt_one tgt_two tgt_three\n" in run.out
assert "SOURCES:src_one src_two src_three\n" in run.out
assert "TEMPLATE_CMDS: \n" in run.out
assert "TEMPLATE_PROCESSORS: \n" in run.out
@pytest.mark.parametrize("difference", ["lower", "equal", "higher"])
@@ -84,7 +84,7 @@ def test_existing_scores(runner, yadm, difference):
alt_scores=(2)
alt_targets=("testtgt")
alt_sources=("existing_src")
alt_template_cmds=("")
alt_template_processors=("")
record_score "{score}" "testtgt" "new_src" ""
{REPORT_RESULTS}
"""
@@ -95,7 +95,7 @@ def test_existing_scores(runner, yadm, difference):
assert f"SCORES:{expected_score}\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert f"SOURCES:{expected_src}\n" in run.out
assert "TEMPLATE_CMDS:\n" in run.out
assert "TEMPLATE_PROCESSORS:\n" in run.out
def test_existing_template(runner, yadm):
@@ -107,7 +107,7 @@ def test_existing_template(runner, yadm):
alt_scores=(1)
alt_targets=("testtgt")
alt_sources=("src")
alt_template_cmds=("existing_template")
alt_template_processors=("existing_template")
record_score "2" "testtgt" "new_src" ""
{REPORT_RESULTS}
"""
@@ -118,7 +118,7 @@ def test_existing_template(runner, yadm):
assert "SCORES:1\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert "SOURCES:src\n" in run.out
assert "TEMPLATE_CMDS:existing_template\n" in run.out
assert "TEMPLATE_PROCESSORS:existing_template\n" in run.out
def test_config_first(runner, yadm):
@@ -130,7 +130,7 @@ def test_config_first(runner, yadm):
{INIT_VARS}
YADM_CONFIG={config}
record_score "1" "tgt_before" "src_before" ""
record_score "1" "tgt_tmp" "src_tmp" "cmd_tmp"
record_score "1" "tgt_tmp" "src_tmp" "processor_tmp"
record_score "2" "{config}" "src_config" ""
record_score "3" "tgt_after" "src_after" ""
{REPORT_RESULTS}
@@ -142,7 +142,7 @@ def test_config_first(runner, yadm):
assert "SCORES:2 1 1 3\n" in run.out
assert f"TARGETS:{config} tgt_before tgt_tmp tgt_after\n" in run.out
assert "SOURCES:src_config src_before src_tmp src_after\n" in run.out
assert "TEMPLATE_CMDS: cmd_tmp \n" in run.out
assert "TEMPLATE_PROCESSORS: processor_tmp \n" in run.out
def test_new_template(runner, yadm):
@@ -151,9 +151,9 @@ def test_new_template(runner, yadm):
script = f"""
YADM_TEST=1 source {yadm}
{INIT_VARS}
record_score 0 "tgt_one" "src_one" "cmd_one"
record_score 0 "tgt_two" "src_two" "cmd_two"
record_score 0 "tgt_three" "src_three" "cmd_three"
record_score 0 "tgt_one" "src_one" "processor_one"
record_score 0 "tgt_two" "src_two" "processor_two"
record_score 0 "tgt_three" "src_three" "processor_three"
{REPORT_RESULTS}
"""
run = runner(command=["bash"], inp=script)
@@ -163,7 +163,7 @@ def test_new_template(runner, yadm):
assert "SCORES:0 0 0\n" in run.out
assert "TARGETS:tgt_one tgt_two tgt_three\n" in run.out
assert "SOURCES:src_one src_two src_three\n" in run.out
assert "TEMPLATE_CMDS:cmd_one cmd_two cmd_three\n" in run.out
assert "TEMPLATE_PROCESSORS:processor_one processor_two processor_three\n" in run.out
def test_overwrite_existing_template(runner, yadm):
@@ -174,9 +174,9 @@ def test_overwrite_existing_template(runner, yadm):
{INIT_VARS}
alt_scores=(0)
alt_targets=("testtgt")
alt_template_cmds=("existing_cmd")
alt_template_processors=("existing_processor")
alt_sources=("existing_src")
record_score 0 "testtgt" "new_src" "new_cmd"
record_score 0 "testtgt" "new_src" "new_processor"
{REPORT_RESULTS}
"""
run = runner(command=["bash"], inp=script)
@@ -186,4 +186,4 @@ def test_overwrite_existing_template(runner, yadm):
assert "SCORES:0\n" in run.out
assert "TARGETS:testtgt\n" in run.out
assert "SOURCES:new_src\n" in run.out
assert "TEMPLATE_CMDS:new_cmd\n" in run.out
assert "TEMPLATE_PROCESSORS:new_processor\n" in run.out

View File

@@ -267,14 +267,14 @@ def test_score_values_templates(runner, yadm):
assert run.out == expected
@pytest.mark.parametrize("cmd_generated", [True, False], ids=["supported-template", "unsupported-template"])
def test_template_recording(runner, yadm, cmd_generated):
"""Template should be recorded if choose_template_cmd outputs a command"""
@pytest.mark.parametrize("processor_generated", [True, False], ids=["supported-template", "unsupported-template"])
def test_template_recording(runner, yadm, processor_generated):
"""Template should be recorded if choose_template_processor outputs a command"""
mock = "function choose_template_cmd() { return; }"
mock = "function choose_template_processor() { return; }"
expected = ""
if cmd_generated:
mock = 'function choose_template_cmd() { echo "test_cmd"; }'
if processor_generated:
mock = 'function choose_template_processor() { echo "test_processor"; }'
expected = "template recorded"
script = f"""

View File

@@ -231,7 +231,7 @@ def test_template_default(runner, yadm, tmpdir):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script, env={"VAR": ENV_VAR})
assert run.success
@@ -251,7 +251,7 @@ def test_source(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
set_awk
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
@@ -285,7 +285,7 @@ def test_include(runner, yadm, tmpdir):
set_awk
local_class="{LOCAL_CLASS}"
local_system="{LOCAL_SYSTEM}"
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
@@ -305,7 +305,7 @@ def test_nested_ifs(runner, yadm, tmpdir):
YADM_TEST=1 source {yadm}
set_awk
local_user="me"
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
@@ -323,7 +323,7 @@ def test_env(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
set_awk
template_default "{input_file}" "{output_file}"
template default "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success

View File

@@ -140,7 +140,7 @@ def test_template_esh(runner, yadm, tmpdir):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_esh "{input_file}" "{output_file}"
template esh "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
@@ -159,7 +159,7 @@ def test_source(runner, yadm, tmpdir):
script = f"""
YADM_TEST=1 source {yadm}
template_esh "{input_file}" "{output_file}"
template esh "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success

View File

@@ -146,7 +146,7 @@ def test_template_j2(runner, yadm, tmpdir, processor):
local_user="{LOCAL_USER}"
local_distro="{LOCAL_DISTRO}"
local_distro_family="{LOCAL_DISTRO_FAMILY}"
template_{processor} "{input_file}" "{output_file}"
template {processor} "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success
@@ -166,7 +166,7 @@ def test_source(runner, yadm, tmpdir, processor):
script = f"""
YADM_TEST=1 source {yadm}
template_{processor} "{input_file}" "{output_file}"
template {processor} "{input_file}" "{output_file}"
"""
run = runner(command=["bash"], inp=script)
assert run.success