1# -*- coding: utf-8 -*-
2
3# Copyright (C) 2014-2018 Red Hat, Inc.
4#
5# This copyrighted material is made available to anyone wishing to use,
6# modify, copy, or redistribute it subject to the terms and conditions of
7# the GNU General Public License v.2, or (at your option) any later version.
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY expressed or implied, including the implied warranties of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11# Public License for more details.  You should have received a copy of the
12# GNU General Public License along with this program; if not, write to the
13# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
15# source code or documentation are not subject to the GNU General Public
16# License and may only be used or replicated with the express permission of
17# Red Hat, Inc.
18#
19
20from __future__ import absolute_import
21from __future__ import unicode_literals
22
23import dnf.conf
24from libdnf.conf import ConfigParser
25
26import tests.support
27
28substitute = ConfigParser.substitute
29
30
31class ParserTest(tests.support.TestCase):
32    def test_substitute(self):
33        substs = {'lies': 'fact'}
34        # Test a single word without braces
35        rawstr = '$Substitute some $lies.'
36        result = '$Substitute some fact.'
37        self.assertEqual(substitute(rawstr, substs), result)
38        # And with braces
39        rawstr = '$Substitute some ${lies}.'
40        self.assertEqual(substitute(rawstr, substs), result)
41
42        # Test a word with braces without space
43        rawstr = '$Substitute some ${lies}withoutspace.'
44        result = '$Substitute some factwithoutspace.'
45        self.assertEqual(substitute(rawstr, substs), result)
46
47        # Tests a single brace before (no substitution)
48        rawstr = '$Substitute some ${lieswithoutspace.'
49        result = '$Substitute some ${lieswithoutspace.'
50        self.assertEqual(substitute(rawstr, substs), result)
51
52        # Tests a single brace after (substitution and leave the brace)
53        rawstr = '$Substitute some $lies}withoutspace.'
54        result = '$Substitute some fact}withoutspace.'
55        self.assertEqual(substitute(rawstr, substs), result)
56
57    def test_empty_option(self):
58        # Parser is able to read config file with option without value
59        FN = tests.support.resource_path('etc/empty_option.conf')
60        conf = dnf.conf.Conf()
61        conf.config_file_path = FN
62        conf.read()
63        self.assertEqual(conf.reposdir, '')
64