From 99decdf9444d90056bc917f015029fb648e4d294 Mon Sep 17 00:00:00 2001
From: Luis Araujo <luis.araujo@collabora.co.uk>
Date: Tue, 27 Nov 2018 18:35:43 +0800
Subject: [PATCH] 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: Luis Araujo <luis.araujo@collabora.co.uk>
---
 renderer/exceptions.py    | 35 +++++++++++++++++++++++++++++++++++
 renderer/parser.py        | 10 ++++------
 renderer/render-test-case |  7 ++++++-
 3 files changed, 45 insertions(+), 7 deletions(-)
 create mode 100644 renderer/exceptions.py

diff --git a/renderer/exceptions.py b/renderer/exceptions.py
new file mode 100644
index 0000000..3b5ea7a
--- /dev/null
+++ b/renderer/exceptions.py
@@ -0,0 +1,35 @@
+#!/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
diff --git a/renderer/parser.py b/renderer/parser.py
index 224b192..76602ac 100644
--- a/renderer/parser.py
+++ b/renderer/parser.py
@@ -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)
diff --git a/renderer/render-test-case b/renderer/render-test-case
index 55363a7..427eb25 100755
--- a/renderer/render-test-case
+++ b/renderer/render-test-case
@@ -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.
-- 
GitLab