From d7694943e715ec6e3514a26f7ccbfbaf10b01aa3 Mon Sep 17 00:00:00 2001 From: milde Date: Sun, 3 Jan 2021 21:05:04 +0000 Subject: [PATCH] Fix tests for code directive warnings. The Docutils parser test suite sets report_level to 5. Test warnings for non-supported languages with publish_string(). git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8598 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- .../test_rst/test_directives/test_code.py | 15 +++-- .../test_rst/test_directives/test_code_long.py | 3 +- .../test_rst/test_directives/test_code_parsing.py | 68 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py diff --git a/docutils/test/test_parsers/test_rst/test_directives/test_code.py b/docutils/test/test_parsers/test_rst/test_directives/test_code.py index c68df0542..14d0903a3 100644 --- a/docutils/test/test_parsers/test_rst/test_directives/test_code.py +++ b/docutils/test/test_parsers/test_rst/test_directives/test_code.py @@ -214,6 +214,8 @@ totest['code-parsing'] = [ This is a code block with text. """], ["""\ +Code not parsed but warning silenced in ParserTestCase. + .. code:: s-lang % abc.sl @@ -221,14 +223,11 @@ totest['code-parsing'] = [ """, """\ - - - Cannot analyze code. No Pygments lexer found for "s-lang". - - .. code:: s-lang - \n\ - % abc.sl - autoload("abc_mode", "abc"); + + Code not parsed but warning silenced in ParserTestCase. + + % abc.sl + autoload("abc_mode", "abc"); """], ["""\ Place the language name in a class argument to avoid the no-lexer warning: diff --git a/docutils/test/test_parsers/test_rst/test_directives/test_code_long.py b/docutils/test/test_parsers/test_rst/test_directives/test_code_long.py index 89249f73f..9266470a4 100644 --- a/docutils/test/test_parsers/test_rst/test_directives/test_code_long.py +++ b/docutils/test/test_parsers/test_rst/test_directives/test_code_long.py @@ -15,7 +15,8 @@ from test_parsers import DocutilsTestSupport from docutils.utils.code_analyzer import with_pygments def suite(): - s = DocutilsTestSupport.ParserTestSuite(suite_settings={'syntax_highlight':'long'}) + settings = {'syntax_highlight':'long'} + s = DocutilsTestSupport.ParserTestSuite(suite_settings=settings) if with_pygments: s.generateTests(totest) return s diff --git a/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py b/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py new file mode 100644 index 000000000..646403320 --- /dev/null +++ b/docutils/test/test_parsers/test_rst/test_directives/test_code_parsing.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- +# :Copyright: © 2020 Günter Milde. +# :License: Released under the terms of the `2-Clause BSD license`_, in short: +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. +# This file is offered as-is, without any warranty. +# +# .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause + +""" +Various tests for the `pygments` code highlighter. +""" + +from __future__ import absolute_import +import unittest + +if __name__ == '__main__': + import __init__ +from test_parsers import DocutilsTestSupport # must be imported before docutils +from docutils import core, utils +from docutils.core import publish_string +from docutils.utils.code_analyzer import with_pygments + +unknown_language = """\ +Unknown language "S-Lang". + +.. code:: s-lang + + % abc.sl + autoload("abc_mode", "abc"); +""" + +class_arg = """\ +Unknown language "S-Lang". + +.. code:: + :class: s-lang + + % abc.sl + autoload("abc_mode", "abc"); +""" + +skip_msg = 'optional module "pygments" not found' +settings = {'warning_stream': ''} + +class CodeParsingTests(unittest.TestCase): + + @unittest.skipUnless(with_pygments, skip_msg) + def test_lexer_error(self): + output = publish_string(unknown_language, settings_overrides=settings) + self.assertIn(b'', output) + + @unittest.skipUnless(with_pygments, skip_msg) + def test_lexer_error_workaround(self): + output = publish_string(class_arg, settings_overrides=settings) + self.assertNotIn(b'