summaryrefslogtreecommitdiffstats
path: root/test/t/unit/test_unit_expand_tilde.py
blob: 3552fd63f95af3d78e6a5cf8a2bcdc39fed4550a (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
import pytest

from conftest import assert_bash_exec


@pytest.mark.bashcomp(cmd=None)
class TestUnitExpandTilde:
    def test_1(self, bash):
        """The old interface `__expand_tilde_by_ref` should not fail when it is
        called without arguments"""
        assert_bash_exec(bash, "__expand_tilde_by_ref >/dev/null")

    def test_2(self, bash):
        """Test environment non-pollution, detected at teardown."""
        assert_bash_exec(
            bash,
            '_x() { local REPLY; _comp_expand_tilde "~"; }; _x; unset -f _x',
        )

    @pytest.fixture(scope="class")
    def functions(self, bash):
        # $HOME tinkering: protect against $HOME != ~user; our "home" is the
        # latter but plain_tilde follows $HOME
        assert_bash_exec(
            bash,
            '_comp__test_unit() { local REPLY HOME=$1; _comp_expand_tilde "$2"; printf "%s\\n" "$REPLY"; }',
        )

    @pytest.mark.parametrize("plain_tilde", (True, False))
    @pytest.mark.parametrize(
        "suffix_expanded",
        (
            ("", True),
            ("/foo", True),
            (r"/\$HOME", True),
            ("/a  b", True),
            ("/*", True),
            (";echo hello", False),
            ("/a;echo hello", True),
        ),
    )
    def test_expand(
        self, bash, user_home, plain_tilde, suffix_expanded, functions
    ):
        user, home = user_home
        suffix, expanded = suffix_expanded
        home2 = home
        if plain_tilde:
            user = ""
            if not suffix or not expanded:
                home2 = "~"
        elif not expanded:
            home2 = "~%s" % user
        output = assert_bash_exec(
            bash,
            r'_comp__test_unit "%s" "~%s%s"' % (home, user, suffix),
            want_output=True,
        )
        assert output.strip() == "%s%s" % (home2, suffix.replace(r"\$", "$"))