2024-12-15 09:10:02 -05:00
|
|
|
"""Unit tests: choose_template_processor"""
|
2024-11-11 16:30:41 -05:00
|
|
|
|
2019-10-01 09:12:18 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("label", ["", "default", "other"])
|
|
|
|
@pytest.mark.parametrize("awk", [True, False], ids=["awk", "no-awk"])
|
2019-10-30 18:29:17 -04:00
|
|
|
def test_kind_default(runner, yadm, awk, label):
|
|
|
|
"""Test kind: default"""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2024-12-15 09:10:02 -05:00
|
|
|
expected = "default"
|
2023-07-10 15:43:17 -04:00
|
|
|
awk_avail = "true"
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
if not awk:
|
2023-07-10 15:43:17 -04:00
|
|
|
awk_avail = "false"
|
|
|
|
expected = ""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if label == "other":
|
|
|
|
expected = ""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2024-11-11 16:30:41 -05:00
|
|
|
function awk_available {{ {awk_avail}; }}
|
2024-12-15 09:10:02 -05:00
|
|
|
template="$(choose_template_processor "{label}")"
|
2019-10-01 09:12:18 -04:00
|
|
|
echo "TEMPLATE:$template"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert f"TEMPLATE:{expected}\n" in run.out
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("label", ["envtpl", "j2cli", "j2", "other"])
|
|
|
|
@pytest.mark.parametrize("envtpl", [True, False], ids=["envtpl", "no-envtpl"])
|
|
|
|
@pytest.mark.parametrize("j2cli", [True, False], ids=["j2cli", "no-j2cli"])
|
2019-10-01 09:12:18 -04:00
|
|
|
def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
|
|
|
|
"""Test kind: j2 (both j2cli & envtpl)
|
|
|
|
|
|
|
|
j2cli is preferred over envtpl if available.
|
|
|
|
"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
envtpl_avail = "true" if envtpl else "false"
|
|
|
|
j2cli_avail = "true" if j2cli else "false"
|
2019-10-01 09:12:18 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if label in ("j2cli", "j2") and j2cli:
|
2024-12-15 09:10:02 -05:00
|
|
|
expected = "j2cli"
|
2023-07-10 15:43:17 -04:00
|
|
|
elif label in ("envtpl", "j2") and envtpl:
|
2024-12-15 09:10:02 -05:00
|
|
|
expected = "envtpl"
|
2019-10-01 09:12:18 -04:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = ""
|
2019-10-01 09:12:18 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2024-11-11 16:30:41 -05:00
|
|
|
function envtpl_available {{ {envtpl_avail}; }}
|
|
|
|
function j2cli_available {{ {j2cli_avail}; }}
|
2024-12-15 09:10:02 -05:00
|
|
|
template="$(choose_template_processor "{label}")"
|
2019-10-01 09:12:18 -04:00
|
|
|
echo "TEMPLATE:$template"
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-01 09:12:18 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert f"TEMPLATE:{expected}\n" in run.out
|