porting code to python3.x with os patch
[tools/git-buildpackage.git] / tests / 18_test_Config.py
1 # vim: set fileencoding=utf-8 :
2
3 import os
4
5 import unittest
6 from gbp.config import GbpOptionParser, GbpOptionGroup
7 from . import context
8
9 class TestConfigParser(unittest.TestCase):
10     def setUp(self):
11         self.conffiles_save = os.environ.get('GBP_CONF_FILES')
12         self.confname = 'tests/data/test1.conf'
13         self.assertTrue(os.stat(self.confname))
14         os.environ['GBP_CONF_FILES'] = self.confname
15
16     def tearDown(self):
17         if self.conffiles_save:
18             os.environ['GBP_CONF_FILES'] = self.conffiles_save
19
20     def test_default(self):
21         """
22         A value only in the default section should be available in all commands
23         """
24         for n in range(1,5):
25             for prefix in [ '', 'git-', 'gbp-' ]:
26                 parser = GbpOptionParser('cmd%d' % n)
27                 self.assertEqual(parser.config['default_option'], 'default_default1')
28
29     def test_single_override(self):
30         """
31         A value in any command section should override the default
32         """
33         for prefix in [ '', 'git-', 'gbp-' ]:
34             parser = GbpOptionParser('%scmd1' % prefix)
35             self.assertEqual(parser.config['single_override_option1'], 'single_override_value1')
36
37     def test_single_git_override(self):
38         """
39         A value in any git-command section should override the default
40         """
41         for prefix in [ '', 'git-' ]:
42             parser = GbpOptionParser('%scmd2' % prefix)
43             self.assertEqual(parser.config['single_git_override_option1'], 'single_git_override_value1')
44
45     def test_single_gbp_override(self):
46         """
47         A value in any gbp-command section should override the default
48         """
49         for prefix in [ '', 'gbp-' ]:
50             parser = GbpOptionParser('%scmd3' % prefix)
51             self.assertEqual(parser.config['single_gbp_override_option1'], 'single_gbp_override_value1')
52         # FIXME: for all prefixes
53
54     def test_new_overrides_git(self):
55         """
56         A value in the cmd section should override the old git-cmd section independent from
57         how we're invoked
58         """
59         for n in range(4, 6):
60             for prefix in [ '', 'git-']:
61                 cmd = '%scmd%d' % (prefix, n)
62                 parser = GbpOptionParser(cmd)
63                 actual = parser.config['new_overrides_git_option1']
64                 expected = 'new_overrides_git_value1'
65                 self.assertEqual(actual, expected, "%s != %s for %s" % (actual, expected, cmd))
66
67     def test_get_config_file_value(self):
68         """
69         Read a single value from the parse config
70         """
71         parser = GbpOptionParser('cmd4')
72         self.assertEqual(parser.get_config_file_value('new_overrides_git_option1'),
73                          'new_overrides_git_value1')
74         self.assertEqual(parser.get_config_file_value('doesnotexist'), None)
75
76     def test_param_list(self):
77         parser = GbpOptionParser('cmd4')
78
79         branch_group = GbpOptionGroup(parser, "branch options", "branch update and layout options")
80         parser.add_option_group(branch_group)
81         branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
82         branch_group.add_config_file_option("debian-branch", dest="upstream_branch")
83         parser.add_config_file_option(option_name="color", dest="color", type='tristate')
84
85         params = parser.valid_options
86         self.assertTrue('upstream-branch' in params)
87         self.assertTrue('debian-branch' in params)
88         self.assertTrue('color' in params)