From 070630da945f35643736e19045526e4548384224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Schmidts?= Date: Sun, 22 Oct 2017 21:22:22 +0200 Subject: [PATCH] triggering error on required not set --- tests/test_the_conf.py | 9 +++++++++ the_conf.example.yml | 2 +- the_conf/the_conf.py | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_the_conf.py b/tests/test_the_conf.py index 0bb1f3f..ce36c83 100644 --- a/tests/test_the_conf.py +++ b/tests/test_the_conf.py @@ -39,3 +39,12 @@ def test_conf_from_obj(self): self.assertEqual('a', tc.option) tc.option = 1 self.assertEqual('1', tc.option) + + def test_trigger_error_w_required(self): + metaconf = {'parameters': [ + {'option': {'type': str, 'required': True}}], + 'config_files': []} + self.assertRaises(ValueError, + TheConf, metaconf, cmd_line_opts=[]) + tc = TheConf(metaconf, cmd_line_opts=['--option=stuff']) + self.assertEqual('stuff', tc.option) diff --git a/the_conf.example.yml b/the_conf.example.yml index 41ba66a..1d862f4 100644 --- a/the_conf.example.yml +++ b/the_conf.example.yml @@ -16,7 +16,7 @@ parameters: - nested: - value: type: int - required: true # default + required: false # default - other_value: type: bool - int_value: diff --git a/the_conf/the_conf.py b/the_conf/the_conf.py index 4e62489..ce0f969 100644 --- a/the_conf/the_conf.py +++ b/the_conf/the_conf.py @@ -66,6 +66,11 @@ def load(self): else: raise Exception('unknown order %r') + for path, value, param in self._get_path_val_param(): + if value is node.NoValue and param.get('required'): + raise ValueError('loading finished and %r is not set' + % '.'.join(path)) + def write(self, config_file=None): if config_file is None and not self._config_files: raise ValueError('no config file to write in')