diff options
| author | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-03-14 22:24:14 +0000 |
|---|---|---|
| committer | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-03-22 18:35:55 +0000 |
| commit | f135fd041c6dee1aabea2f759d004627638862e2 (patch) | |
| tree | f3b0b4ed263ac17bfc556cd18a402708178c2997 /tests | |
| parent | b3f900170561c03b40d376e76fdedf49799e0c7d (diff) | |
| download | oslo-f135fd041c6dee1aabea2f759d004627638862e2.tar.gz oslo-f135fd041c6dee1aabea2f759d004627638862e2.tar.xz oslo-f135fd041c6dee1aabea2f759d004627638862e2.zip | |
Finish implementing MultiStrOpt
Fixes bug 955308
Previously only multiple string options from the CLI were supported.
This change adds support for config files too and merges the results
from both CLI and config files.
Change-Id: I642408c03ed295fac050105fd4380940e876f228
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/test_cfg.py | 23 | ||||
| -rw-r--r-- | tests/unit/test_iniparser.py | 111 |
2 files changed, 127 insertions, 7 deletions
diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index dc7b94e..5ea7970 100644 --- a/tests/unit/test_cfg.py +++ b/tests/unit/test_cfg.py @@ -445,24 +445,23 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertEquals(self.conf.foo, ['bar']) def test_conf_file_multistr_values_append(self): - self.conf.register_cli_opt(ListOpt('foo')) + self.conf.register_cli_opt(MultiStrOpt('foo')) paths = self.create_tempfiles([('1.conf', '[DEFAULT]\n' - 'foo = bar\n'), + 'foo = bar1\n'), ('2.conf', '[DEFAULT]\n' - 'foo = bar\n')]) + 'foo = bar2\n' + 'foo = bar3\n')]) - self.conf(['--foo', 'bar', + self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) - # FIXME(markmc): values spread across the CLI and multiple - # config files should be appended - # self.assertEquals(self.conf.foo, ['bar', 'bar', 'bar']) + self.assertEquals(self.conf.foo, ['bar0', 'bar1', 'bar2', 'bar3']) def test_conf_file_multiple_opts(self): self.conf.register_opts([StrOpt('foo'), StrOpt('bar')]) @@ -983,3 +982,13 @@ class CommonOptsTestCase(BaseTestCase): CommonConfigOpts.DEFAULT_LOG_DATE_FORMAT) self.assertEquals(self.conf.use_syslog, False) + + +class ConfigParserTestCase(unittest.TestCase): + def test_no_section(self): + with tempfile.NamedTemporaryFile() as tmpfile: + tmpfile.write('foo = bar') + tmpfile.flush() + + parser = ConfigParser(tmpfile.name, {}) + self.assertRaises(ParseError, parser.parse) diff --git a/tests/unit/test_iniparser.py b/tests/unit/test_iniparser.py new file mode 100644 index 0000000..5b3a0de --- /dev/null +++ b/tests/unit/test_iniparser.py @@ -0,0 +1,111 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +from openstack.common import iniparser + + +class TestParser(iniparser.BaseParser): + comment_called = False + values = None + section = '' + + def __init__(self): + self.values = {} + + def assignment(self, key, value): + self.values.setdefault(self.section, {}) + self.values[self.section][key] = value + + def new_section(self, section): + self.section = section + + def comment(self, section): + self.comment_called = True + + +class BaseParserTestCase(unittest.TestCase): + def setUp(self): + self.parser = iniparser.BaseParser() + + def _assertParseError(self, *lines): + self.assertRaises(iniparser.ParseError, self.parser.parse, lines) + + def test_invalid_assignment(self): + self._assertParseError("foo - bar") + + def test_empty_key(self): + self._assertParseError(": bar") + + def test_unexpected_continuation(self): + self._assertParseError(" baz") + + def test_invalid_section(self): + self._assertParseError("[section") + + def test_no_section_name(self): + self._assertParseError("[]") + + +class ParserTestCase(unittest.TestCase): + def setUp(self): + self.parser = TestParser() + + def test_blank_line(self): + lines = [""] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {}) + + def test_assignment_equal(self): + lines = ["foo = bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar']}}) + + def test_assignment_colon(self): + lines = ["foo: bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar']}}) + + def test_assignment_multiline(self): + lines = ["foo = bar0", " bar1"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'': {'foo': ['bar0', 'bar1']}}) + + def test_assignment_multline_empty(self): + lines = ["foo = bar0", "", " bar1"] + self.assertRaises(iniparser.ParseError, self.parser.parse, lines) + + def test_section_assignment(self): + lines = ["[test]", "foo = bar"] + self.parser.parse(lines) + self.assertEquals(self.parser.values, {'test': {'foo': ['bar']}}) + + def test_new_section(self): + lines = ["[foo]"] + self.parser.parse(lines) + self.assertEquals(self.parser.section, 'foo') + + def test_comment(self): + lines = ["# foobar"] + self.parser.parse(lines) + self.assertTrue(self.parser.comment_called) + + +class ExceptionTestCase(unittest.TestCase): + def test_parseerror(self): + exc = iniparser.ParseError('test', 42, 'example') + self.assertEquals(str(exc), "at line 42, test: 'example'") |
