1"""Unit tests: choose_template_cmd"""
2import pytest
3
4
5@pytest.mark.parametrize('label', ['', 'default', 'other'])
6@pytest.mark.parametrize('awk', [True, False], ids=['awk', 'no-awk'])
7def test_kind_default(runner, yadm, awk, label):
8    """Test kind: default"""
9
10    expected = 'template_default'
11    awk_avail = 'true'
12
13    if not awk:
14        awk_avail = 'false'
15        expected = ''
16
17    if label == 'other':
18        expected = ''
19
20    script = f"""
21        YADM_TEST=1 source {yadm}
22        function awk_available {{ { awk_avail}; }}
23        template="$(choose_template_cmd "{label}")"
24        echo "TEMPLATE:$template"
25    """
26    run = runner(command=['bash'], inp=script)
27    assert run.success
28    assert run.err == ''
29    assert f'TEMPLATE:{expected}\n' in run.out
30
31
32@pytest.mark.parametrize('label', ['envtpl', 'j2cli', 'j2', 'other'])
33@pytest.mark.parametrize('envtpl', [True, False], ids=['envtpl', 'no-envtpl'])
34@pytest.mark.parametrize('j2cli', [True, False], ids=['j2cli', 'no-j2cli'])
35def test_kind_j2cli_envtpl(runner, yadm, envtpl, j2cli, label):
36    """Test kind: j2 (both j2cli & envtpl)
37
38    j2cli is preferred over envtpl if available.
39    """
40
41    envtpl_avail = 'true' if envtpl else 'false'
42    j2cli_avail = 'true' if j2cli else 'false'
43
44    if label in ('j2cli', 'j2') and j2cli:
45        expected = 'template_j2cli'
46    elif label in ('envtpl', 'j2') and envtpl:
47        expected = 'template_envtpl'
48    else:
49        expected = ''
50
51    script = f"""
52        YADM_TEST=1 source {yadm}
53        function envtpl_available {{ { envtpl_avail}; }}
54        function j2cli_available {{ { j2cli_avail}; }}
55        template="$(choose_template_cmd "{label}")"
56        echo "TEMPLATE:$template"
57    """
58    run = runner(command=['bash'], inp=script)
59    assert run.success
60    assert run.err == ''
61    assert f'TEMPLATE:{expected}\n' in run.out
62