test_git_config.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2009 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the git_config.py module."""
  17. from __future__ import print_function
  18. import os
  19. import unittest
  20. import git_config
  21. def fixture(*paths):
  22. """Return a path relative to test/fixtures.
  23. """
  24. return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
  25. class GitConfigUnitTest(unittest.TestCase):
  26. """Tests the GitConfig class.
  27. """
  28. def setUp(self):
  29. """Create a GitConfig object using the test.gitconfig fixture.
  30. """
  31. config_fixture = fixture('test.gitconfig')
  32. self.config = git_config.GitConfig(config_fixture)
  33. def test_GetString_with_empty_config_values(self):
  34. """
  35. Test config entries with no value.
  36. [section]
  37. empty
  38. """
  39. val = self.config.GetString('section.empty')
  40. self.assertEqual(val, None)
  41. def test_GetString_with_true_value(self):
  42. """
  43. Test config entries with a string value.
  44. [section]
  45. nonempty = true
  46. """
  47. val = self.config.GetString('section.nonempty')
  48. self.assertEqual(val, 'true')
  49. def test_GetString_from_missing_file(self):
  50. """
  51. Test missing config file
  52. """
  53. config_fixture = fixture('not.present.gitconfig')
  54. config = git_config.GitConfig(config_fixture)
  55. val = config.GetString('empty')
  56. self.assertEqual(val, None)
  57. def test_GetBoolean_undefined(self):
  58. """Test GetBoolean on key that doesn't exist."""
  59. self.assertIsNone(self.config.GetBoolean('section.missing'))
  60. def test_GetBoolean_invalid(self):
  61. """Test GetBoolean on invalid boolean value."""
  62. self.assertIsNone(self.config.GetBoolean('section.boolinvalid'))
  63. def test_GetBoolean_true(self):
  64. """Test GetBoolean on valid true boolean."""
  65. self.assertTrue(self.config.GetBoolean('section.booltrue'))
  66. def test_GetBoolean_false(self):
  67. """Test GetBoolean on valid false boolean."""
  68. self.assertFalse(self.config.GetBoolean('section.boolfalse'))
  69. def test_GetInt_undefined(self):
  70. """Test GetInt on key that doesn't exist."""
  71. self.assertIsNone(self.config.GetInt('section.missing'))
  72. def test_GetInt_invalid(self):
  73. """Test GetInt on invalid integer value."""
  74. self.assertIsNone(self.config.GetBoolean('section.intinvalid'))
  75. def test_GetInt_valid(self):
  76. """Test GetInt on valid integers."""
  77. TESTS = (
  78. ('inthex', 16),
  79. ('inthexk', 16384),
  80. ('int', 10),
  81. ('intk', 10240),
  82. ('intm', 10485760),
  83. ('intg', 10737418240),
  84. )
  85. for key, value in TESTS:
  86. self.assertEqual(value, self.config.GetInt('section.%s' % (key,)))
  87. if __name__ == '__main__':
  88. unittest.main()