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
16 Test module for the command line interface.
20 from io
import StringIO
22 from pathlib
import Path
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
):
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
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']
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')
81 def test_main_help(self
):
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
):
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
):
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__':