From 0855de2b5aa3b26aa61edcdfe5fd2ba612094eb5 Mon Sep 17 00:00:00 2001 From: vg Date: Thu, 16 Jan 2020 12:45:41 +0100 Subject: return error status on command invocation --- contacts_validation/contacts_validation/__init__.py | 5 +++++ contacts_validation/contacts_validation/command_line.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'contacts_validation') diff --git a/contacts_validation/contacts_validation/__init__.py b/contacts_validation/contacts_validation/__init__.py index 822e789..0f00c18 100644 --- a/contacts_validation/contacts_validation/__init__.py +++ b/contacts_validation/contacts_validation/__init__.py @@ -58,10 +58,12 @@ def print_validation_error(data_obj, exception): def validate_yaml_data(data_obj, schema_obj): + valid = True try: jsonschema.validate(convert_types(data_obj), schema_obj) except jsonschema.ValidationError as e: print_validation_error(data_obj, e) + valid = False # schema ok, check for duplicates in display and uuids display_dups = collections.defaultdict(list) @@ -72,7 +74,10 @@ def validate_yaml_data(data_obj, schema_obj): for e in (data_obj[e] for e in display_dups.values() if len(e) > 2): print('WARNING: duplicated display value for these contacts:') print(f'=> display: {e["display"]} uuid: {e["uuid"]}') + valid = False # by default warning are errors on cmdline for e in (data_obj[e] for e in uuid_dups.values() if len(e) > 2): print('ERROR: duplicated uuid value for these contacts:') print(f'=> display: {e["display"]} uuid: {e["uuid"]}') + valid = False + return valid diff --git a/contacts_validation/contacts_validation/command_line.py b/contacts_validation/contacts_validation/command_line.py index 0310c78..29ec2dd 100644 --- a/contacts_validation/contacts_validation/command_line.py +++ b/contacts_validation/contacts_validation/command_line.py @@ -44,8 +44,12 @@ def main(): schema_obj = yaml.safe_load(schema_fh.read()) assert schema_obj + error_occured = False for i, stream in enumerate(gen_streams(args['FILENAME'] or ['-'])): print('#'*60, f'# Valdating stream {i}', '#'*60, sep='\n') yaml_data = yaml.safe_load(stream.read()) assert yaml_data - validate_yaml_data(yaml_data, schema_obj) + if not validate_yaml_data(yaml_data, schema_obj): + error_occured = True + if error_occured: + raise SystemExit(1) -- cgit v1.2.3