test_git_config.py 3.1 KB

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