From 4ec92cb9e7d1eb2f90d36e5255ee8814ca0a8513 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Wed, 22 Oct 2014 11:03:48 -0500 Subject: Options: ensure macros are always fixed up This fixes several cases in which macros would not be properly processed: options that are not added to the parser yet when early options are parsed; and config file options whose default value is used. --- .../Testsrc/Testlib/TestOptions/TestComponents.py | 25 +++++++++++++++++++--- .../Testsrc/Testlib/TestOptions/TestOptions.py | 8 ++++++- testsuite/Testsrc/Testlib/TestOptions/__init__.py | 7 ++++-- 3 files changed, 34 insertions(+), 6 deletions(-) (limited to 'testsuite') diff --git a/testsuite/Testsrc/Testlib/TestOptions/TestComponents.py b/testsuite/Testsrc/Testlib/TestOptions/TestComponents.py index d637d34c5..61b87de2a 100644 --- a/testsuite/Testsrc/Testlib/TestOptions/TestComponents.py +++ b/testsuite/Testsrc/Testlib/TestOptions/TestComponents.py @@ -3,8 +3,8 @@ import argparse import os -from Bcfg2.Options import Option, BooleanOption, ComponentAction, get_parser, \ - new_parser, Types, ConfigFileAction +from Bcfg2.Options import Option, BooleanOption, PathOption, ComponentAction, \ + get_parser, new_parser, Types, ConfigFileAction, Common from testsuite.Testsrc.Testlib.TestOptions import make_config, One, Two, \ OptionTestCase @@ -51,18 +51,27 @@ class ConfigFileComponent(object): default="bar")] +class PathComponent(object): + """fake component for testing macros in child components.""" + options = [PathOption(cf=("test", "test_path")), + PathOption(cf=("test", "test_path_default"), + default="/test/default")] + + class ParentComponentAction(ComponentAction): """parent component loader action.""" mapping = {"one": ComponentOne, "two": ComponentTwo, "three": ComponentThree, - "config": ConfigFileComponent} + "config": ConfigFileComponent, + "path": PathComponent} class TestComponentOptions(OptionTestCase): """test cases for component loading.""" def setUp(self): + OptionTestCase.setUp(self) self.options = [ Option("--parent", type=Types.comma_list, default=["one", "two"], action=ParentComponentAction)] @@ -147,6 +156,16 @@ class TestComponentOptions(OptionTestCase): self.parser.parse(["-C", config_file, "--parent", "config"]) self.assertEqual(self.result.config2, None) + @make_config({"test": {"test_path": "/test"}}) + def test_macros_in_component_options(self, config_file): + """fix up macros in component options.""" + self.parser.add_options([Common.repository]) + self.parser.parse(["-C", config_file, "-Q", "/foo/bar", + "--parent", "path"]) + self.assertEqual(self.result.test_path, "/foo/bar/test") + self.assertEqual(self.result.test_path_default, + "/foo/bar/test/default") + class ImportComponentAction(ComponentAction): """action that imports real classes for testing.""" diff --git a/testsuite/Testsrc/Testlib/TestOptions/TestOptions.py b/testsuite/Testsrc/Testlib/TestOptions/TestOptions.py index b76cd6d3a..9f4a7873c 100644 --- a/testsuite/Testsrc/Testlib/TestOptions/TestOptions.py +++ b/testsuite/Testsrc/Testlib/TestOptions/TestOptions.py @@ -20,6 +20,7 @@ class TestBasicOptions(OptionTestCase): # that's probably bad -- and it's definitely bad if we ever # want to do real on-the-fly config changes -- but it's easier # to leave it as is and set the options on each test. + OptionTestCase.setUp(self) self.options = [ BooleanOption("--test-true-boolean", env="TEST_TRUE_BOOLEAN", cf=("test", "true_boolean"), default=True), @@ -367,13 +368,16 @@ class TestBasicOptions(OptionTestCase): parser.add_options, [Option(cf=("test", "option"))]) - @make_config() + @make_config({"test": {"test_path": "/test"}}) def test_repository_macro(self, config_file): """fix up macros.""" result = argparse.Namespace() parser = Parser(namespace=result) parser.add_options([PathOption("--test1"), PathOption("--test2"), + PathOption(cf=("test", "test_path")), + PathOption(cf=("test", "test_path_default"), + default="/test/default"), Common.repository]) parser.parse(["-C", config_file, "-Q", "/foo/bar", "--test1", "/test1", @@ -381,6 +385,8 @@ class TestBasicOptions(OptionTestCase): self.assertEqual(result.repository, "/foo/bar") self.assertEqual(result.test1, "/foo/bar/test1") self.assertEqual(result.test2, "/foo/bar/foo/bar") + self.assertEqual(result.test_path, "/foo/bar/test") + self.assertEqual(result.test_path_default, "/foo/bar/test/default") @make_config() def test_file_like_path_option(self, config_file): diff --git a/testsuite/Testsrc/Testlib/TestOptions/__init__.py b/testsuite/Testsrc/Testlib/TestOptions/__init__.py index 688f4e54c..ca2c41359 100644 --- a/testsuite/Testsrc/Testlib/TestOptions/__init__.py +++ b/testsuite/Testsrc/Testlib/TestOptions/__init__.py @@ -4,7 +4,7 @@ import os import tempfile from Bcfg2.Compat import wraps, ConfigParser -from Bcfg2.Options import Parser +from Bcfg2.Options import Parser, PathOption from testsuite.common import Bcfg2TestCase @@ -75,7 +75,10 @@ class OptionTestCase(Bcfg2TestCase): Parser.unit_test = False Bcfg2TestCase.setUpClass() + def setUp(self): + Bcfg2TestCase.setUp(self) + PathOption.repository = None + @classmethod def tearDownClass(cls): Parser.unit_test = True - Bcfg2TestCase.tearDownClass() -- cgit v1.2.3-1-g7c22