Debian Codegraph @

test/test_CLI.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2022 Günter Milde.
3
4 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
5 #
6 # Copying and distribution of this file, with or without modification,
7 # are permitted in any medium without royalty provided the copyright
8 # notice and this notice are preserved.
9 # This file is offered as-is, without any warranty.
10 #
11 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
12
13 # :Id: $Id: test_CLI.py 10091 2025-04-17 12:21:36Z milde $
14
15 """
16 Test module for the command line interface.
17 """
18
19 import difflib
20 from io import StringIO
21 import locale
22 from pathlib import Path
23 import os
24 import re
25 import sys
26 import unittest
27
28 if __name__ == '__main__':
29 # prepend the "docutils root" to the Python library path
30 # so we import the local `docutils` package.
31 sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
32
33 from docutils import __main__, core
34
35 # DATA_ROOT is ./test/data/ from the docutils root
36 DATA_ROOT = Path(__file__).parent / 'data'
37
38
39 def print_mismatch(expected, output):
40 diff = ''.join(difflib.unified_diff(
41 expected.splitlines(keepends=True),
42 output.splitlines(keepends=True),
43 'expected', 'output'))
44 raise AssertionError('Unexpected output:\n'+diff)
45
46
47 class CliTests(unittest.TestCase):
48
49 def setUp(self):
50 # save state
51 self.orig_argv = sys.argv
52 self.orig_stdout = sys.stdout
53 os.environ['DOCUTILSCONFIG'] = '' # don't read config files
54 sys.stdout = StringIO() # re-direct sys.stdout
55
56 def tearDown(self):
57 del os.environ['DOCUTILSCONFIG']
58 sys.stdout = self.orig_stdout
59 sys.argv = self.orig_argv
60 # restore default locale settings:
61 if sys.platform != "win32":
62 locale.setlocale(locale.LC_MESSAGES, 'C')
63 locale.setlocale(locale.LC_TIME, 'C')
64
65 def get_help_text(self, prog, entry_point):
66 # call entry_point function and collect help text
67 sys.argv = [prog, '--help']
68 try:
69 entry_point()
70 except SystemExit:
71 pass
72 output = sys.stdout.getvalue()
73 # replace unpredictable paths (eventually wrapped)
74 output = re.sub(r'default:[^)]*[/\\][^)]*\)', 'default: [...])',
75 output, flags=re.DOTALL)
76 # normalise error encoding default
77 output = output.replace(
78 'error output. Default: '
79 f'{core.OptionParser.default_error_encoding}',
80 'error output. Default: utf-8')
81 return output
82
83 def test_main_help(self):
84 # collect help text
85 output = self.get_help_text('docutils', __main__.main)
86
87 # compare to stored version
88 docutils_txt = os.path.join(DATA_ROOT, 'help/docutils.rst')
89 expected = Path(docutils_txt).read_text(encoding='utf-8')
90 if expected != output:
91 print_mismatch(expected, output)
92
93 def test_rst2html_help(self):
94 # collect help text
95 output = self.get_help_text('rst2html', core.rst2html)
96 # compare to stored version
97 rst2html_txt = os.path.join(DATA_ROOT, 'help/rst2html.rst')
98 expected = Path(rst2html_txt).read_text(encoding='utf-8')
99 if expected != output:
100 print_mismatch(expected, output)
101
102 def test_rst2latex_help(self):
103 # collect help text
104 output = self.get_help_text('rst2latex', core.rst2latex)
105 # compare to stored version
106 rst2latex_txt = os.path.join(DATA_ROOT, 'help/rst2latex.rst')
107 expected = Path(rst2latex_txt).read_text(encoding='utf-8')
108 if expected != output:
109 print_mismatch(expected, output)
110
111
112 if __name__ == '__main__':
113 unittest.main()