[hawkmoth] Re: [PATCH v2 3/3] test: add diagnostic tests

  • From: Jani Nikula <jani@xxxxxxxxxx>
  • To: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>, hawkmoth mailing list <hawkmoth@xxxxxxxxxxxxx>
  • Date: Mon, 06 May 2019 23:07:28 +0300

On Wed, 01 May 2019, Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx> wrote:

This patch lists no new tests, but the existing examples provide enough
coverage for Clang related warnings and errors: we don't care for
checking every possible kind of error as long as we can assert they are
generated appropriately.

The framework is quite general though and will accept tests for any new
diagnostics we add support for.

I've gone ahead and applied this.

But I think we should eventually fix the accidental errors we get now,
and instead add separate tests with intentional errors. So this series
actually benefits us by making us aware our own tests and examples fail!

(The examples failing also produces errors/warnings in 'make
SPHINXOPTS=-vv html' output.)

Thanks,
Jani.


---
 test/example-50-struct.stderr   |  1 +
 test/example-70-function.stderr |  2 ++
 test/example-80-compat.stderr   |  2 ++
 test/test_cautodoc.py           |  4 ++--
 test/test_hawkmoth.py           |  9 +++++++--
 test/testenv.py                 | 15 +++++++++------
 6 files changed, 23 insertions(+), 10 deletions(-)
 create mode 100644 test/example-50-struct.stderr
 create mode 100644 test/example-70-function.stderr
 create mode 100644 test/example-80-compat.stderr

diff --git a/test/example-50-struct.stderr b/test/example-50-struct.stderr
new file mode 100644
index 0000000..ea95c38
--- /dev/null
+++ b/test/example-50-struct.stderr
@@ -0,0 +1 @@
+ERROR: 9: unknown type name 'list_data_t'
diff --git a/test/example-70-function.stderr b/test/example-70-function.stderr
new file mode 100644
index 0000000..1686172
--- /dev/null
+++ b/test/example-70-function.stderr
@@ -0,0 +1,2 @@
+WARNING: 9: declaration of 'struct list' will not be visible outside of this 
function
+WARNING: 9: declaration of 'enum mode' will not be visible outside of this 
function
diff --git a/test/example-80-compat.stderr b/test/example-80-compat.stderr
new file mode 100644
index 0000000..1686172
--- /dev/null
+++ b/test/example-80-compat.stderr
@@ -0,0 +1,2 @@
+WARNING: 9: declaration of 'struct list' will not be visible outside of this 
function
+WARNING: 9: declaration of 'enum mode' will not be visible outside of this 
function
diff --git a/test/test_cautodoc.py b/test/test_cautodoc.py
index 4d7c746..38bc119 100755
--- a/test/test_cautodoc.py
+++ b/test/test_cautodoc.py
@@ -23,7 +23,7 @@ def _get_output(input_filename, app, status, warning, 
**options):
 
     app.build()
 
-    return testenv.read_file(os.path.join(app.outdir, 'index.txt'))
+    return testenv.read_file(os.path.join(app.outdir, 'index.txt')), None
 
 @with_app(confdir=testenv.testdir, create_new_srcdir=True, 
buildername='text')
 def _get_expected(input_filename, app, status, warning, **options):
@@ -32,7 +32,7 @@ def _get_expected(input_filename, app, status, warning, 
**options):
 
     app.build()
 
-    return testenv.read_file(os.path.join(app.outdir, 'index.txt'))
+    return testenv.read_file(os.path.join(app.outdir, 'index.txt')), None
 
 class DirectiveTest(unittest.TestCase):
     pass
diff --git a/test/test_hawkmoth.py b/test/test_hawkmoth.py
index aa68a9c..1cf3992 100755
--- a/test/test_hawkmoth.py
+++ b/test/test_hawkmoth.py
@@ -10,16 +10,21 @@
 
 def _get_output(input_filename, **options):
     docs_str = ''
+    errors_str = ''
 
     docs, errors = parse(input_filename, **options)
 
     for (doc, meta) in docs:
         docs_str += doc + '\n'
 
-    return docs_str
+    for (severity, filename, lineno, msg) in errors:
+        errors_str += '{}: {}: {}\n'.format(severity.name, lineno, msg)
+
+    return docs_str, errors_str
 
 def _get_expected(input_filename, **options):
-    return testenv.read_file(input_filename, ext='rst')
+    return testenv.read_file(input_filename, ext='rst'), \
+        testenv.read_file(input_filename, ext='stderr')
 
 class ParserTest(unittest.TestCase):
     pass
diff --git a/test/testenv.py b/test/testenv.py
index c86749a..7f5e199 100644
--- a/test/testenv.py
+++ b/test/testenv.py
@@ -64,18 +64,21 @@ def modify_filename(filename, **kwargs):
 def read_file(filename, **kwargs):
     filename = modify_filename(filename, **kwargs)
 
-    with open(filename, 'r') as f:
-        expected = f.read()
+    if not os.path.isfile(filename):
+        # Emulate empty file.
+        return ''
 
-    return expected
+    with open(filename, 'r') as f:
+        return f.read()
 
 def _test_generator(get_output, get_expected, input_filename, **options):
     """Return a function that compares output/expected results on 
input_filename."""
     def test(self):
-        output = get_output(input_filename, **options)
-        expected = get_expected(input_filename, **options)
+        output_docs, output_errors = get_output(input_filename, **options)
+        expect_docs, expect_errors = get_expected(input_filename, **options)
 
-        self.assertEqual(expected, output)
+        self.assertEqual(expect_docs, output_docs)
+        self.assertEqual(expect_errors, output_errors)
 
     return test
 
-- 
2.21.0

Other related posts: