summaryrefslogtreecommitdiffstats
path: root/tests/test_substitute.py
blob: 81eb2e05c0aff1b67a4a9e64186e3e86cb1310b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pytest

from debputy.architecture_support import faked_arch_table
from debputy.commands.debputy_cmd.output import no_fancy_output
from debputy.dh_migration.models import (
    DHMigrationSubstitution,
    AcceptableMigrationIssues,
    FeatureMigration,
)
from debputy.filesystem_scan import FSROOverlay
from debputy.highlevel_manifest import MutableYAMLManifest
from debputy.substitution import SubstitutionImpl, VariableContext

MOCK_ENV = {
    # This conflicts with the dpkg arch table intentionally (to ensure we can tell which one is being resolved)
    "DEB_HOST_ARCHITECTURE": "i386",
}
MOCK_DPKG_ARCH_TABLE = faked_arch_table("amd64", build_arch="i386")
MOCK_VARIABLE_CONTEXT = VariableContext(FSROOverlay.create_root_dir("debian", "debian"))


@pytest.mark.parametrize(
    "value,expected",
    [
        (
            "unchanged",
            "unchanged",
        ),
        (
            "unchanged\\{{\n}}",
            "unchanged\\{{\n}}",
        ),  # Newline is not an allowed part of a substitution
        (
            "{{token:DOUBLE_OPEN_CURLY_BRACE}}{{token:NL}}{{token:DOUBLE_CLOSE_CURLY_BRACE}}",
            "{{\n}}",
        ),
        (
            "{{token:DOUBLE_OPEN_CURLY_BRACE}}token:TAB}}{{token:TAB{{token:DOUBLE_CLOSE_CURLY_BRACE}}",
            "{{token:TAB}}{{token:TAB}}",
        ),
        (
            "/usr/lib/{{DEB_HOST_MULTIARCH}}",
            f'/usr/lib/{MOCK_DPKG_ARCH_TABLE["DEB_HOST_MULTIARCH"]}',
        ),
    ],
)
def test_substitution_match(debputy_plugin_feature_set, value, expected) -> None:
    subst = SubstitutionImpl(
        plugin_feature_set=debputy_plugin_feature_set,
        dpkg_arch_table=MOCK_DPKG_ARCH_TABLE,
        environment=MOCK_ENV,
        variable_context=MOCK_VARIABLE_CONTEXT,
    )
    replacement = subst.substitute(value, "test def")
    assert replacement == expected


def test_migrate_substitution() -> None:
    feature_migration = FeatureMigration("test migration", no_fancy_output())
    subst = DHMigrationSubstitution(
        MOCK_DPKG_ARCH_TABLE,
        AcceptableMigrationIssues(frozenset()),
        feature_migration,
        MutableYAMLManifest({}),
    )
    replacement = subst.substitute("usr/lib/${DEB_HOST_MULTIARCH}/foo", "test def")
    assert replacement == "usr/lib/{{DEB_HOST_MULTIARCH}}/foo"