1import pytest
2
3from conftest import assert_bash_exec
4
5
6@pytest.mark.bashcomp(cmd=None, ignore_env=r"^[+-]var=")
7class TestUnitExpandTildeByRef:
8    def test_1(self, bash):
9        assert_bash_exec(bash, "__expand_tilde_by_ref >/dev/null")
10
11    def test_2(self, bash):
12        """Test environment non-pollution, detected at teardown."""
13        assert_bash_exec(
14            bash,
15            '_x() { local aa="~"; __expand_tilde_by_ref aa; }; _x; unset _x',
16        )
17
18    @pytest.mark.parametrize("plain_tilde", (True, False))
19    @pytest.mark.parametrize(
20        "suffix_expanded",
21        (
22            ("", True),
23            ("/foo", True),
24            (r"/\$HOME", True),
25            ("/a  b", True),
26            ("/*", True),
27            (";echo hello", False),
28            ("/a;echo hello", True),
29        ),
30    )
31    def test_expand(self, bash, user_home, plain_tilde, suffix_expanded):
32        user, home = user_home
33        suffix, expanded = suffix_expanded
34        if plain_tilde:
35            user = ""
36            if not suffix or not expanded:
37                home = "~"
38        elif not expanded:
39            home = "~%s" % user
40        output = assert_bash_exec(
41            bash,
42            r'var="~%s%s"; __expand_tilde_by_ref var; printf "%%s\n" "$var"'
43            % (user, suffix),
44            want_output=True,
45        )
46        assert output.strip() == "%s%s" % (home, suffix.replace(r"\$", "$"),)
47