diff options
Diffstat (limited to 'tests/test_git_config.py')
| -rw-r--r-- | tests/test_git_config.py | 134 |
1 files changed, 127 insertions, 7 deletions
diff --git a/tests/test_git_config.py b/tests/test_git_config.py index b735f27f..faf12a2e 100644 --- a/tests/test_git_config.py +++ b/tests/test_git_config.py | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | # -*- coding:utf-8 -*- | ||
| 2 | # | ||
| 3 | # Copyright (C) 2009 The Android Open Source Project | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 4 | # | 2 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| @@ -16,21 +14,22 @@ | |||
| 16 | 14 | ||
| 17 | """Unittests for the git_config.py module.""" | 15 | """Unittests for the git_config.py module.""" |
| 18 | 16 | ||
| 19 | from __future__ import print_function | ||
| 20 | |||
| 21 | import os | 17 | import os |
| 18 | import tempfile | ||
| 22 | import unittest | 19 | import unittest |
| 23 | 20 | ||
| 24 | import git_config | 21 | import git_config |
| 25 | 22 | ||
| 23 | |||
| 26 | def fixture(*paths): | 24 | def fixture(*paths): |
| 27 | """Return a path relative to test/fixtures. | 25 | """Return a path relative to test/fixtures. |
| 28 | """ | 26 | """ |
| 29 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) | 27 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) |
| 30 | 28 | ||
| 31 | class GitConfigUnitTest(unittest.TestCase): | 29 | |
| 32 | """Tests the GitConfig class. | 30 | class GitConfigReadOnlyTests(unittest.TestCase): |
| 33 | """ | 31 | """Read-only tests of the GitConfig class.""" |
| 32 | |||
| 34 | def setUp(self): | 33 | def setUp(self): |
| 35 | """Create a GitConfig object using the test.gitconfig fixture. | 34 | """Create a GitConfig object using the test.gitconfig fixture. |
| 36 | """ | 35 | """ |
| @@ -68,5 +67,126 @@ class GitConfigUnitTest(unittest.TestCase): | |||
| 68 | val = config.GetString('empty') | 67 | val = config.GetString('empty') |
| 69 | self.assertEqual(val, None) | 68 | self.assertEqual(val, None) |
| 70 | 69 | ||
| 70 | def test_GetBoolean_undefined(self): | ||
| 71 | """Test GetBoolean on key that doesn't exist.""" | ||
| 72 | self.assertIsNone(self.config.GetBoolean('section.missing')) | ||
| 73 | |||
| 74 | def test_GetBoolean_invalid(self): | ||
| 75 | """Test GetBoolean on invalid boolean value.""" | ||
| 76 | self.assertIsNone(self.config.GetBoolean('section.boolinvalid')) | ||
| 77 | |||
| 78 | def test_GetBoolean_true(self): | ||
| 79 | """Test GetBoolean on valid true boolean.""" | ||
| 80 | self.assertTrue(self.config.GetBoolean('section.booltrue')) | ||
| 81 | |||
| 82 | def test_GetBoolean_false(self): | ||
| 83 | """Test GetBoolean on valid false boolean.""" | ||
| 84 | self.assertFalse(self.config.GetBoolean('section.boolfalse')) | ||
| 85 | |||
| 86 | def test_GetInt_undefined(self): | ||
| 87 | """Test GetInt on key that doesn't exist.""" | ||
| 88 | self.assertIsNone(self.config.GetInt('section.missing')) | ||
| 89 | |||
| 90 | def test_GetInt_invalid(self): | ||
| 91 | """Test GetInt on invalid integer value.""" | ||
| 92 | self.assertIsNone(self.config.GetBoolean('section.intinvalid')) | ||
| 93 | |||
| 94 | def test_GetInt_valid(self): | ||
| 95 | """Test GetInt on valid integers.""" | ||
| 96 | TESTS = ( | ||
| 97 | ('inthex', 16), | ||
| 98 | ('inthexk', 16384), | ||
| 99 | ('int', 10), | ||
| 100 | ('intk', 10240), | ||
| 101 | ('intm', 10485760), | ||
| 102 | ('intg', 10737418240), | ||
| 103 | ) | ||
| 104 | for key, value in TESTS: | ||
| 105 | self.assertEqual(value, self.config.GetInt('section.%s' % (key,))) | ||
| 106 | |||
| 107 | def test_GetSyncAnalysisStateData(self): | ||
| 108 | """Test config entries with a sync state analysis data.""" | ||
| 109 | superproject_logging_data = {} | ||
| 110 | superproject_logging_data['test'] = False | ||
| 111 | options = type('options', (object,), {})() | ||
| 112 | options.verbose = 'true' | ||
| 113 | options.mp_update = 'false' | ||
| 114 | TESTS = ( | ||
| 115 | ('superproject.test', 'false'), | ||
| 116 | ('options.verbose', 'true'), | ||
| 117 | ('options.mpupdate', 'false'), | ||
| 118 | ('main.version', '1'), | ||
| 119 | ) | ||
| 120 | self.config.UpdateSyncAnalysisState(options, superproject_logging_data) | ||
| 121 | sync_data = self.config.GetSyncAnalysisStateData() | ||
| 122 | for key, value in TESTS: | ||
| 123 | self.assertEqual(sync_data[f'{git_config.SYNC_STATE_PREFIX}{key}'], value) | ||
| 124 | self.assertTrue(sync_data[f'{git_config.SYNC_STATE_PREFIX}main.synctime']) | ||
| 125 | |||
| 126 | |||
| 127 | class GitConfigReadWriteTests(unittest.TestCase): | ||
| 128 | """Read/write tests of the GitConfig class.""" | ||
| 129 | |||
| 130 | def setUp(self): | ||
| 131 | self.tmpfile = tempfile.NamedTemporaryFile() | ||
| 132 | self.config = self.get_config() | ||
| 133 | |||
| 134 | def get_config(self): | ||
| 135 | """Get a new GitConfig instance.""" | ||
| 136 | return git_config.GitConfig(self.tmpfile.name) | ||
| 137 | |||
| 138 | def test_SetString(self): | ||
| 139 | """Test SetString behavior.""" | ||
| 140 | # Set a value. | ||
| 141 | self.assertIsNone(self.config.GetString('foo.bar')) | ||
| 142 | self.config.SetString('foo.bar', 'val') | ||
| 143 | self.assertEqual('val', self.config.GetString('foo.bar')) | ||
| 144 | |||
| 145 | # Make sure the value was actually written out. | ||
| 146 | config = self.get_config() | ||
| 147 | self.assertEqual('val', config.GetString('foo.bar')) | ||
| 148 | |||
| 149 | # Update the value. | ||
| 150 | self.config.SetString('foo.bar', 'valll') | ||
| 151 | self.assertEqual('valll', self.config.GetString('foo.bar')) | ||
| 152 | config = self.get_config() | ||
| 153 | self.assertEqual('valll', config.GetString('foo.bar')) | ||
| 154 | |||
| 155 | # Delete the value. | ||
| 156 | self.config.SetString('foo.bar', None) | ||
| 157 | self.assertIsNone(self.config.GetString('foo.bar')) | ||
| 158 | config = self.get_config() | ||
| 159 | self.assertIsNone(config.GetString('foo.bar')) | ||
| 160 | |||
| 161 | def test_SetBoolean(self): | ||
| 162 | """Test SetBoolean behavior.""" | ||
| 163 | # Set a true value. | ||
| 164 | self.assertIsNone(self.config.GetBoolean('foo.bar')) | ||
| 165 | for val in (True, 1): | ||
| 166 | self.config.SetBoolean('foo.bar', val) | ||
| 167 | self.assertTrue(self.config.GetBoolean('foo.bar')) | ||
| 168 | |||
| 169 | # Make sure the value was actually written out. | ||
| 170 | config = self.get_config() | ||
| 171 | self.assertTrue(config.GetBoolean('foo.bar')) | ||
| 172 | self.assertEqual('true', config.GetString('foo.bar')) | ||
| 173 | |||
| 174 | # Set a false value. | ||
| 175 | for val in (False, 0): | ||
| 176 | self.config.SetBoolean('foo.bar', val) | ||
| 177 | self.assertFalse(self.config.GetBoolean('foo.bar')) | ||
| 178 | |||
| 179 | # Make sure the value was actually written out. | ||
| 180 | config = self.get_config() | ||
| 181 | self.assertFalse(config.GetBoolean('foo.bar')) | ||
| 182 | self.assertEqual('false', config.GetString('foo.bar')) | ||
| 183 | |||
| 184 | # Delete the value. | ||
| 185 | self.config.SetBoolean('foo.bar', None) | ||
| 186 | self.assertIsNone(self.config.GetBoolean('foo.bar')) | ||
| 187 | config = self.get_config() | ||
| 188 | self.assertIsNone(config.GetBoolean('foo.bar')) | ||
| 189 | |||
| 190 | |||
| 71 | if __name__ == '__main__': | 191 | if __name__ == '__main__': |
| 72 | unittest.main() | 192 | unittest.main() |
