From c12bad295f9a8e8c3ce4fb8aca5c44d7f34af8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 3 Aug 2017 14:42:40 +0200 Subject: [PATCH] config: Add option for dumping config schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it easier to compare changes to the schema in different versions. Signed-off-by: Lubomír Sedlář --- bin/pungi-config-validate | 11 +++++++++++ pungi/checks.py | 6 +++--- tests/test_checks.py | 21 +++++++++++---------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/bin/pungi-config-validate b/bin/pungi-config-validate index ce3f5cda..67913a60 100755 --- a/bin/pungi-config-validate +++ b/bin/pungi-config-validate @@ -4,6 +4,7 @@ from __future__ import print_function import argparse +import json import kobo.conf import os import sys @@ -95,8 +96,18 @@ def run(config, topdir, has_old): return errors +class DumpSchemaAction(argparse.Action): + def __call__(self, parser, ns, values, option_string=None): + json.dump(pungi.checks.make_schema(), sys.stdout, + sort_keys=True, indent=4) + print('') + sys.exit(0) + + def main(args=None): parser = argparse.ArgumentParser() + parser.add_argument('--dump-schema', nargs=0, action=DumpSchemaAction, + help='print JSON Schema of configuration and exit') parser.add_argument('config', metavar='CONFIG', help='configuration file to validate') parser.add_argument('--old-composes', action='store_true', diff --git a/pungi/checks.py b/pungi/checks.py index f6f87f67..9fb820d5 100644 --- a/pungi/checks.py +++ b/pungi/checks.py @@ -31,7 +31,7 @@ outline of the process: 3. Extra validation can happen in ``validate()`` method of any phase. When a new config option is added, the schema must be updated (see the -``_make_schema`` function). The dependencies should be encoded into +``make_schema`` function). The dependencies should be encoded into ``CONFIG_DEPS`` mapping. """ @@ -188,7 +188,7 @@ def validate(config): Undefined values for which a default value exists will be filled in. """ - schema = _make_schema() + schema = make_schema() DefaultValidator = _extend_with_default_and_alias(jsonschema.Draft4Validator) validator = DefaultValidator(schema, {'array': (tuple, list), @@ -393,7 +393,7 @@ class ConfigOptionError(jsonschema.exceptions.ValidationError): pass -def _make_schema(): +def make_schema(): return { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Pungi Configuration", diff --git a/tests/test_checks.py b/tests/test_checks.py index 84866141..6f53d638 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -16,6 +16,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from pungi import checks + class CheckDependenciesTestCase(unittest.TestCase): def dont_find(self, paths): @@ -195,7 +196,7 @@ class TestSchemaValidator(unittest.TestCase): conf.load_from_string(string) return conf - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_property(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -218,7 +219,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(len(warnings), 0) self.assertEqual(config.get("release_name", None), "dummy product") - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_alias_property(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -241,7 +242,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertRegexpMatches(warnings[0], r"^WARNING: Config option 'product_name' is deprecated and now an alias to 'release_name'.*") self.assertEqual(config.get("release_name", None), "dummy product") - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_required_is_missing(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -265,7 +266,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(len(warnings), 1) self.assertIn("WARNING: Unrecognized config option: name.", warnings) - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_required_is_in_alias(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -289,7 +290,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertRegexpMatches(warnings[0], r"^WARNING: Config option 'product_name' is deprecated and now an alias to 'release_name'.*") self.assertEqual(config.get("release_name", None), "dummy product") - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_redundant_alias(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -315,7 +316,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertRegexpMatches(warnings[0], r"^WARNING: Config option 'product_name' is deprecated and now an alias to 'release_name'.*") self.assertEqual(config.get("release_name", None), "dummy product") - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_properties_in_deep(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -356,7 +357,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(config.get("release_name", None), "dummy product") self.assertEqual(config.get("foophase", {}).get("repo", None), "http://www.exampe.com/os") - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_append_option(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -396,7 +397,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(config.get("release_name", None), "dummy product") self.assertEqual(config.get("repo", None), ["http://url/to/repo", "Server"]) - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_append_to_nonexist_option(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -435,7 +436,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(config.get("release_name", None), "dummy product") self.assertEqual(config.get("repo", None), ["http://url/to/repo", "Server"]) - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_multiple_appends(self, make_schema): schema = { "$schema": "http://json-schema.org/draft-04/schema#", @@ -480,7 +481,7 @@ class TestSchemaValidator(unittest.TestCase): self.assertEqual(config.get("release_name", None), "dummy product") self.assertEqual(config.get("repo", None), ["http://url/to/repo", "Server", "Client"]) - @mock.patch('pungi.checks._make_schema') + @mock.patch('pungi.checks.make_schema') def test_anyof_validator_not_raise_our_warnings_as_error(self, make_schema): # https://pagure.io/pungi/issue/598 schema = {