18_test_Config.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # vim: set fileencoding=utf-8 :
  2. import os
  3. import unittest
  4. from gbp.config import GbpOptionParser, GbpOptionGroup
  5. from .testutils import GbpLogTester
  6. class TestConfigParser(unittest.TestCase, GbpLogTester):
  7. def __init__(self, methodName='runTest'):
  8. unittest.TestCase.__init__(self, methodName)
  9. GbpLogTester.__init__(self)
  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. self._capture_log(True)
  16. def tearDown(self):
  17. if self.conffiles_save:
  18. os.environ['GBP_CONF_FILES'] = self.conffiles_save
  19. self._capture_log(False)
  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('%scmd%d' % (prefix, n))
  27. self.assertEqual(parser.config['default_option'], 'default_default1')
  28. def test_single_override(self):
  29. """
  30. A value in any command section should override the default
  31. """
  32. for prefix in ['', 'git-', 'gbp-']:
  33. parser = GbpOptionParser('%scmd1' % prefix)
  34. self.assertEqual(parser.config['single_override_option1'], 'single_override_value1')
  35. # No deprecation warning since the test1.conf section is [cmd1]
  36. self._check_log_empty()
  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. for line in range(0, 2):
  45. self._check_log(line, ".*Old style config section \[git-cmd2\] found please rename to \[cmd2\]")
  46. def test_single_gbp_override(self):
  47. """
  48. A value in any gbp-command section should override the default
  49. """
  50. for prefix in ['', 'gbp-']:
  51. parser = GbpOptionParser('%scmd3' % prefix)
  52. self.assertEqual(parser.config['single_gbp_override_option1'], 'single_gbp_override_value1')
  53. for line in range(0, 2):
  54. self._check_log(line, ".*Old style config section \[gbp-cmd3\] found please rename to \[cmd3\]")
  55. def test_single_git_override_disabled_deprecations(self):
  56. """
  57. With disabled deprecations we shouldn't see a log line
  58. """
  59. for prefix in ['', 'git-']:
  60. os.environ['GBP_DISABLE_SECTION_DEPRECTATION'] = 'true'
  61. parser = GbpOptionParser('%scmd2' % prefix)
  62. self.assertEqual(parser.config['single_git_override_option1'], 'single_git_override_value1')
  63. for line in range(0, 2):
  64. self._check_log_empty()
  65. os.environ.pop('GBP_DISABLE_SECTION_DEPRECTATION')
  66. def test_new_overrides_git(self):
  67. """
  68. A value in the cmd section should override the old git-cmd section independent from
  69. how we're invoked
  70. """
  71. for n in range(4, 6):
  72. for prefix in ['', 'git-']:
  73. cmd = '%scmd%d' % (prefix, n)
  74. parser = GbpOptionParser(cmd)
  75. actual = parser.config['new_overrides_git_option1']
  76. expected = 'new_overrides_git_value1'
  77. self.assertEqual(actual, expected, "%s != %s for %s" % (actual, expected, cmd))
  78. def test_get_config_file_value(self):
  79. """
  80. Read a single value from the parsed config
  81. """
  82. parser = GbpOptionParser('cmd4')
  83. self.assertEqual(parser.get_config_file_value('new_overrides_git_option1'),
  84. 'new_overrides_git_value1')
  85. self.assertEqual(parser.get_config_file_value('doesnotexist'), None)
  86. def test_param_list(self):
  87. parser = GbpOptionParser('cmd4')
  88. branch_group = GbpOptionGroup(parser, "branch options", "branch update and layout options")
  89. parser.add_option_group(branch_group)
  90. branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
  91. branch_group.add_config_file_option("debian-branch", dest="upstream_branch")
  92. parser.add_config_file_option(option_name="color", dest="color", type='tristate')
  93. params = parser.valid_options
  94. self.assertTrue('upstream-branch' in params)
  95. self.assertTrue('debian-branch' in params)
  96. self.assertTrue('color' in params)
  97. def test_short_option_with_prefix(self):
  98. """Options with short options can't have a prefix"""
  99. class TestOptonParser(GbpOptionParser):
  100. list_opts = []
  101. defaults = {'withshort': 'foo'}
  102. short_opts = {'withshort': '-S'}
  103. parser = TestOptonParser('cmd', prefix='p')
  104. with self.assertRaisesRegexp(ValueError, "Options with prefix cannot have a short option"):
  105. parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo")
  106. def test_short_option(self):
  107. class TestOptionParser(GbpOptionParser):
  108. list_opts = []
  109. defaults = {'withshort': 'foo'}
  110. short_opts = {'withshort': '-S'}
  111. parser = TestOptionParser('cmd')
  112. parser.add_config_file_option(option_name="withshort", dest="with_short", help="foo")
  113. self.assertItemsEqual(['withshort'], parser.valid_options)
  114. self.assertTrue(parser.has_option("--withshort"))
  115. self.assertTrue(parser.has_option("-S"))