aboutsummaryrefslogtreecommitdiff
blob: 89d91f5c2b3705c413893530571ec3cb2620b0af (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import io
import textwrap

import pytest
from pkgcore.ebuild.filter_env import main_run


class TestFilterEnv:

    def get_output(self, raw_data, funcs=None, vars=None, preserve_funcs=False,
                   preserve_vars=False, debug=False, global_envvar_callback=None):
        out = io.BytesIO()
        if funcs:
            funcs = funcs.split(',')
        if vars:
            vars = vars.split(',')
        main_run(out, raw_data, vars, funcs, preserve_vars, preserve_funcs,
            global_envvar_callback=global_envvar_callback)
        return out.getvalue().decode('utf-8')

    def test_function_foo(self):
        ret = ''.join(self.get_output("function foo() {:;}", funcs="foo"))
        assert ret == ''
        ret = ''.join(self.get_output("functionfoo() {:;}", funcs="foo"))
        assert ret == 'functionfoo() {:;}'

    def test_simple(self):
        data = textwrap.dedent("""\
            foo() {
                :
            }

            bar() {
                :
            }
        """)
        ret = ''.join(self.get_output(data))
        assert 'foo' in ret
        assert 'bar' in ret
        ret = ''.join(self.get_output(data, funcs='foo'))
        assert 'foo' not in ret
        assert 'bar' in ret
        ret = ''.join(self.get_output(data, funcs='bar'))
        assert 'foo' in ret
        assert 'bar' not in ret
        ret = ''.join(self.get_output(data, funcs='bar,foo'))
        assert 'foo' not in ret
        assert 'bar' not in ret

    def test1(self):
        data = textwrap.dedent("""\
            MODULE_NAMES=${MODULE_NAMES//${i}(*};
            tc-arch ()
            {
                tc-ninja_magic_to_arch portage $@
            }
        """)
        ret = ''.join(self.get_output(data, vars='MODULE_NAMES'))
        assert 'MODULE_NAMES' not in ret
        assert 'tc-arch' in ret

    def test_comments(self):
        data = "dar=${yar##.%}\nfoo() {\n:\n}\n"
        ret = ''.join(self.get_output(data, vars='dar'))
        assert 'dar' not in ret
        assert 'foo' in ret

        data = textwrap.dedent("""\
            src_unpack() {
                use idn && {
                # BIND 9.4.0 doesn't have this patch
                :
                }
            }

            src_compile() {
                :
            }
        """)
        assert 'src_unpack' in ''.join(self.get_output(data, funcs='src_compile'))
        ret = ''.join(self.get_output(data, funcs='src_unpack'))
        assert 'src_compile' in ret
        assert 'src_unpack' not in ret
        data = textwrap.dedent("""\
            src_install() {
                local -f ${f##*=}
            }

            pkg_postinst() {
                :
            }
        """)
        assert 'pkg_postinst' not in ''.join(self.get_output(data, funcs='pkg_postinst'))
        data = textwrap.dedent("""\
            src_unpack() {
                fnames=$(scanelf -pyqs__uClibc_start_main -F%F#s)
            }
            src_compile() {
                :
            }
        """)
        assert 'src_compile' in ''.join(self.get_output(data, funcs='src_unpack'))

        data = textwrap.dedent("""\
            findtclver() {
                [ "$(#i)" = "3" ]
            }

            pkg_setup() {
                :
            }
        """)
        assert 'pkg_setup' in ''.join(self.get_output(data, funcs='findtclver'))

    def test_here(self):
        data = textwrap.dedent("""\
            src_install() {
                cat >${D}/etc/modules.d/davfs2 <<EOF
            alias char-major-67 coda
            alias /dev/davfs*   coda
            EOF
            }

            pkg_setup() {
                :
            }
        """)
        assert 'pkg_setup' not in ''.join(self.get_output(data, funcs='pkg_setup'))

        data = textwrap.dedent("""\
            pkg_setup() {
                    while read line; do elog "${line}"; done <<EOF
            The default behaviour of tcsh has significantly changed starting from
            version 6.14-r1.  In contrast to previous ebuilds, the amount of
            customisation to the default shell's behaviour has been reduced to a
            bare minimum (a customised prompt).
            If you rely on the customisations provided by previous ebuilds, you will
            have to copy over the relevant (now commented out) parts to your own
            ~/.tcshrc.  Please check all tcsh-* files in
            /usr/share/doc/${P}/examples/ and include their behaviour in your own
            configuration files.
            The tcsh-complete file is not any longer sourced by the default system
            scripts.
            EOF
            }

            pkg_foo() {
                :
            }
        """)
        assert 'pkg_foo' not in ''.join(self.get_output(data, funcs='pkg_foo'))

    def test_vars(self):
        data = textwrap.dedent("""\
            f() {
                x=$y
            }

            z() {
                :
            }
        """)
        assert 'z' in ''.join(self.get_output(data, funcs='f'))

        data = textwrap.dedent("""\
            f() {
                x="${y}"
            }

            z() {
                :
            }
        """)
        assert 'z' in ''.join(self.get_output(data, funcs='f'))

        data = textwrap.dedent("""\
            src_compile() {
                $(ABI=foo get_libdir)
            }

            pkg_setup() {
                :
            }
        """)
        assert 'pkg_setup' in ''.join(self.get_output(data, funcs='src_compile'))

    def test_quoting(self):
        data = textwrap.dedent("""\
            pkg_postinst() {
                einfo " /bin/ls ${ROOT}etc/init.d/net.* | grep -v '/net.lo$' | xargs -n1 ln -sfvn net.lo"
            }

            pkg_setup() {
                :
            }
        """)
        assert 'pkg_setup' in ''.join(self.get_output(data, funcs='pkg_postinst'))

        data = textwrap.dedent("""\
            src_unpack() {
                testExp=$'\177\105\114\106\001\001\001'
            }

            src_install() {
                :
            }
        """)
        assert 'src_install' in ''.join(self.get_output(data, funcs='src_unpack'))

    def test_arg_awareness(self):
        data = "f() {\n x \\{}\n}\n"
        assert '}' not in ''.join(self.get_output(data, 'f'))

    @pytest.mark.parametrize(("data", "var_list"), (
        ("f(){\nX=dar\n}", set()),
        ("f(){\nX=dar\n}\nY=a", {'Y'}),
        ("f(){\nX=dar\n}\nmy command\nY=a\nf=$(dar)", {'Y', 'f'}),
        ("f(){\nX=dar\n}\nmy command\nY=a\nf=$(dar) foon\n", {'Y', 'f'}),
        ("f(){\nX=dar foon\n}\nY=dar\nf2(){Z=dar;}\n", {'Y'})
    ))
    def test_print_vars(self, data, var_list):
        l = set()
        self.get_output(data, global_envvar_callback=l.add)
        assert var_list == l