Mask trailing whitespace in test sample.
[docutils.git] / docutils / test / test_CLI.py
blob2bada39b283900f3f5d151e234ca18efaae679b2
1 #!/usr/bin/env python3
2 # :Copyright: © 2022 Günter Milde.
4 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
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.
11 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
13 # :Id: $Id$
15 """
16 Test module for the command line interface.
17 """
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
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]))
33 from docutils import __main__, core
35 # DATA_ROOT is ./test/data/ from the docutils root
36 DATA_ROOT = Path(__file__).parent / 'data'
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)
47 class CliTests(unittest.TestCase):
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
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 locale.setlocale(locale.LC_MESSAGES, 'C')
62 locale.setlocale(locale.LC_TIME, 'C')
64 def get_help_text(self, prog, entry_point):
65 # call entry_point function and collect help text
66 sys.argv = [prog, '--help']
67 try:
68 entry_point()
69 except SystemExit:
70 pass
71 output = sys.stdout.getvalue()
72 # replace unpredictable paths (eventually wrapped)
73 output = re.sub(r'default:[^)]*[/\\][^)]*\)', 'default: [...])',
74 output, flags=re.DOTALL)
75 # normalise error encoding default
76 output = output.replace(
77 f'{core.OptionParser.default_error_encoding}:backslashreplace',
78 'utf-8:backslashreplace')
79 return output
81 def test_main_help(self):
82 # collect help text
83 output = self.get_help_text('docutils', __main__.main)
85 # compare to stored version
86 docutils_txt = os.path.join(DATA_ROOT, 'help/docutils.txt')
87 with open(docutils_txt, encoding='utf-8') as samplefile:
88 expected = samplefile.read()
89 if expected != output:
90 print_mismatch(expected, output)
92 def test_rst2html_help(self):
93 # collect help text
94 output = self.get_help_text('rst2html', core.rst2html)
95 # compare to stored version
96 rst2html_txt = os.path.join(DATA_ROOT, 'help/rst2html.txt')
97 with open(rst2html_txt, encoding='utf-8') as samplefile:
98 expected = samplefile.read()
99 if expected != output:
100 print_mismatch(expected, output)
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.txt')
107 with open(rst2latex_txt, encoding='utf-8') as samplefile:
108 expected = samplefile.read()
109 if expected != output:
110 print_mismatch(expected, output)
113 if __name__ == '__main__':
114 unittest.main()