def test_collect_shell_variables_from_text_can_resolve(self):
        text = '''
foo=bar
baz=$foo
bez=${baz}
'''
        result, errors = bashparse.collect_shell_variables_from_text(text)
        expected = [
            ShellVariable(name='foo', value='bar'),
            ShellVariable(name='baz', value='$foo'),
            ShellVariable(name='bez', value='${baz}'),
        ]
        assert result == expected

        expected = []
        assert errors == expected

        result, errors = bashparse.collect_shell_variables_from_text(
            text, resolve=True)
        expected = [
            ShellVariable(name='foo', value='bar'),
            ShellVariable(name='baz', value='bar'),
            ShellVariable(name='bez', value='bar'),
        ]
        assert result == expected

        expected = []
        assert errors == expected
 def test_collect_shell_variables_from_text_can_parse_trailing_comments(
         self):
     text = '\noptions="!check" # out of disk space (>35GB)'
     result, errors = bashparse.collect_shell_variables_from_text(text)
     expected = [ShellVariable(name='options', value='!check')]
     assert result == expected
     expected = []
     assert errors == expected
 def test_collect_shell_variables_from_text_can_parse_single_quoted_vars(
         self):
     text = "\nlicense='AGPL3'"
     result, errors = bashparse.collect_shell_variables_from_text(text)
     expected = [ShellVariable(name='license', value='AGPL3')]
     assert result == expected
     expected = []
     assert errors == expected
 def test_collect_shell_variables_from_text_can_parse_var_at_start(self):
     text = "foo=bar"
     result, errors = bashparse.collect_shell_variables_from_text(text)
     expected = [
         ShellVariable(name='foo', value='bar'),
     ]
     assert result == expected
     expected = []
     assert errors == expected
 def test_collect_shell_variables_from_text_can_parse_empty_var(self):
     text = "foo=\nbar=baz\nlicense=\n"
     result, errors = bashparse.collect_shell_variables_from_text(text)
     expected = [
         ShellVariable(name='foo', value=''),
         ShellVariable(name='bar', value='baz'),
         ShellVariable(name='license', value=''),
     ]
     assert result == expected
     expected = []
     assert errors == expected
 def test_collect_shell_variables_from_text_simple(self):
     result, errors = bashparse.collect_shell_variables_from_text(
         TEST_TEXT1)
     expected = [
         ShellVariable(name='pkgname', value='gcc'),
         ShellVariable(name='_pkgbase', value='10.3.1'),
         ShellVariable(name='pkgver', value='10.3.1_git20210424'),
         ShellVariable(name='pkgname', value='$pkgname$_target'),
         ShellVariable(name='pkgrel', value='2'),
         ShellVariable(name='pkgdesc', value='The GNU Compiler Collection'),
         ShellVariable(name='url', value='https://gcc.gnu.org'),
         ShellVariable(name='arch', value='all'),
         ShellVariable(name='license',
                       value='GPL-2.0-or-later LGPL-2.1-or-later'),
         ShellVariable(name='_gccrel', value='$pkgver-r$pkgrel'),
         ShellVariable(name='depends', value='binutils$_target'),
         ShellVariable(
             name='makedepends_build',
             value=
             'gcc$_cross g++$_cross bison flex texinfo gawk zip gmp-dev mpfr-dev mpc1-dev zlib-dev'
         ),
         ShellVariable(
             name='makedepends_host',
             value=
             'linux-headers gmp-dev mpfr-dev mpc1-dev isl-dev zlib-dev !gettext-dev libucontext-dev'
         ),
         ShellVariable(name='makedepends',
                       value='$makedepends_build $makedepends_host'),
         ShellVariable(
             name='source',
             value=
             'https://dev.alpinelinux.org/~nenolod/gcc-${pkgver}.tar.xz\n    0001-posix_memalign.patch\n    '
         ),
         ShellVariable(
             name='sha512sums',
             value=
             '0ef281e6633b8bef7ce24d1448ec7b96aef66e414f90821a9  gcc-10.3.1_git20210424.tar.xz\nd1e10db83a04c02d99f9f6ce03f9  0001-posix_memalign.patch\n'
         ),
     ]
     assert result == expected
     assert errors == [
         "Duplicate variable name: 'pkgname' value: '$pkgname$_target' existing value: 'gcc'"
     ]
    def test_collect_shell_variables_from_text_filters_on_needed_variables(
            self):
        text = '''
arch="x86_64"
options="foo"
license='AGPL3'
options=duplicate
'''

        result, errors = bashparse.collect_shell_variables_from_text(
            text=text,
            needed_variables=set(['license']),
        )
        expected = [
            ShellVariable(name='license', value='AGPL3'),
        ]
        assert result == expected
        expected = []
        assert errors == expected
    def test_collect_shell_variables_from_text_can_parse_combo_single_quote_and_trailing_comments(
            self):
        text = '''
arch="x86_64 ppc64le aarch64"
options="!check" # out of disk space (>35GB)
license='AGPL3'
pkgusers="mongodb"
option2s='!baz' # out of disk space (>35GB)
options=duplicate
'''
        result, errors = bashparse.collect_shell_variables_from_text(text)
        expected = [
            ShellVariable(name='arch', value='x86_64 ppc64le aarch64'),
            ShellVariable(name='options', value='!check'),
            ShellVariable(name='license', value='AGPL3'),
            ShellVariable(name='pkgusers', value='mongodb'),
            ShellVariable(name='option2s', value='!baz'),
            ShellVariable(name='options', value='duplicate'),
        ]
        assert result == expected
        expected = [
            "Duplicate variable name: 'options' value: 'duplicate' existing value: '!check'"
        ]
        assert errors == expected