return test_type
def export_pending_data(self):
- """Write the accumulated data to disk.
+ """Write the data to disk, raise error if invalid.
Create missing directories.
Reset both file path and data to avoid writing multiple times.
If no file path is set, do not write anything,
as that is the failsafe behavior when caller from unexpected place.
- Aso do not write anything when EXPORT_JSON constant is false.
+ Also do not write anything when EXPORT_JSON constant is false.
- Regardless of whether data was written, it is cleared.
+ :raises: ValidationError if data export does not conform to schema.
"""
+ error = None
if not Constants.EXPORT_JSON or not self.file_path:
self.data = None
self.file_path = None
return
new_file_path = write_output(self.file_path, self.data)
- # Data is going to be cleared (as a sign that export succeeded),
- # so this is the last chance to detect if it was for a test case.
- is_testcase = "result" in self.data
+ if "result" in self.data:
+ error = validate(new_file_path, self.validators["tc_info"])
+ if error:
+ # Mark as failed and re-export.
+ self.data["passed"] = False
+ self.data["message"] = str(error)
+ write_output(self.file_path, self.data)
self.data = None
- # Validation for output goes here when ready.
self.file_path = None
- if is_testcase:
- validate(new_file_path, self.validators["tc_info"])
+ if error:
+ raise error
def warn_on_bad_export(self):
"""If bad state is detected, log a warning and clean up state."""
-# Copyright (c) 2023 Cisco and/or its affiliates.
+# Copyright (c) 2025 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
:param validator: Validator instance to use for validation.
:type file_path: str
:type validator: jsonschema.validators.Validator
- :raises ValidationError: If schema validation fails.
+ :returns: None if valid, error if invalid.
+ :rtype: Optional[ValidationError]
"""
with open(file_path, "rt", encoding="utf-8") as file_in:
instance = json.load(file_in)
error = jsonschema.exceptions.best_match(validator.iter_errors(instance))
if error is not None:
print(json.dumps(instance, indent=4))
- raise error
+ return error