18_test_Config.py 5.61 KB
Newer Older
1 2 3 4
# vim: set fileencoding=utf-8 :

import os
import unittest
5
from gbp.config import GbpOptionParser, GbpOptionGroup
6
from .testutils import GbpLogTester
7

Guido Günther's avatar
Guido Günther committed
8

9 10 11 12
class TestConfigParser(unittest.TestCase, GbpLogTester):
    def __init__(self, methodName='runTest'):
        unittest.TestCase.__init__(self, methodName)
        GbpLogTester.__init__(self)
13 14 15 16 17 18

    def setUp(self):
        self.conffiles_save = os.environ.get('GBP_CONF_FILES')
        self.confname = 'tests/data/test1.conf'
        self.assertTrue(os.stat(self.confname))
        os.environ['GBP_CONF_FILES'] = self.confname
19
        self._capture_log(True)
20 21 22 23

    def tearDown(self):
        if self.conffiles_save:
            os.environ['GBP_CONF_FILES'] = self.conffiles_save
24
        self._capture_log(False)
25 26 27 28 29

    def test_default(self):
        """
        A value only in the default section should be available in all commands
        """
Guido Günther's avatar
Guido Günther committed
30 31
        for n in range(1, 5):
            for prefix in ['', 'git-', 'gbp-']:
32
                parser = GbpOptionParser('%scmd%d' % (prefix, n))
33 34 35 36 37 38
                self.assertEqual(parser.config['default_option'], 'default_default1')

    def test_single_override(self):
        """
        A value in any command section should override the default
        """
Guido Günther's avatar
Guido Günther committed
39
        for prefix in ['', 'git-', 'gbp-']:
40 41
            parser = GbpOptionParser('%scmd1' % prefix)
            self.assertEqual(parser.config['single_override_option1'], 'single_override_value1')
42 43
        # No deprecation warning since the test1.conf section is [cmd1]
        self._check_log_empty()
44 45 46 47 48

    def test_single_git_override(self):
        """
        A value in any git-command section should override the default
        """
Guido Günther's avatar
Guido Günther committed
49
        for prefix in ['', 'git-']:
50 51
            parser = GbpOptionParser('%scmd2' % prefix)
            self.assertEqual(parser.config['single_git_override_option1'], 'single_git_override_value1')
Guido Günther's avatar
Guido Günther committed
52
        for line in range(0, 2):
53
            self._check_log(line, r'.*Old style config section \[git-cmd2\] found please rename to \[cmd2\]')
54 55 56 57 58

    def test_single_gbp_override(self):
        """
        A value in any gbp-command section should override the default
        """
Guido Günther's avatar
Guido Günther committed
59
        for prefix in ['', 'gbp-']:
60 61
            parser = GbpOptionParser('%scmd3' % prefix)
            self.assertEqual(parser.config['single_gbp_override_option1'], 'single_gbp_override_value1')
Guido Günther's avatar
Guido Günther committed
62
        for line in range(0, 2):
63
            self._check_log(line, r'.*Old style config section \[gbp-cmd3\] found please rename to \[cmd3\]')
64

65 66 67 68 69
    def test_single_git_override_disabled_deprecations(self):
        """
        With disabled deprecations we shouldn't see a log line
        """
        for prefix in ['', 'git-']:
70
            os.environ['GBP_DISABLE_SECTION_DEPRECATION'] = 'true'
71 72 73 74
            parser = GbpOptionParser('%scmd2' % prefix)
            self.assertEqual(parser.config['single_git_override_option1'], 'single_git_override_value1')
        for line in range(0, 2):
            self._check_log_empty()
75
        os.environ.pop('GBP_DISABLE_SECTION_DEPRECATION')
76 77 78 79 80 81 82

    def test_new_overrides_git(self):
        """
        A value in the cmd section should override the old git-cmd section independent from
        how we're invoked
        """
        for n in range(4, 6):
Guido Günther's avatar
Guido Günther committed
83
            for prefix in ['', 'git-']:
84 85 86 87 88
                cmd = '%scmd%d' % (prefix, n)
                parser = GbpOptionParser(cmd)
                actual = parser.config['new_overrides_git_option1']
                expected = 'new_overrides_git_value1'
                self.assertEqual(actual, expected, "%s != %s for %s" % (actual, expected, cmd))
89 90 91

    def test_get_config_file_value(self):
        """
92
        Read a single value from the parsed config
93 94 95 96 97
        """
        parser = GbpOptionParser('cmd4')
        self.assertEqual(parser.get_config_file_value('new_overrides_git_option1'),
                         'new_overrides_git_value1')
        self.assertEqual(parser.get_config_file_value('doesnotexist'), None)
98 99 100 101 102 103 104 105 106 107 108 109 110 111

    def test_param_list(self):
        parser = GbpOptionParser('cmd4')

        branch_group = GbpOptionGroup(parser, "branch options", "branch update and layout options")
        parser.add_option_group(branch_group)
        branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
        branch_group.add_config_file_option("debian-branch", dest="upstream_branch")
        parser.add_config_file_option(option_name="color", dest="color", type='tristate')

        params = parser.valid_options
        self.assertTrue('upstream-branch' in params)
        self.assertTrue('debian-branch' in params)
        self.assertTrue('color' in params)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    def test_short_option_with_prefix(self):
        """Options with short options can't have a prefix"""
        class TestOptonParser(GbpOptionParser):
            list_opts = []
            defaults = {'withshort': 'foo'}
            short_opts = {'withshort': '-S'}
        parser = TestOptonParser('cmd', prefix='p')
        with self.assertRaisesRegexp(ValueError, "Options with prefix cannot have a short option"):
            parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo")

    def test_short_option(self):
        class TestOptionParser(GbpOptionParser):
            list_opts = []
            defaults = {'withshort': 'foo'}
            short_opts = {'withshort': '-S'}

        parser = TestOptionParser('cmd')
        parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo")
131 132
        self.assertEquals('withshort', parser.valid_options[0])
        self.assertEquals(len(parser.valid_options), 1)
133 134
        self.assertTrue(parser.has_option("--withshort"))
        self.assertTrue(parser.has_option("-S"))