fix: warnings and errors support
This commit is contained in:
parent
1d29366b7c
commit
e2cd668806
24 changed files with 172 additions and 5 deletions
|
|
@ -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,
|
||||
|
|
|
|||
5
tests/owner-results/default.json
Normal file
5
tests/owner-results/default.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"rougail": {
|
||||
"a_first_variable": "default value"
|
||||
}
|
||||
}
|
||||
5
tests/owner-results/new_owner.json
Normal file
5
tests/owner-results/new_owner.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"rougail": {
|
||||
"a_third_variable": "a value"
|
||||
}
|
||||
}
|
||||
5
tests/owner-results/user.json
Normal file
5
tests/owner-results/user.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"rougail": {
|
||||
"a_second_variable": "a value"
|
||||
}
|
||||
}
|
||||
10
tests/owner/file.yml
Normal file
10
tests/owner/file.yml
Normal file
|
|
@ -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
|
||||
...
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rougail": {}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rougail": {}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rougail": {}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rougail": {}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rougail": {}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
5
tests/subconfig-results/family.json
Normal file
5
tests/subconfig-results/family.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"a_sub_family": {
|
||||
"a_variable": true
|
||||
}
|
||||
}
|
||||
5
tests/subconfig-results/result.json
Normal file
5
tests/subconfig-results/result.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"a_sub_family": {
|
||||
"a_variable": true
|
||||
}
|
||||
}
|
||||
1
tests/subconfig-results/variable.json
Normal file
1
tests/subconfig-results/variable.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
true
|
||||
10
tests/subconfig/file.yml
Normal file
10
tests/subconfig/file.yml
Normal file
|
|
@ -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
|
||||
...
|
||||
|
|
@ -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}'
|
||||
|
|
|
|||
5
tests/warnings/results/errors.yml
Normal file
5
tests/warnings/results/errors.yml
Normal file
|
|
@ -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\""
|
||||
]
|
||||
}
|
||||
8
tests/warnings/results/warnings.yml
Normal file
8
tests/warnings/results/warnings.yml
Normal file
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
4
tests/warnings/structures/rougail/00-test.yml
Normal file
4
tests/warnings/structures/rougail/00-test.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
version: 1.1
|
||||
|
||||
a_var:
|
||||
4
tests/warnings/yaml/config.yml
Normal file
4
tests/warnings/yaml/config.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
rougail:
|
||||
a_var: a_value
|
||||
an_unknown_var: a_value
|
||||
Loading…
Reference in a new issue