From 1fd5fc07a5936cae310e4e32af773e1412bd5376 Mon Sep 17 00:00:00 2001 From: Mikhail Gordeev Date: Wed, 9 Feb 2022 01:19:03 +0300 Subject: [PATCH] Allow override required parameters --- cloud_build/cloud_build.py | 12 +++++++++--- tests/test_errors.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cloud_build/cloud_build.py b/cloud_build/cloud_build.py index 6d24576..4bb5607 100755 --- a/cloud_build/cloud_build.py +++ b/cloud_build/cloud_build.py @@ -216,6 +216,12 @@ class CB: msg = f'Could not read config file `{e.filename}`: {e.strerror}' raise Error(msg) + def lazy_get_raises(key): + if key in override: + return override[key] + else: + return cfg[key] + self.mkimage_profiles_git = self.expand_path( cfg.get('mkimage_profiles_git', '') ) @@ -260,9 +266,9 @@ class CB: self.key = '{:X}'.format(self.key) try: - self._remote = self.expand_path(cfg['remote']) - self._images = cfg['images'] - self._branches = cfg['branches'] + self._remote = self.expand_path(lazy_get_raises('remote')) + self._images = lazy_get_raises('images') + self._branches = lazy_get_raises('branches') for _, branch in self._branches.items(): branch['arches'] = {k: {} if v is None else v for k, v in branch['arches'].items()} diff --git a/tests/test_errors.py b/tests/test_errors.py index 034dba3..fec5c7f 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -60,6 +60,24 @@ class TestErrors(TestCase): self.kwargs.update(config=self.config) self.assertRaisesRegex(Error, regex, CB, **self.kwargs) + def test_override_required_parameters_in_config(self): + self.config = tempfile.mktemp(prefix='cb_conf') + config_override = { + 'remote': '/var/empty', + 'images': {}, + 'branches': {}, + } + with open('tests/minimal_config.yaml') as f: + cfg = yaml.safe_load(f) + + for parameter in ['remote', 'images', 'branches']: + with open(self.config, 'w') as f: + yaml.safe_dump(update(cfg, {parameter: None}), f) + + self.kwargs.update(config=self.config) + self.kwargs.update(config_override=config_override) + CB(**self.kwargs) + def test_run_already_running(self): self.kwargs.update(config='tests/minimal_config.yaml') cb = CB(**self.kwargs) # noqa F841