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

Dylan Baker baker.dylan.c at gmail.com
Tue Apr 21 15:44:30 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 | 107 ++++++++++--------------------
 2 files changed, 47 insertions(+), 89 deletions(-)

diff --git a/framework/test/glsl_parser_test.py b/framework/test/glsl_parser_test.py
index 0ab3b0e..26c46f5 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)
 
@@ -154,25 +149,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 eb0f9bd..cba63bb 100644
--- a/framework/tests/glsl_parser_test_tests.py
+++ b/framework/tests/glsl_parser_test_tests.py
@@ -21,20 +21,15 @@
 """ 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
-
 
 def _check_config(content):
     """ This is the test that actually checks the glsl config section """
@@ -55,55 +50,41 @@ def test_no_config_start():
                 msg="No config section found, no exception raised")
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_find_config_start():
     """ GLSLParserTest finds [config] """
     content = ('// [config]\n'
                '// glsl_version: 1.00\n'
                '//\n')
     with utils.with_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():
     """ GLSLParserTest requires [end config] """
     with utils.with_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():
     """ expect_result section is required """
     content = ('// [config]\n'
                '// glsl_version: 1.00\n'
                '//\n')
     with utils.with_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():
     """ glsl_version section is required """
     content = ('// [config]\n'
                '// expect_result: pass\n'
                '// [end config]\n')
     with utils.with_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():
@@ -206,6 +187,7 @@ def test_config_to_command():
         yield check_config_to_command, config, result
 
 
+ at nt.raises(exceptions.PiglitFatalError)
 def test_bad_section_name():
     """ A section name not in the _CONFIG_KEYS name raises an error """
     content = ('// [config]\n'
@@ -215,14 +197,10 @@ def test_bad_section_name():
                '// [end config]\n')
 
     with utils.with_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(exceptions.PiglitFatalError)
 def test_good_section_names():
     """ A section name in the _CONFIG_KEYS does not raise an error """
     content = ('// [config]\n'
@@ -232,26 +210,20 @@ def test_good_section_names():
                '// check_link: True\n'
                '// [end config]\n')
 
-    try:
-        _check_config(content)
-    except glsl.GLSLParserException as e:
-        raise AssertionError(e)
-
-
-def check_no_duplicates(content, dup):
-    """ Ensure that duplicate entries raise an error """
-    with utils.with_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))
+    _check_config(content)
 
 
 @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.with_tempfile(content) as tfile:
+            glsl.GLSLParserTest(tfile)
+
+
     content = [
         ('expect_result', '// expect_result: pass\n'),
         ('glsl_version', '// glsl_version: 1.00\n'),
@@ -264,17 +236,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 AssertionError(e.exception)
+        yield check_no_duplicates, test
 
 
 @utils.nose_generator
@@ -286,6 +248,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'),
@@ -307,19 +274,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.GLSLParserException:
-        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.00\n'
@@ -337,4 +300,4 @@ def test_good_extensions():
             'require_extension {} is valid'.format(x)
 
         with utils.with_tempfile(test) as tfile:
-            yield check_good_extension, tfile, x
+            yield check_good_extension, tfile
-- 
2.3.5



More information about the Piglit mailing list