From 32a6415e58c1fcbfd0026232092a75011a39d710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 25 Jun 2019 15:13:16 +0200 Subject: [PATCH] config: Keep known options defined on CLI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the validation or dumping script is given some options, they should only be removed if they are not valid. We have to remove the invalid ones, otherwise that would cause a warning about unknown options. Signed-off-by: Lubomír Sedlář --- bin/pungi-config-dump | 3 +-- bin/pungi-config-validate | 3 +-- pungi_utils/config_utils.py | 10 ++++++++++ tests/test_config_utils.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bin/pungi-config-dump b/bin/pungi-config-dump index 91b3fd0c..f8d29601 100755 --- a/bin/pungi-config-dump +++ b/bin/pungi-config-dump @@ -112,8 +112,7 @@ def process_file( # Clean up defines from the final final config. We don't want to keep them # as they would cause warnings during validation. - for key in defines: - del conf[key] + config_utils.remove_unknown(conf, defines) if callback: callback(conf) diff --git a/bin/pungi-config-validate b/bin/pungi-config-validate index 5b9db3f9..ba7a045c 100755 --- a/bin/pungi-config-validate +++ b/bin/pungi-config-validate @@ -92,8 +92,7 @@ def run(config, topdir, has_old, offline, defined_variables): # Load actual configuration conf = pungi.util.load_config(config, defined_variables) # Remove the dummy variables used for defaults. - for key in defined_variables: - del conf[key] + config_utils.remove_unknown(conf, defined_variables) errors, warnings = pungi.checks.validate(conf, offline=offline) if errors or warnings: diff --git a/pungi_utils/config_utils.py b/pungi_utils/config_utils.py index fbe7d3dd..326bd769 100644 --- a/pungi_utils/config_utils.py +++ b/pungi_utils/config_utils.py @@ -3,6 +3,8 @@ import argparse import re +from pungi.checks import make_schema + def validate_definition(value): """Check that the variable name is a valid Python variable name, and that @@ -18,3 +20,11 @@ def validate_definition(value): def extract_defines(args): """Given an iterable of "key=value" strings, parse them into a dict.""" return dict(var.split("=", 1) for var in args) + + +def remove_unknown(conf, keys): + """Remove given keys from the config unless they are known Pungi options.""" + schema = make_schema() + for key in keys: + if key not in schema["properties"]: + del conf[key] diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index f0628a89..26bfed45 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -35,3 +35,13 @@ class TestDefineHelpers(unittest.TestCase): def test_validate_define_incorrect(self, value): with self.assertRaises(argparse.ArgumentTypeError): config_utils.validate_definition(value) + + def test_remove_unknown(self): + conf = {"foo": "bar"} + config_utils.remove_unknown(conf, ["foo"]) + self.assertEqual(conf, {}) + + def test_remove_known(self): + conf = {"release_name": "bar"} + config_utils.remove_unknown(conf, ["release_name"]) + self.assertEqual(conf, {"release_name": "bar"})