[Piglit] [PATCH v2 13/16] glsl_parser_test.py: use piglit generic exceptions

Dylan Baker baker.dylan.c at gmail.com
Mon May 18 10:57:47 PDT 2015


Use piglit exceptions for aborting and erroring rather than custom
classes.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/test/glsl_parser_test.py        |  29 ++++-----
 framework/tests/glsl_parser_test_tests.py | 103 ++++++++++--------------------
 2 files changed, 47 insertions(+), 85 deletions(-)

diff --git a/framework/test/glsl_parser_test.py b/framework/test/glsl_parser_test.py
index 44519fa..76576ae 100644
--- a/framework/test/glsl_parser_test.py
+++ b/framework/test/glsl_parser_test.py
@@ -24,26 +24,21 @@
 from __future__ import print_function, absolute_import
 import os
 import re
-import sys
 
+from framework import exceptions
 from .piglit_test import PiglitBaseTest
 
 __all__ = [
     'GLSLParserTest',
-    'GLSLParserError',
     'GLSLParserNoConfigError',
 ]
 
 
-class GLSLParserError(Exception):
+class GLSLParserNoConfigError(exceptions.PiglitInternalError):
     pass
 
 
-class GLSLParserNoConfigError(GLSLParserError):
-    pass
-
-
-class GLSLParserInternalError(GLSLParserError):
+class GLSLParserInternalError(exceptions.PiglitInternalError):
     pass
 
 
@@ -76,8 +71,8 @@ class GLSLParserTest(PiglitBaseTest):
                 command = self.__get_command(self.__parser(testfile, filepath),
                                              filepath)
             except GLSLParserInternalError as e:
-                print(e.message, file=sys.stderr)
-                sys.exit(1)
+                raise exceptions.PiglitFatalError(
+                    'In file "{}":\n{}'.format(filepath, e.message))
 
         super(GLSLParserTest, self).__init__(command, run_concurrent=True)
 
@@ -155,25 +150,25 @@ class GLSLParserTest(PiglitBaseTest):
             if match:
                 if match.group('key') not in GLSLParserTest._CONFIG_KEYS:
                     raise GLSLParserInternalError(
-                        "Key {0} in file {1} is not a valid key for a "
+                        "Key {} is not a valid key for a "
                         "glslparser test config block".format(
-                            match.group('key'), filepath))
+                            match.group('key')))
                 elif match.group('key') in self.__found_keys:
                     # If this key has already been encountered throw an error,
                     # there are no duplicate keys allows
                     raise GLSLParserInternalError(
-                        'Duplicate entry for key {0} in file {1}'.format(
-                            match.group('key'), filepath))
+                        'Duplicate entry for key {}'.format(
+                            match.group('key')))
                 else:
                     bad = bad_values.search(match.group('value'))
                     # XXX: this always seems to return a match object, even
                     # when the match is ''
                     if bad.group():
                         raise GLSLParserInternalError(
-                            'Bad character "{0}" in file: "{1}", '
-                            'line: "{2}". Only alphanumerics, _, and space '
+                            'Bad character "{}" at line: "{}". '
+                            'Only alphanumerics, _, and space '
                             'are allowed'.format(
-                                bad.group()[0], filepath, line))
+                                bad.group()[0], line))
 
                     # Otherwise add the key to the set of found keys, and add
                     # it to the dictionary that will be returned
diff --git a/framework/tests/glsl_parser_test_tests.py b/framework/tests/glsl_parser_test_tests.py
index 53777e6..bb5c246 100644
--- a/framework/tests/glsl_parser_test_tests.py
+++ b/framework/tests/glsl_parser_test_tests.py
@@ -21,19 +21,16 @@
 """ Provides tests for the shader_test module """
 
 from __future__ import print_function, absolute_import
-import sys
 import os
 
 import nose.tools as nt
 
-import framework.test as glsl
+from framework import exceptions
+import framework.test.glsl_parser_test as glsl
 import framework.tests.utils as utils
 from framework.test import TEST_BIN_DIR
 
-# Nose does not capture stderr, so all of the error catching tetss will spam
-# the console, however, it does capture stdout, so redirecting stderr to stdout
-# will cause it to be captured in the event that something is wrong.
-sys.stderr = sys.stdout
+# pylint: disable=line-too-long,invalid-name
 
 
 def _check_config(content):
@@ -56,6 +53,7 @@ def test_no_config_start():
                 msg="No config section found, no exception raised")
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_find_config_start():
     """test.glsl_parser_test.GLSLParserTest: successfully finds [config] section
     """
@@ -63,24 +61,18 @@ def test_find_config_start():
                '// glsl_version: 1.10\n'
                '//\n')
     with utils.tempfile(content) as tfile:
-        with nt.assert_raises(SystemExit) as exc:
-            glsl.GLSLParserTest(tfile)
-            nt.assert_not_equal(
-                exc.exception, 'No [config] section found!',
-                msg="Config section not parsed")
+        glsl.GLSLParserTest(tfile)
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_no_config_end():
     """test.glsl_parser_test.GLSLParserTest: exception is raised if [end config] section is missing
     """
     with utils.tempfile('// [config]\n') as tfile:
-        with nt.assert_raises(SystemExit) as exc:
-            glsl.GLSLParserTest(tfile)
-            nt.assert_equal(
-                exc.exception, 'No [end config] section found!',
-                msg="config section not closed, no exception raised")
+        glsl.GLSLParserTest(tfile)
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_no_expect_result():
     """test.glsl_parser_test.GLSLParserTest: exception is raised if "expect_result" key is missing
     """
@@ -88,14 +80,10 @@ def test_no_expect_result():
                '// glsl_version: 1.10\n'
                '//\n')
     with utils.tempfile(content) as tfile:
-        with nt.assert_raises(SystemExit) as exc:
-            glsl.GLSLParserTest(tfile)
-            nt.assert_equal(
-                exc.exception,
-                'Missing required section expect_result from config',
-                msg="config section not closed, no exception raised")
+        glsl.GLSLParserTest(tfile)
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_no_glsl_version():
     """test.glsl_parser_test.GLSLParserTest: exception is raised if "glsl_version" key is missing
     """
@@ -103,12 +91,7 @@ def test_no_glsl_version():
                '// expect_result: pass\n'
                '// [end config]\n')
     with utils.tempfile(content) as tfile:
-        with nt.assert_raises(SystemExit) as exc:
-            glsl.GLSLParserTest(tfile)
-            nt.assert_equal(
-                exc.exception,
-                'Missing required section glsl_version from config',
-                msg="config section not closed, no exception raised")
+        glsl.GLSLParserTest(tfile)
 
 
 def test_cpp_comments():
@@ -218,6 +201,7 @@ def test_config_to_command():
         yield check_config_to_command, config, result
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_bad_section_name():
     """test.glsl_parser_test.GLSLParserTest: A section name not in the _CONFIG_KEYS name raises an error"""
     content = ('// [config]\n'
@@ -227,15 +211,10 @@ def test_bad_section_name():
                '// [end config]\n')
 
     with utils.tempfile(content) as tfile:
-        with nt.assert_raises(SystemExit) as e:
-            glsl.GLSLParserTest(tfile)
-
-            nt.eq_(e.exception.message,
-                   'Key new_awesome_key in file {0 is not a valid key for a '
-                   'glslparser test config block'.format(tfile))
+        glsl.GLSLParserTest(tfile)
 
 
- at utils.not_raises(glsl.GLSLParserError)
+ at utils.not_raises(exceptions.PiglitFatalError)
 def test_good_section_names():
     """test.glsl_parser_test.GLSLParserTest: A section name in the _CONFIG_KEYS does not raise an error"""
     content = ('// [config]\n'
@@ -248,20 +227,17 @@ def test_good_section_names():
     _check_config(content)
 
 
-def check_no_duplicates(content, dup):
-    """ Ensure that duplicate entries raise an error """
-    with utils.tempfile(content) as tfile:
-        with nt.assert_raises(SystemExit) as e:
-            glsl.GLSLParserTest(tfile)
-
-            nt.eq_(
-                e.exception.message,
-                'Duplicate entry for key {0} in file {1}'.format(dup, tfile))
-
-
 @utils.nose_generator
 def test_duplicate_entries():
     """ Generate tests for duplicate keys in the config block """
+
+    @nt.raises(exceptions.PiglitFatalError)
+    def check_no_duplicates(content):
+        """ Ensure that duplicate entries raise an error """
+        with utils.tempfile(content) as tfile:
+            glsl.GLSLParserTest(tfile)
+
+
     content = [
         ('expect_result', '// expect_result: pass\n'),
         ('glsl_version', '// glsl_version: 1.10\n'),
@@ -275,17 +251,7 @@ def test_duplicate_entries():
         test = '// [config]\n{0}{1}// [end config]'.format(
             ''.join(x[1] for x in content), value)
 
-        yield check_no_duplicates, test, name
-
-
-def check_bad_character(tfile):
-    """ Check for bad characters """
-    with nt.assert_raises(SystemExit) as e:
-        glsl.GLSLParserTest(tfile)
-
-        # Obviously this isn't a perfect check, but it should be close enough
-        if not e.exception.message.startswith('Bad character "'):
-            raise utils.TestFailure(e.exception)
+        yield check_no_duplicates, test
 
 
 @utils.nose_generator
@@ -297,6 +263,11 @@ def glslparser_exetensions_seperators():
     glslparser test
 
     """
+    @nt.raises(exceptions.PiglitFatalError)
+    def check_bad_character(tfile):
+        """ Check for bad characters """
+        glsl.GLSLParserTest(tfile)
+
     problems = [
         ('comma seperator', '// require_extensions: ARB_ham, ARB_turkey\n'),
         ('semi-colon seperator', '// require_extensions: ARB_ham; ARB_turkey\n'),
@@ -319,19 +290,15 @@ def glslparser_exetensions_seperators():
             yield check_bad_character, tfile
 
 
-def check_good_extension(file_, desc):
-    """ A good extension should not raise a GLSLParserException """
-    try:
-        glsl.GLSLParserTest(file_)
-    except glsl.GLSLParserError:
-        nt.ok_(False,
-               'GLSLParserException was raised by "required_extensions: {}"'
-               ', but should not'.format(desc))
-
-
 @utils.nose_generator
 def test_good_extensions():
     """ Generates tests with good extensions which shouldn't raise errors """
+
+    @utils.not_raises(exceptions.PiglitFatalError)
+    def check_good_extension(file_):
+        """ A good extension should not raise a GLSLParserException """
+        glsl.GLSLParserTest(file_)
+
     content = ('// [config]\n'
                '// expect_result: pass\n'
                '// glsl_version: 1.10\n'
@@ -350,4 +317,4 @@ def test_good_extensions():
             'require_extension {} is valid'.format(x))
 
         with utils.tempfile(test) as tfile:
-            yield check_good_extension, tfile, x
+            yield check_good_extension, tfile
-- 
2.4.0



More information about the Piglit mailing list