Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_should_raise_exception(self):
with pytest.raises(EmptyConfigFileError) as excinfo:
load_config_file("tests/data/empty.yaml")
assert str(excinfo.value) == "File 'tests/data/empty.yaml' is empty."
def empty_config_file(*args, **kwargs):
raise EmptyConfigFileError("valid_path/api.yaml")
def scan():
spec_path = settings["spec_path"]
try:
api_spec = load_config_file(spec_path)
except FileNotFoundError as e:
error_message = f"Could not find API spec file: {spec_path}. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except EmptyConfigFileError as e:
error_message = f"API spec file is empty. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except (yaml.YAMLError, FileFormatNotSupportedError) as e:
logger.error(e)
raise SystemExit(ExitCode.USAGE_ERROR)
try:
if API_KEY not in api_spec:
raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE)
root_node = EndpointNode(api_spec[API_KEY])
results = root_node.run()
except (
InvalidKeyError,
def load_config_file(file_path):
extension = os.path.splitext(file_path)[-1]
if extension not in (".yaml", ".yml", ".json"):
raise FileFormatNotSupportedError(extension, file_path)
with open(file_path, "r") as stream:
logger.info(f"Loading file {file_path}")
data = yaml.load(stream, Loader)
if not data:
raise EmptyConfigFileError(file_path)
return data
def __init__(self, file_path, *args):
message = f"File '{file_path}' is empty."
super(EmptyConfigFileError, self).__init__(message, *args)