19_test_gbp_scripts_config.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # vim: set fileencoding=utf-8 :
  2. # (C) 2014 Guido Günther <agx@sigxcpu.org>
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, please see
  15. # <http://www.gnu.org/licenses/>
  16. """Test the L{gbp} config command"""
  17. import os
  18. import unittest
  19. import gbp.scripts.config
  20. class TestGbpConfigCommand(unittest.TestCase):
  21. class SingleValuePrintStub(object):
  22. def __init__(self):
  23. self.result = None
  24. def __call__(self, arg):
  25. self.result = arg
  26. class AllValuesPrintStub(object):
  27. def __init__(self, cmd):
  28. self.cmd = cmd
  29. self.result = {}
  30. def __call__(self, arg):
  31. k, v = arg.split('=', 1)
  32. self.result[k] = v
  33. def setUp(self):
  34. self.conffiles_save = os.environ.get('GBP_CONF_FILES')
  35. self.confname = 'tests/data/gbp_config.conf'
  36. self.assertTrue(os.stat(self.confname))
  37. os.environ['GBP_CONF_FILES'] = self.confname
  38. def test_invocation_single_value(self):
  39. """Can invoke it for a sngle value without error"""
  40. ret = gbp.scripts.config.main(['doesnotmatter', 'config.color'])
  41. self.assertEqual(ret, 0)
  42. def test_invocation_missing_value(self):
  43. """Can we detect a missing value"""
  44. ret = gbp.scripts.config.main(['doesnotmatter', 'config.doesnotexist'])
  45. self.assertEqual(ret, 2)
  46. def test_print_cmd_single_value_default(self):
  47. """Can we fetch a single configuration value that is at it's default"""
  48. printstub = self.SingleValuePrintStub()
  49. query = 'config.color'
  50. ret = gbp.scripts.config.print_cmd_values(query, printstub)
  51. self.assertEqual(printstub.result, '%s=auto' % query)
  52. self.assertEqual(ret, 0)
  53. def test_print_cmd_single_value_empty_default(self):
  54. """Can we fetch a single configuration value that is at it's default which is empty"""
  55. printstub = self.SingleValuePrintStub()
  56. query = 'buildpackage.keyid'
  57. ret = gbp.scripts.config.print_cmd_values(query, printstub)
  58. self.assertEqual(printstub.result, '%s=' % query)
  59. self.assertEqual(ret, 0)
  60. def test_print_cmd_single_value_override(self):
  61. """Can we fetch a single configuration value that is overriden by config"""
  62. printstub = self.SingleValuePrintStub()
  63. query = 'config.color-scheme'
  64. ret = gbp.scripts.config.print_cmd_values(query, printstub)
  65. self.assertEqual(printstub.result, '%s=checkcheck' % query)
  66. self.assertEqual(ret, 0)
  67. def test_print_cmd_all_values(self):
  68. """Can we fetch the configuration for all commands"""
  69. for cmd in [ 'buildpackage',
  70. 'clone',
  71. 'config',
  72. 'create_remote_repo',
  73. 'dch',
  74. 'import_dsc',
  75. 'import_orig',
  76. 'pq',
  77. 'pull' ]:
  78. printstub = self.AllValuesPrintStub(cmd)
  79. ret = gbp.scripts.config.print_cmd_values(cmd, printstub)
  80. self.assertIn('%s.color' % cmd, printstub.result.keys())
  81. self.assertEquals(printstub.result['%s.color' % cmd], 'auto')
  82. self.assertEqual(ret, 0)
  83. def test_unexistent_cmds(self):
  84. """Unexisting commands should print no values"""
  85. for cmd in ["import_dscs", "supercommand"]:
  86. printstub = self.AllValuesPrintStub(cmd)
  87. ret = gbp.scripts.config.print_cmd_values(cmd, printstub)
  88. self.assertEquals(printstub.result, dict())
  89. self.assertEqual(ret, 2)