Skip to content
Snippets Groups Projects
Commit 99decdf9 authored by Luis Araujo's avatar Luis Araujo
Browse files

Add parser exceptions


Using exceptions offers a better interface for the parser to
report errors.

It also makes more convenient to test the parser code.

Signed-off-by: default avatarLuis Araujo <luis.araujo@collabora.co.uk>
parent f351981c
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
###################################################################################
# Test case parser exceptions.
#
# Copyright (C) 2018
# Luis Araujo <luis.araujo@collabora.co.uk>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US
###################################################################################
class ParserError(Exception):
pass
class ParserTypeError(ParserError):
def __init__(self, msg):
self._msg = msg
class ParserMissingFieldError(ParserError):
def __init__(self, msg):
self._msg = msg
......@@ -24,6 +24,8 @@
import sys
import yaml
from exceptions import ParserTypeError, ParserMissingFieldError
image_types = [ "any", "development", "minimal", "SDK", "target", "tiny-lxc" ]
image_arch = ['any', 'amd64', 'arm64', 'armhf']
tests_types = ['compatibility', 'functional', 'performance', 'sanity', 'system']
......@@ -63,9 +65,7 @@ def _parse_format(test_case, test_case_format):
value = test_case.get(tagf)
if not value:
if mandatory:
sys.stderr.write("Error: mandatory field missing: '{}'\n"
.format(tagf))
sys.exit(1)
raise ParserMissingFieldError("Mandatory field missing: " + tagf)
# Test case doesn't have this non-mandatory tag, so just continue.
continue
......@@ -80,9 +80,7 @@ def _parse_format(test_case, test_case_format):
continue
if type(value) != type(valuef):
sys.stderr.write("Error: incorrect type for field: '{}'\n"
.format(tagf))
sys.exit(1)
raise ParserTypeError("Incorrect type for field: " + tagf)
if type(value) == dict:
_parse_format(value, valuef)
......
......@@ -29,6 +29,7 @@ from jinja2 import Environment, FileSystemLoader
from argparse import ArgumentParser
from parser import parse_format
from exceptions import ParserTypeError, ParserMissingFieldError
TEMPLATE_DIR="."
......@@ -171,7 +172,11 @@ def generate_test_case(tc_file, directory):
# Parse file to detect any syntax error.
print("Parsing file", tc_file)
parse_format(tc_data)
try:
parse_format(tc_data)
except (ParserTypeError, ParserMissingFieldError) as error:
print("Error: " + str(error))
exit(1)
env = Environment(loader=FileSystemLoader([TEMPLATE_DIR]))
# Get template from environment and render it.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment