diff --git a/src/rougail/output_json/__init__.py b/src/rougail/output_json/__init__.py index 883a415..6964b64 100644 --- a/src/rougail/output_json/__init__.py +++ b/src/rougail/output_json/__init__.py @@ -135,15 +135,30 @@ class RougailOutputJson: self.errors.append(f" - {option.description()}") def manage_warnings(self) -> None: - if self.warnings: - self.dico["_warnings"] = self.warnings + if not self.warnings: + return + warnings = [] + for w in self.warnings: + if isinstance(w, dict): + msg, opt = next(iter(w.items())) + warnings.append(_('{0}: {1}').format(opt.path, msg)) + else: + warnings.append(w) + self.dico["_warnings"] = warnings def manage_errors(self) -> bool: if not self.errors: - return False - self.dico = {"_errors": self.errors} + return True + errors = [] + for e in self.errors: + if isinstance(e, dict): + msg, opt = next(iter(e.items())) + errors.append(_('{0}: {1}').format(opt.path, msg)) + else: + errors.append(e) + self.dico = {"_errors": errors} self.manage_warnings() - return True + return False def parse_family( self, diff --git a/tests/owner-results/default.json b/tests/owner-results/default.json new file mode 100644 index 0000000..5cd8d6f --- /dev/null +++ b/tests/owner-results/default.json @@ -0,0 +1,5 @@ +{ + "rougail": { + "a_first_variable": "default value" + } +} \ No newline at end of file diff --git a/tests/owner-results/new_owner.json b/tests/owner-results/new_owner.json new file mode 100644 index 0000000..425a34a --- /dev/null +++ b/tests/owner-results/new_owner.json @@ -0,0 +1,5 @@ +{ + "rougail": { + "a_third_variable": "a value" + } +} \ No newline at end of file diff --git a/tests/owner-results/user.json b/tests/owner-results/user.json new file mode 100644 index 0000000..27efdc4 --- /dev/null +++ b/tests/owner-results/user.json @@ -0,0 +1,5 @@ +{ + "rougail": { + "a_second_variable": "a value" + } +} \ No newline at end of file diff --git a/tests/owner/file.yml b/tests/owner/file.yml new file mode 100644 index 0000000..6cab496 --- /dev/null +++ b/tests/owner/file.yml @@ -0,0 +1,10 @@ +%YAML 1.2 +--- +version: 1.1 + +a_first_variable: default value + +a_second_variable: default value + +a_third_variable: default value +... diff --git a/tests/results/test/04_5disabled_calculation_variable11.json b/tests/results/test/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/results/test/04_5disabled_calculation_variable11.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/results/test_mandatory/04_5disabled_calculation_variable11.json b/tests/results/test_mandatory/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/results/test_mandatory/04_5disabled_calculation_variable11.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/results/test_namespace/04_5disabled_calculation_variable11.json b/tests/results/test_namespace/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..91936ef --- /dev/null +++ b/tests/results/test_namespace/04_5disabled_calculation_variable11.json @@ -0,0 +1,3 @@ +{ + "rougail": {} +} \ No newline at end of file diff --git a/tests/results/test_namespace_mandatory/04_5disabled_calculation_variable11.json b/tests/results/test_namespace_mandatory/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..91936ef --- /dev/null +++ b/tests/results/test_namespace_mandatory/04_5disabled_calculation_variable11.json @@ -0,0 +1,3 @@ +{ + "rougail": {} +} \ No newline at end of file diff --git a/tests/results/test_namespace_read_write/04_5disabled_calculation_variable11.json b/tests/results/test_namespace_read_write/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..91936ef --- /dev/null +++ b/tests/results/test_namespace_read_write/04_5disabled_calculation_variable11.json @@ -0,0 +1,3 @@ +{ + "rougail": {} +} \ No newline at end of file diff --git a/tests/results/test_namespace_read_write_mandatory/04_5disabled_calculation_variable11.json b/tests/results/test_namespace_read_write_mandatory/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..91936ef --- /dev/null +++ b/tests/results/test_namespace_read_write_mandatory/04_5disabled_calculation_variable11.json @@ -0,0 +1,3 @@ +{ + "rougail": {} +} \ No newline at end of file diff --git a/tests/results/test_namespace_read_write_mandatory_errors/04_5disabled_calculation_variable11.json b/tests/results/test_namespace_read_write_mandatory_errors/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..91936ef --- /dev/null +++ b/tests/results/test_namespace_read_write_mandatory_errors/04_5disabled_calculation_variable11.json @@ -0,0 +1,3 @@ +{ + "rougail": {} +} \ No newline at end of file diff --git a/tests/results/test_read_write/04_5disabled_calculation_variable11.json b/tests/results/test_read_write/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/results/test_read_write/04_5disabled_calculation_variable11.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/results/test_read_write_mandatory/04_5disabled_calculation_variable11.json b/tests/results/test_read_write_mandatory/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/results/test_read_write_mandatory/04_5disabled_calculation_variable11.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/results/test_read_write_mandatory_errors/04_5disabled_calculation_variable11.json b/tests/results/test_read_write_mandatory_errors/04_5disabled_calculation_variable11.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/results/test_read_write_mandatory_errors/04_5disabled_calculation_variable11.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/subconfig-results/family.json b/tests/subconfig-results/family.json new file mode 100644 index 0000000..c18a7a9 --- /dev/null +++ b/tests/subconfig-results/family.json @@ -0,0 +1,5 @@ +{ + "a_sub_family": { + "a_variable": true + } +} \ No newline at end of file diff --git a/tests/subconfig-results/result.json b/tests/subconfig-results/result.json new file mode 100644 index 0000000..c18a7a9 --- /dev/null +++ b/tests/subconfig-results/result.json @@ -0,0 +1,5 @@ +{ + "a_sub_family": { + "a_variable": true + } +} \ No newline at end of file diff --git a/tests/subconfig-results/variable.json b/tests/subconfig-results/variable.json new file mode 100644 index 0000000..f32a580 --- /dev/null +++ b/tests/subconfig-results/variable.json @@ -0,0 +1 @@ +true \ No newline at end of file diff --git a/tests/subconfig/file.yml b/tests/subconfig/file.yml new file mode 100644 index 0000000..853871f --- /dev/null +++ b/tests/subconfig/file.yml @@ -0,0 +1,10 @@ +%YAML 1.2 +--- +version: 1.1 + +a_family: # a family + + a_sub_family: # a subfamily + + a_variable: true # a variable inside a family +... diff --git a/tests/test_load.py b/tests/test_load.py index a25b134..f385288 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -1,6 +1,7 @@ from pytest import fixture # , raises from pathlib import Path from rougail import Rougail, RougailConfig +from rougail.user_data_yaml import RougailUserDataYaml from rougail.output_json import RougailOutputJson as RougailOutput from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config @@ -149,3 +150,67 @@ def test_subconfig_variable(): with output_file.open() as outfh: attented_output = outfh.read() assert generated_output == attented_output, f'filename {output_file}' + + +def test_warnings(): + rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True) + ################################## + rougailconfig['step.output'] = 'json' + rougailconfig['step.user_data'] = ['yaml'] + rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml'] + extra_namespaces = rougailconfig["extra_namespaces"] + rougailconfig['extra_namespaces'] = extra_namespaces + ################################## + rougail = Rougail(rougailconfig) + config = rougail.run() + generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run() + err_warn = rougail.user_data(generated_user_data) + output = RougailOutput( + config=config, + rougailconfig=rougailconfig, + user_data_errors=err_warn["errors"], + user_data_warnings=err_warn["warnings"], + ) + ret = output.run() + assert ret[0] is True + output_file = Path(__file__).parent / "warnings" / 'results' / "warnings.yml" + if not output_file.is_file(): + if not output_file.parent.is_dir(): + output_file.parent.mkdir() + with output_file.open('w') as outfh: + outfh.write(ret[1]) + with output_file.open() as outfh: + attented_output = outfh.read() + assert ret[1] == attented_output, f'filename {output_file}' + + +def test_errors(): + rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True) + ################################## + rougailconfig['step.output'] = 'json' + rougailconfig['step.user_data'] = ['yaml'] + rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml'] + extra_namespaces = rougailconfig["extra_namespaces"] + rougailconfig['extra_namespaces'] = extra_namespaces + ################################## + rougail = Rougail(rougailconfig) + config = rougail.run() + generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run() + err_warn = rougail.user_data(generated_user_data, unknown_user_data_error=True) + output = RougailOutput( + config=config, + rougailconfig=rougailconfig, + user_data_errors=err_warn["errors"], + user_data_warnings=err_warn["warnings"], + ) + ret = output.run() + assert ret[0] is False + output_file = Path(__file__).parent / "warnings" / 'results' / "errors.yml" + if not output_file.is_file(): + if not output_file.parent.is_dir(): + output_file.parent.mkdir() + with output_file.open('w') as outfh: + outfh.write(ret[1]) + with output_file.open() as outfh: + attented_output = outfh.read() + assert ret[1] == attented_output, f'filename {output_file}' diff --git a/tests/warnings/results/errors.yml b/tests/warnings/results/errors.yml new file mode 100644 index 0000000..e80fb79 --- /dev/null +++ b/tests/warnings/results/errors.yml @@ -0,0 +1,5 @@ +{ + "_errors": [ + "rougail: variable or family \"an_unknown_var\" does not exist so cannot load \"rougail.an_unknown_var\", it has been loading from the YAML file \"tests/warnings/yaml/config.yml\"" + ] +} \ No newline at end of file diff --git a/tests/warnings/results/warnings.yml b/tests/warnings/results/warnings.yml new file mode 100644 index 0000000..4ba1408 --- /dev/null +++ b/tests/warnings/results/warnings.yml @@ -0,0 +1,8 @@ +{ + "_warnings": [ + "rougail: variable or family \"an_unknown_var\" does not exist so cannot load \"rougail.an_unknown_var\", it will be ignored when loading from the YAML file \"tests/warnings/yaml/config.yml\"" + ], + "rougail": { + "a_var": "a_value" + } +} \ No newline at end of file diff --git a/tests/warnings/structures/rougail/00-test.yml b/tests/warnings/structures/rougail/00-test.yml new file mode 100644 index 0000000..416f051 --- /dev/null +++ b/tests/warnings/structures/rougail/00-test.yml @@ -0,0 +1,4 @@ +--- +version: 1.1 + +a_var: diff --git a/tests/warnings/yaml/config.yml b/tests/warnings/yaml/config.yml new file mode 100644 index 0000000..12517d1 --- /dev/null +++ b/tests/warnings/yaml/config.yml @@ -0,0 +1,4 @@ +--- +rougail: + a_var: a_value + an_unknown_var: a_value