Skip to content

Commit

Permalink
Refactor template handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erijo committed Dec 15, 2024
1 parent eb81978 commit 119d1dd
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Unit tests: choose_template_cmd"""
"""Unit tests: choose_template_processor"""

import pytest

Expand All @@ -8,7 +8,7 @@
def test_kind_default(runner, yadm, awk, label):
"""Test kind: default"""

expected = "template_default"
expected = "default"
awk_avail = "true"

if not awk:
Expand All @@ -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)
Expand All @@ -43,17 +43,17 @@ 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 = ""

script = f"""
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)
Expand Down
2 changes: 1 addition & 1 deletion test/test_unit_copy_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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
Expand Down
34 changes: 17 additions & 17 deletions test/test_unit_record_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
alt_scores=()
alt_targets=()
alt_sources=()
alt_template_cmds=()
alt_template_processors=()
"""

REPORT_RESULTS = """
echo "SIZE:${#alt_scores[@]}"
echo "SCORES:${alt_scores[@]}"
echo "TARGETS:${alt_targets[@]}"
echo "SOURCES:${alt_sources[@]}"
echo "TEMPLATE_CMDS:${alt_template_cmds[@]}"
echo "TEMPLATE_PROCESSORS:${alt_template_processors[@]}"
"""


Expand All @@ -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):
Expand All @@ -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"])
Expand All @@ -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}
"""
Expand All @@ -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):
Expand All @@ -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}
"""
Expand All @@ -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):
Expand All @@ -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}
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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
12 changes: 6 additions & 6 deletions test/test_unit_score_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
10 changes: 5 additions & 5 deletions test/test_unit_template_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/test_unit_template_esh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/test_unit_template_j2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 119d1dd

Please sign in to comment.